10591 - Happy Number

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

Moderator: Board moderators

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Post by Raiyan Kamal » Thu Apr 14, 2005 6:05 pm

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.

thirddawn
New poster
Posts: 9
Joined: Sat Oct 15, 2005 4:41 am

10591 TLE

Post by thirddawn » Mon Dec 05, 2005 6:55 pm

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

ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon » Mon Dec 05, 2005 7:13 pm

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.
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program

beloni
Learning poster
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

10591 why WA !!!

Post by beloni » Wed May 31, 2006 10:58 pm

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
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor

User avatar
Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Re: 10591 why WA !!!

Post by Martin Macko » Thu Jun 01, 2006 5:55 am

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.

beloni
Learning poster
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

Post by beloni » Thu Jun 01, 2006 1:05 pm

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
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor

User avatar
Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Post by Martin Macko » Fri Jun 02, 2006 8:51 pm

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.

beloni
Learning poster
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

Post by beloni » Fri Jun 02, 2006 10:17 pm

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
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor

User avatar
Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Post by Martin Macko » Sat Jun 03, 2006 2:51 am

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.

kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh
Contact:

10591 CE! sumbuddy help me!

Post by kolpobilashi » Thu Jul 06, 2006 10:36 am

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. :(
Sanjana

ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon » Thu Jul 06, 2006 2:05 pm

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);
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program

kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh
Contact:

Post by kolpobilashi » Thu Jul 06, 2006 8:26 pm

:)

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;
}
Sanjana

nymo
Experienced poster
Posts: 149
Joined: Sun Jun 01, 2003 8:58 am
Location: :)

:)

Post by nymo » Fri Jul 07, 2006 11:40 am

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

kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh
Contact:

Post by kolpobilashi » Fri Jul 07, 2006 4:04 pm

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;
}
Sanjana

savage
New poster
Posts: 4
Joined: Sat Feb 11, 2006 4:07 pm
Location: Bangladesh

10591::WA?????

Post by savage » Thu Aug 03, 2006 5:26 am

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:
Last edited by savage on Sun Aug 13, 2006 3:18 pm, edited 1 time in total.

Post Reply

Return to “Volume 105 (10500-10599)”