Page 2 of 5

Posted: Thu Apr 14, 2005 6:05 pm
by Raiyan Kamal
Dear Bugmans,

Are you sure you got WA with this code ? I think you are supposed to get Compile error because of these two lines:

Code: Select all

freopen("10591i.txt","r",stdin); 
freopen("10591o.txt","w",stdout);
I assume these two lines dont exist in your submitted code. Then have a look at this part,

Code: Select all

if(IsHappyNumber(N)==TRUE) 
     printf("Case #%ld: %ld is a Happy number.\n"); 
   else 
     printf("Case #%ld: %ld is an Unhappy number.\n");
You are probably using a feature of C / C++ that is not platform independent. May be the judge computer handles these situations differently than you computer.

10591 TLE

Posted: Mon Dec 05, 2005 6:55 pm
by thirddawn

Code: Select all

#include <stdio.h>

int main(){
	int get,ans,i,j,check,counter;
	scanf("%d",&counter);
	for (j = 0;j < counter;j++){
		check = 0;
		scanf("%d",&get);
		printf("Case #%d: ",j+1);
		ans = 0;
		i= get;
		while (check != 1 && check != get){
			ans = ans + (i%10) * (i%10) ;
			i = i / 10;
			if (i == 0){
				check = ans;
				i = ans;
				ans = 0;
			}
		}
		if (check == get)
			printf("%d is an Unhappy number.\n",get);
		if (check == 1)
			printf("%d is a Happy number.\n",get);
	}
	return 0;
}
I think I handle it with a simple way,but dunno why I get TLE = ="

Anyway,thanks for assisting

Posted: Mon Dec 05, 2005 7:13 pm
by ayon
cycle caused an endless loop. try input 2. your program gives:
check->2->4->16->37->58->89->145->42->20->4...endless cycle, program must stop at 4, bcoz 4 is found before.

10591 why WA !!!

Posted: Wed May 31, 2006 10:58 pm
by beloni
hello,
why the following code was WA ???

Code: Select all


#include <stdio.h>
#define SIZE 10000


typedef long long int llint;


llint store[SIZE];

int ishappy( llint num )
{
	llint sum = num, tmp;

	while(1)
		{
			tmp = sum;
			sum = 0;
			while( tmp > 0 )
				{
					sum += (tmp%10) * (tmp%10);
					tmp /= 10;
				}

			if( store[sum] != num )	/* avoid loops */
				store[sum] = num;
			else
				return 0;

			if( sum == 1 )
				return 1;
			if( sum == num )
				return 0;
		}
}


int main()
{
	llint ncases, w, num;

	for( w = 0; w < SIZE; w++ )
		store[w] = -1;
	scanf( "%lld", &ncases );
	for( w = 1; w <= ncases; w++ )
		{
			scanf( "%lld", &num );
			printf( "Case #%lld: ", w );
			if( ishappy( num ) )
				printf( "%lld is a Happy number.\n", num );
			else
				printf( "%lld is an Unhappy number.\n", num );
		}

	return 0;
}
thanks

Re: 10591 why WA !!!

Posted: Thu Jun 01, 2006 5:55 am
by Martin Macko
There are already plenty of topics on this problem... Please, read them and use one of them to post your question instead of creating a new one. Eg., see http://online-judge.uva.es/board/viewtopic.php?t=6400.

Posted: Thu Jun 01, 2006 1:05 pm
by beloni
hello,
why the following code was WA ???

Code: Select all

#include <stdio.h>
#define SIZE 10000


typedef long long int llint;


llint store[SIZE];

int ishappy( llint num )
{
   llint sum = num, tmp;

   while(1)
      {
         tmp = sum;
         sum = 0;
         while( tmp > 0 )
            {
               sum += (tmp%10) * (tmp%10);
               tmp /= 10;
            }

         if( store[sum] != num )   /* avoid loops */
            store[sum] = num;
         else
            return 0;

         if( sum == 1 )
            return 1;
         if( sum == num )
            return 0;
      }
}


int main()
{
   llint ncases, w, num;

   for( w = 0; w < SIZE; w++ )
      store[w] = -1;
   scanf( "%lld", &ncases );
   for( w = 1; w <= ncases; w++ )
      {
         scanf( "%lld", &num );
         printf( "Case #%lld: ", w );
         if( ishappy( num ) )
            printf( "%lld is a Happy number.\n", num );
         else
            printf( "%lld is an Unhappy number.\n", num );
      }

   return 0;
}
thanks

Posted: Fri Jun 02, 2006 8:51 pm
by Martin Macko
beloni wrote:hello,
why the following code was WA ???
Your code's not working for the following input

Code: Select all

2
10
10
The correct output is

Code: Select all

Case #1: 10 is a Happy number.
Case #2: 10 is a Happy number.

Posted: Fri Jun 02, 2006 10:17 pm
by beloni
thanks guy,

I've additioned the following function before each call to ishappy():

Code: Select all

void init_store()
{
        int w;
        for( w = 0; w < SIZE; w++ )
                store[w] = -1;
}
I got AC, thank you very much

Posted: Sat Jun 03, 2006 2:51 am
by Martin Macko
beloni wrote:I got AC, thank you very much
As you've already got AC, please, remove your code from the previous post. So we won't have too much spoilers here.

10591 CE! sumbuddy help me!

Posted: Thu Jul 06, 2006 10:36 am
by kolpobilashi
from some prev posts i got some sample input/output which are exactly same with my code, but still i got CE from the judge. i received the msg below.

Code: Select all

04704672_24.c: In function `int main()':
04704672_24.c:14: implicit declaration of function `int itoa(...)'

"implicit declaration"...what does it mean? is the itoa function restricted here? plz tell me. :(

Posted: Thu Jul 06, 2006 2:05 pm
by ayon
itoa() function is not allowed in unix, you have to write the function yourself, but better process is using sprintf() function as
sprintf(str, "%d", n);

Posted: Thu Jul 06, 2006 8:26 pm
by kolpobilashi
:)

thanx a lot..
but now i got WA.will any1 check out???

Code: Select all

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



int main()
{
	int m,j;
	while(scanf("%ld",&m)!=EOF)
	{
		for(j=1;j<=m;j++)
		{	 
			long long n;
			scanf("%lld",&n);
			char s2[22];
			sprintf(s2,"%d",n);
			do
			{
				int i,len;
				len=strlen(s2);
				long long sqr1=0;
				for(i=0;i<len;i++)
				{
					int p=s2[i]-'0';
					sqr1=sqr1+(p*p);
				}
				s2[0]='\0';
				
				sprintf(s2,"%d",sqr1);
				
			}while(strlen(s2)!=1);
			if(atoi(s2)==1)
				printf("Case #%d: %lld is a Happy number.\n",j,n);
			else
				printf("Case #%d: %lld is a Unhappy number.\n",j,n);
		
		}
	}
	return 0;
}

:)

Posted: Fri Jul 07, 2006 11:40 am
by nymo
Hi,
you declared m as int, still you read with ...

Code: Select all

while(scanf("%ld",&m)!=EOF) ... 

it should be 

while (scanf("%d", &m) != EOF)
doing sprintf() several times is costly, but you can easily do it with loop [you can separate the digits of a number]

I don't properly remember how I solved it... but I think I stored the generated numbers and check for repeatations... don't assume the repeating cycle begins with the same input number. i.e. for 4, repeating cycle begins with 4, but probably I assumed it may not be the case always.

and...
there are only 10 different digits. you can easily have their square values precomputed rather than squaring everytime :)

Posted: Fri Jul 07, 2006 4:04 pm
by kolpobilashi
thanx a lot. now ive changed the code like below...and got WA :cry:
wats wrong....can any1 say....



Code: Select all

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

int main()
{
	long long m,j;
	long long arr[]={0,1,4,9,16,25,36,49,64,81};
	while(scanf("%lld",&m)!=EOF)
	{
		for(j=1;j<=m;j++)
		{	 
			long long n;
			scanf("%lld",&n);
			char s2[22];
			sprintf(s2,"%d",n);
			do
			{
				long long i,len;
				len=strlen(s2);
				long long sqr1=0;
				for(i=0;i<len;i++)
				{
					long long p=s2[i]-'0';
					sqr1=sqr1+arr[p];
				}
				s2[0]='\0';
				
				sprintf(s2,"%d",sqr1);
				
			}while(strlen(s2)!=1);
			if(atoi(s2)==1)
				printf("Case #%lld: %lld is a Happy number.\n",j,n);
			else
				printf("Case #%lld: %lld is a Unhappy number.\n",j,n);
		
		}
	}
	return 0;
}

10591::WA?????

Posted: Thu Aug 03, 2006 5:26 am
by savage
my code print right for every number but i got WA.plz check my code n
give me some clue why WA :(

#include <iostream>

using namespace std;

long long square_digit(long long a);

removed after ac :roll: