10591  Happy Number
Moderator: Board moderators

 New poster
 Posts: 32
 Joined: Thu Jul 31, 2003 6:21 am
 Location: Daffodil Univ, Bangladesh
 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]
[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!
Visit phpBB!

 Guru
 Posts: 724
 Joined: Wed Dec 19, 2001 2:00 am
 Location: Germany

 New poster
 Posts: 32
 Joined: Thu Jul 31, 2003 6:21 am
 Location: Daffodil Univ, Bangladesh
 Contact:
I have got ac fixing a mistake in print. Thanks Guru. I always make mistake in writing anything .
Hate WA
Visit phpBB!
Visit phpBB!
10591(wrong answer)
who can tell me what's wrong???
special input or output!!!
thanks
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

 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.

 Experienced poster
 Posts: 106
 Joined: Thu Jan 29, 2004 12:07 pm
 Location: Bangladesh
 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.
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.
10591CE
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;
}
#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
10591pls 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;
}
#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
10591RunTimeError
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;
}
#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
Re: 10591CE
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.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; }
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
Re: 10591RunTimeError
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
Got WA again
Raiyan Kamal's sample inputs is correct for my program.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.
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");
}
}