Moderator: Board moderators

#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;
}beloni wrote:hello,
why the following code was WA ???
2
10
10Case #1: 10 is a Happy number.
Case #2: 10 is a Happy number.
void init_store()
{
int w;
for( w = 0; w < SIZE; w++ )
store[w] = -1;
}
beloni wrote:I got AC, thank you very much

04704672_24.c: In function `int main()':
04704672_24.c:14: implicit declaration of function `int itoa(...)'#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;
}while(scanf("%ld",&m)!=EOF) ...
it should be
while (scanf("%d", &m) != EOF)
#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;
}Case #p: N is an Unhappy number.
Copy & Paste method from the Output Section of problemUsers browsing this forum: No registered users and 0 guests