11185 - Ternary

All about problems in Volume 111. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

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

Re: 11185 - Ternary

Post by helloneo » Mon Jun 08, 2009 4:25 pm

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

Post by noor_aub » Fri May 28, 2010 7:57 am

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.

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

Re: 11185 - Ternary

Post by sohel » Fri May 28, 2010 10:22 am

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)

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

Re: 11185 - Ternary

Post by shaon_cse_cu08 » Wed Jul 28, 2010 7:50 am

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 ..... :x

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

11185 - ternary

Post by desperate12 » Sat Dec 10, 2011 12:37 pm

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

Post by brianfry713 » Fri Jan 13, 2012 1:47 am

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

Post by ratul.i » Mon Jul 23, 2012 1:17 am

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

Post by brianfry713 » Tue Jul 24, 2012 1:41 am

instead of

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

Post by ratul.i » Tue Jul 24, 2012 2:10 am

Thnx, Thnx a lot brianfry713 ... Accepted :D

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

Re: 11185 - Ternary

Post by Nahian37 » Sun Oct 14, 2012 11:50 am

#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

Post by brianfry713 » Tue Oct 16, 2012 1:19 am

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

Post by shuvrothpol1 » Thu Oct 25, 2012 9:28 am

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

Post by brianfry713 » Fri Oct 26, 2012 12:22 am

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

Post by shuvrothpol1 » Fri Oct 26, 2012 9:13 pm

Tnx sir.....

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

Re: 11185 - Ternary

Post by raihan_sust5 » Tue Jul 23, 2013 2:35 pm

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

Post Reply

Return to “Volume 111 (11100-11199)”