## 11185 - Ternary

Moderator: Board moderators

### Re: 11185 - Ternary

Try this case..

Code: Select all
`3-1`

My output..

Code: Select all
`10`
helloneo
Guru

Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

### Re: 11185 - Ternary Why W/A

I know My Algorithm is right, Then Why wrong answer

Code: Select all
`Thanks Sohel. I forget the basic rules of Checking Input range.Code Remove After A/C`
Last edited by noor_aub on Fri May 28, 2010 3:19 pm, edited 1 time in total.
noor_aub
New poster

Posts: 26
Joined: Sat Aug 22, 2009 12:16 pm

### Re: 11185 - Ternary

What does your program output if the input is 0?
Hint: When you get WA in a problem, try to check the extreme cases first( In this case 0 and 1000000000)

sohel
Guru

Posts: 862
Joined: Thu Jan 30, 2003 5:50 am
Location: University of Texas at San Antonio

### Re: 11185 - Ternary

Can Someone Help me to determine what is wrong with my code... I have tested several case...
Code: Select all
`Code Removed After AC....`

I didn't think of the out put 0 when the case is 0...
It's not who i m inside me... But what i do, that defines me...:)
shaon_cse_cu08
New poster

Posts: 47
Joined: Tue May 25, 2010 9:10 am

### Re: 11185 - Ternary

Can anyone please tell me why I get runtime error for this code:

Code: Select all
`#include<stdio.h>void reverse(char *num, int count){    while(count>=0)    {        printf("%c",num[count]);        count--;    }}int main(){    long int a;    while(1)    {        scanf("%ld",&a);        if(a<0)            break;        else if(a==0)            printf("0\n");        else        {            char *c; int i=0;            while(a>=1)            {                long int x = a%3;                c[i]=x+'0';                a /= 3;                i++;            }            reverse(c, i-1);            printf("\n");        }    }    return 0;}`
ratul.i
New poster

Posts: 2
Joined: Mon Jul 23, 2012 1:14 am

### Re: 11185 - Ternary

Code: Select all
`char *c;`

try
Code: Select all
`char c[MAXSIZE];`
brianfry713
Guru

Posts: 1771
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11185 - Ternary

Thnx, Thnx a lot brianfry713 ... Accepted
ratul.i
New poster

Posts: 2
Joined: Mon Jul 23, 2012 1:14 am

### Re: 11185 - Ternary

#include<stdio.h>
int main()
{
long n,i,j,a[50];
while(1)
{
scanf("%ld",&n);
if(n<=0) break;

for(i=0;n>0;i++)
{
a[i]=n%3;
n=n/3;
}
for(j=i-1;j>=0;j--)
{
printf("%ld",a[j]);
}
printf("\n");
}
return 0;
}
whats the problem with this code?
Nahian37
New poster

Posts: 2
Joined: Sun Oct 14, 2012 11:45 am

### Re: 11185 - Ternary

For input:
Code: Select all
`0-1`
AC output:
Code: Select all
`0`
brianfry713
Guru

Posts: 1771
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11185 - Ternary

I've accepted this problem after 3 times compiler error.But i can't find out the problem why the code is compiled error. For this I ignore the string.h function and do the task manually and got accepted . But still now I am in fix why I can't use strrev() function ? my previous code given below:

#include <stdio.h>
#include <string.h>

int main ()

{
char str [50];
int n,i;
scanf ("%d",&n);

while (n>=0){

i=0;
if (n==0){
str[i]=0;
str[i++]+=48;
}

while (n!=0){
str[i]=n%3;
str[i++]+=48;
n/=3;
}
str[i]='\0';

printf ("%s\n",strrev(str));
scanf ("%d",&n);

}
return 0;
}
shuvrothpol1
New poster

Posts: 17
Joined: Wed Aug 15, 2012 12:37 pm

### Re: 11185 - Ternary

strrev isn't supported in the judge's compiler.
brianfry713
Guru

Posts: 1771
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11185 - Ternary

Tnx sir.....
shuvrothpol1
New poster

Posts: 17
Joined: Wed Aug 15, 2012 12:37 pm

Previous