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.
#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 = ="
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
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
#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)
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
#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;
}