## 11185 - Ternary

Moderator: Board moderators

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

### Re: 11185 - Ternary

Try this case..

Code: Select all

``````3
-1``````

My output..

Code: Select all

``10``

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

### 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.

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### 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)

shaon_cse_cu08
New poster
Posts: 50
Joined: Tue May 25, 2010 9:10 am
Contact:

### 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...
I'll keep holding on...Until the walls come tumbling down...And freedom is all around .....

desperate12
New poster
Posts: 1
Joined: Sat Dec 10, 2011 12:27 pm

### 11185 - ternary

Why I have got WA I can't understand ... ...

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

int main()
{
long long a;
int ternary[200],i,j;
while(cin>>a)
{
if(a<0)
{
return 0;
}

if(a==0)
{
cout<<'0'<<endl;
}

else
{
for(i=0;a>0;i++)
{
ternary=a%3;
a=a/3;
}
for(j=i-1;j>=0;j--)
{
cout<<ternary[j];
}
}
cout<<endl;

}
return 0;
}

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

### Re: 11185 - ternary

You're printing an extra newline for N=0.
Check input and AC output for thousands of problems on uDebug!

ratul.i
New poster
Posts: 2
Joined: Mon Jul 23, 2012 1:14 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;
}

``````

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

### Re: 11185 - Ternary

Code: Select all

``char *c;``
try

Code: Select all

``char c[MAXSIZE];``
Check input and AC output for thousands of problems on uDebug!

ratul.i
New poster
Posts: 2
Joined: Mon Jul 23, 2012 1:14 am

### Re: 11185 - Ternary

Thnx, Thnx a lot brianfry713 ... Accepted

Nahian37
New poster
Posts: 2
Joined: Sun Oct 14, 2012 11:45 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=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?

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

### Re: 11185 - Ternary

For input:

Code: Select all

``````0
-1``````
AC output:

Code: Select all

``0``
Check input and AC output for thousands of problems on uDebug!

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

### 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=0;
str[i++]+=48;
}

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

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

}
return 0;
}

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

### Re: 11185 - Ternary

strrev isn't supported in the judge's compiler.
Check input and AC output for thousands of problems on uDebug!

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

### Re: 11185 - Ternary

Tnx sir.....

raihan_sust5
New poster
Posts: 6
Joined: Tue Jul 23, 2013 3:04 am

### Re: 11185 - Ternary

any one please give some critical test case so i can test my code.....i am having a wrong answer..