## 10591 - Happy Number

Moderator: Board moderators

Jewel of DIU
New poster
Posts: 32
Joined: Thu Jul 31, 2003 6:21 am
Contact:

### 10591 - Happy Number

I have tried to solve 10591: Happy number but I got WA. Please help me. Here is my code.
[c]

CUT AFTER AC
[/c]
Last edited by Jewel of DIU on Mon Jan 05, 2004 10:26 am, edited 1 time in total.
Hate WA
Visit phpBB!

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany
is an Unappy number
compare your output to the sample output, character by character

Jewel of DIU
New poster
Posts: 32
Joined: Thu Jul 31, 2003 6:21 am
Contact:
I have got ac fixing a mistake in print. Thanks Guru. I always make mistake in writing anything .
Hate WA
Visit phpBB!

Lon
New poster
Posts: 7
Joined: Sat Jan 31, 2004 12:55 pm

who can tell me what's wrong???
special input or output!!!
thanks
Last edited by Lon on Mon Jul 12, 2004 4:45 am, edited 1 time in total.
my name is Lon

Lon
New poster
Posts: 7
Joined: Sat Jan 31, 2004 12:55 pm
I find the wrong!!
my name is Lon

Salmin Sultana
New poster
Posts: 16
Joined: Sun Mar 07, 2004 12:19 pm
Location: Dhaka

### 10591

i found the fault myself,yet thanx to raiyan

Code: Select all

``````[cpp]
the code has been cut
[cpp]``````
Last edited by Salmin Sultana on Tue Sep 14, 2004 4:18 am, edited 1 time in total.

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Contact:
Here are some sample cases. Hope these helps you.

INPUT:

37

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
111
1111
11111
1000
100
10
101
1111111
10111111
11110111
22
55
99
109
10900
91
900001

OUTPUT:

Case #1: 1 is a Happy number.
Case #2: 2 is an Unhappy number.
Case #3: 3 is an Unhappy number.
Case #4: 4 is an Unhappy number.
Case #5: 5 is an Unhappy number.
Case #6: 6 is an Unhappy number.
Case #7: 7 is a Happy number.
Case #8: 8 is an Unhappy number.
Case #9: 9 is an Unhappy number.
Case #10: 10 is a Happy number.
Case #11: 11 is an Unhappy number.
Case #12: 12 is an Unhappy number.
Case #13: 13 is a Happy number.
Case #14: 14 is an Unhappy number.
Case #15: 15 is an Unhappy number.
Case #16: 16 is an Unhappy number.
Case #17: 17 is an Unhappy number.
Case #18: 18 is an Unhappy number.
Case #19: 19 is a Happy number.
Case #20: 20 is an Unhappy number.
Case #21: 111 is an Unhappy number.
Case #22: 1111 is an Unhappy number.
Case #23: 11111 is an Unhappy number.
Case #24: 1000 is a Happy number.
Case #25: 100 is a Happy number.
Case #26: 10 is a Happy number.
Case #27: 101 is an Unhappy number.
Case #28: 1111111 is a Happy number.
Case #29: 10111111 is a Happy number.
Case #30: 11110111 is a Happy number.
Case #31: 22 is an Unhappy number.
Case #32: 55 is an Unhappy number.
Case #33: 99 is an Unhappy number.
Case #34: 109 is a Happy number.
Case #35: 10900 is a Happy number.
Case #36: 91 is a Happy number.
Case #37: 900001 is a Happy number.

59557RC
New poster
Posts: 26
Joined: Sun Mar 20, 2005 9:28 pm
Contact:

### 10591-CE

pls tell me why CE on 10591 :
#include<stdio.h>

int main(void)
{

unsigned long a,b,i,c,num[500],u[500],sum;

scanf("%lu",&c);
for(i=0;i<c;i++) scanf("%ld",&num);
for(i=0;i<c;i++) u=num;
for(i=0;i<c;i++) {
do{
sum=0;
a=u;

do{

b=a%10; //
sum=sum+(b*b);
a=a/10;

} while(a!=0);

u=sum;

}while(u / 10 != 0);
if(sum==1) printf("Case #%lu: %lu is a Happy number.\n",i+1,num);
else printf("Case #%lu: %lu is a Unhappy number.\n",i+1,num);
}
return 0;
}
aaa

59557RC
New poster
Posts: 26
Joined: Sun Mar 20, 2005 9:28 pm
Contact:

### 10591-pls help

pls tell me why CE on 10591 :
#include<stdio.h>

int main(void)
{

unsigned long a,b,i,c,num[500],u[500],sum;

scanf("%lu",&c);
for(i=0;i<c;i++) scanf("%ld",&num);
for(i=0;i<c;i++) u=num;
for(i=0;i<c;i++) {
do{
sum=0;
a=u;

do{

b=a%10; //
sum=sum+(b*b);
a=a/10;

} while(a!=0);

u=sum;

}while(u / 10 != 0);
if(sum==1) printf("Case #%lu: %lu is a Happy number.\n",i+1,num);
else printf("Case #%lu: %lu is a Unhappy number.\n",i+1,num);
}
return 0;
}
aaa

59557RC
New poster
Posts: 26
Joined: Sun Mar 20, 2005 9:28 pm
Contact:

### 10591-RunTimeError

i don't understand what's wrong. it ran without any problem & the output is also correct.so why RuntimeError

#include<stdio.h>

int main(void)
{

unsigned long a,b,i,c,num[1000],u,sum=0;

scanf("%lu",&c);
for(i=0;i<c;i++) scanf("%ld",&num);

for(i=0;i<c;i++) {
u=num;
do{
sum=0;
a=num;

b=a%10;
sum=sum+(b*b);
a=a/10;
while(a!=0){

b=a%10;
sum=sum+(b*b);
a=a/10;

}

num=sum;

}while(num / 10 != 0);
if(sum==1) printf("Case #%lu: %lu is a Happy number.\n",i+1,u);
else printf("Case #%lu: %lu is a Unhappy number.\n",i+1,u);
}
return 0;
}
aaa

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

### Re: 10591-CE

59557RC wrote:pls tell me why CE on 10591 :

Code: Select all

``````#include<stdio.h>

int main(void)
{

unsigned long a,b,i,c,num[500],u[500],sum;

scanf("%lu",&c);
for(i=0;i<c;i++) scanf("%ld",&num[i]);
for(i=0;i<c;i++) u[i]=num[i];
for(i=0;i<c;i++) {
do{
sum=0;
a=u[i];

do{

b=a%10;    //
sum=sum+(b*b);
a=a/10;

} while(a!=0);

u[i]=sum;

}while(u[i] / 10 != 0);
if(sum==1) printf("Case #%lu: %lu is a Happy number.\n",i+1,num[i]);
else printf("Case #%lu: %lu is a Unhappy number.\n",i+1,num[i]);
}
return 0;
}``````
I guess that you submitted this as a C program. The Judge's compiler doesn't like C++ style comments while compiling C programs. Try to remove the redundant // from your code.
Also remember that you can't allocate on the stack big arrays. To avoid compile error for this reason make your arrays global. Maybe this doesn't apply for your program, I don't remember exctly what is the size of memory available on the stack.

Anyway you can check your mailbox as the judge sends an email with the errors from the compiler. If you didn't get it, check that the email address you gave when you registered to the online judge is correct and still valid.

Ciao!!!

Claudio

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

### Re: 10591-RunTimeError

59557RC wrote:i don't understand what's wrong. it ran without any problem & the output is also correct.so why RuntimeError

#include<stdio.h>

int main(void)
{

unsigned long a,b,i,c,num[1000],u,sum=0;

scanf("%lu",&c);
for(i=0;i<c;i++) scanf("%ld",&num);

for(i=0;i<c;i++) {
u=num;
do{
sum=0;
a=num;

b=a%10;
sum=sum+(b*b);
a=a/10;
while(a!=0){

b=a%10;
sum=sum+(b*b);
a=a/10;

}

num=sum;

}while(num / 10 != 0);
if(sum==1) printf("Case #%lu: %lu is a Happy number.\n",i+1,u);
else printf("Case #%lu: %lu is a Unhappy number.\n",i+1,u);
}
return 0;
}

Please post to the appropriate forum and possibly find a thread already open on this problem number.
As for your problem, you assume that you'll get at most 1000 numbers in input. probably this is not the case for Judge's data.
If you fix this ( you don't need to store all the numbers in input first and process it later, just process every number as you read it) you'll get a WA. Check the exact spelling for your answers.

Ciao!!!

Claudio

Learning poster
Posts: 90
Joined: Mon Feb 16, 2004 8:53 pm
Location: Bangalore INDIA
---> b=a%10; //

..C++ style comments are not allowed in ANSI C remove the "//"
if u can think of it .. u can do it in software.

ibrahim
Experienced poster
Posts: 149
Joined: Mon Feb 07, 2005 10:28 pm
Contact:
Thanks Raiyan Kamal.
I got AC useing your i/o.

bugmans
New poster
Posts: 1
Joined: Thu Apr 14, 2005 4:41 pm

### Got WA again

Raiyan Kamal wrote:Here are some sample cases. Hope these helps you.

INPUT:

37

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
111
1111
11111
1000
100
10
101
1111111
10111111
11110111
22
55
99
109
10900
91
900001

OUTPUT:

Case #1: 1 is a Happy number.
Case #2: 2 is an Unhappy number.
Case #3: 3 is an Unhappy number.
Case #4: 4 is an Unhappy number.
Case #5: 5 is an Unhappy number.
Case #6: 6 is an Unhappy number.
Case #7: 7 is a Happy number.
Case #8: 8 is an Unhappy number.
Case #9: 9 is an Unhappy number.
Case #10: 10 is a Happy number.
Case #11: 11 is an Unhappy number.
Case #12: 12 is an Unhappy number.
Case #13: 13 is a Happy number.
Case #14: 14 is an Unhappy number.
Case #15: 15 is an Unhappy number.
Case #16: 16 is an Unhappy number.
Case #17: 17 is an Unhappy number.
Case #18: 18 is an Unhappy number.
Case #19: 19 is a Happy number.
Case #20: 20 is an Unhappy number.
Case #21: 111 is an Unhappy number.
Case #22: 1111 is an Unhappy number.
Case #23: 11111 is an Unhappy number.
Case #24: 1000 is a Happy number.
Case #25: 100 is a Happy number.
Case #26: 10 is a Happy number.
Case #27: 101 is an Unhappy number.
Case #28: 1111111 is a Happy number.
Case #29: 10111111 is a Happy number.
Case #30: 11110111 is a Happy number.
Case #31: 22 is an Unhappy number.
Case #32: 55 is an Unhappy number.
Case #33: 99 is an Unhappy number.
Case #34: 109 is a Happy number.
Case #35: 10900 is a Happy number.
Case #36: 91 is a Happy number.
Case #37: 900001 is a Happy number.
Raiyan Kamal's sample inputs is correct for my program.
I submit it but got WA.
Could anyone help me.

Code: Select all

``````#include<stdio.h>
#define TRUE 1
#define FALSE 0
int IsHappyNumber(long N)
{
long tempN=N,i,n,NextN,stack[1000],index=0;
stack[index++]=N;
while(1)
{NextN=0;
do{n=tempN%10;
NextN+=n*n;
}
while((tempN/=10)!=0);
if(NextN==1)
return TRUE;
for(i=0;i<index;i++)
if(stack[i]==NextN)
return FALSE;
stack[index++]=NextN;
tempN=NextN;
}
}
/****************************************/
void main()
{
freopen("10591i.txt","r",stdin);
freopen("10591o.txt","w",stdout);
long total,i,N,count=0;
scanf("%ld",&total);
for(i=0;i<total;i++)
{scanf("%ld",&N);
count++;
if(IsHappyNumber(N)==TRUE)
printf("Case #%ld: %ld is a Happy number.\n");
else
printf("Case #%ld: %ld is an Unhappy number.\n");
}
}``````