10591 - Happy Number
Moderator: Board moderators
-
- Learning poster
- Posts: 78
- Joined: Sun Nov 30, 2008 5:00 pm
- Location: IUT-OIC, Dhaka, Bangladesh
Re: 10591 - Happy Number
I think one of the easiest way is to use an array, check every value whether it is already in the array,if it is, then this is an unhappy number. Otherwise store that value in the array.
May be tomorrow is a better day............ ![:)](./images/smilies/icon_smile.gif)
![:)](./images/smilies/icon_smile.gif)
10591 - Happy Number
PLZZZZZZZZZZZZZ HELP!!!!!!!!!!
HI,
After Gud Effort i could solve this Happy Number.
BUT RUNTIME ERROR. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I checked with large number of input, and every output came correct, but why RTE????
I attach my code here, plz help..............
megh putra.
HI,
After Gud Effort i could solve this Happy Number.
BUT RUNTIME ERROR. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I checked with large number of input, and every output came correct, but why RTE????
I attach my code here, plz help..............
megh putra.
Code: Select all
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define maxc 100000
#define maxl 100000
long Strrev(char *Str)
{
char Temp[maxc]={'\0'};
long l=strlen(Str),i;
for(i=0;i<l;i++)
{
Temp[i]=Str[l-i-1];
}
Temp[i]='\0';
strcpy(Str,Temp);
return 0;
}
int main()
{
char in[maxc], out[maxc];
long store[maxl];
long len, ho1=0, i=0, j=0, hold, get, sum, let=0, hv, kk=0, tt, mm, flag=0, test1, test=1, ii;
long o, p;
scanf("%ld", &test1);
for(ii=0; ii<test1; ii++)
{
memset(store, '0', sizeof(store));
memset(out, '0', sizeof(out));
memset(in, '0', sizeof(in));
flag=0;
kk=0;
scanf("%s", in);
mm=atol(in);
store[kk]=mm;
kk++;
ho1=atol(in);
len=strlen(in);
while(1)
{
sum=0;let=0;
for(i=0; i<len; i++)
{
let=in[i]-'0';
sum+=pow(let, 2);
}
if(sum==1)
{
get=1;
break;
}
else if(sum==ho1)
{
get=ho1;
break;
}
else
{
j=0;
memset(out, '0', sizeof(out));
while(1)
{
out[j]=(sum%10)+'0';
hold=sum/10;
sum=hold;
if(hold==0)
{
j++;
break;
}
j++;
}
out[j]='\0';
Strrev(out);
hv=atol(out);
store[kk]=hv;
kk++;
for(tt=0; tt<kk-1; tt++)
{
if(store[tt]==hv)
{
flag=1;
break;
}
}
if(flag==1)
{
get=ho1;
break;
}
strcpy(in, out);
len=strlen(in);
}
}
if(get==1)
printf("Case #%ld: %ld is a Happy number.\n", test++, ho1);
else if(get==ho1)
printf("Case #%ld: %ld is an Unhappy number.\n", test++, ho1);
}
return 0;
}
Re: 10591 - Happy Number
hello "toru", all local varible becam global and reduce array size
array size 730 huge for this problem
why u use scanf("%s", in);
all string operation replace by long
long is perfect for this problen
array size 730 huge for this problem
why u use scanf("%s", in);
all string operation replace by long
long is perfect for this problen
-
- New poster
- Posts: 4
- Joined: Tue Sep 21, 2010 4:17 pm
- Location: Islamic University, Kushtia
- Contact:
Re: 10591 - Happy Number
Why get WA pls help me .........
Code: Select all
#include<stdio.h>
#define max 10000
long unhappy(long n);
long search(long t[max],long s,long c);
long m,c,t[max],j;
main()
{
long x;
scanf("%ld",&x);
for(j=1;j<=x;j++)
{
scanf("%ld",&m);
c=1;
unhappy(m);
}
return 0;
}
long unhappy(long n)
{
long p,q,sum;
if(n>9)
{
sum=0;
while(n!=0)
{
p=n%10;
q=p*p;
sum=sum+q;
n=n/10;
}
}
else
sum=n*n;
t[c]=sum;
c=c+1;
search(t,t[c-1],c-1);
return 0;
}
long search(long t[max],long s,long u)
{
long i;
if(s==1)
{
printf("Case #%ld: %ld is a Happy Number.\n",j,m);
return 0;
}
else
{
for(i=1;i<u;i++)
if(s==t[i])
{
printf("Case #%ld: %ld is an Unhappy number.\n",j,m);
return 0;
}
return unhappy(s);
}
}
-
- New poster
- Posts: 1
- Joined: Sat Mar 17, 2012 12:26 pm
Re: 10591 - Happy Number
#include<stdio.h>
#include<math.h>
int main()
{
int t,i;
scanf("%d",&t);
//int n,sum=0,j,p;
// p=sqrt(n);
for(i=1;i<=t;i++)
{
int n,sum=0,j,p,s;
scanf("%d",&n);
p=n;
while(p>=9)
{
for(j=p;j>0;j=j/10)
{
s=j%10;
sum=sum+s*s;
}
p=sum;
sum=0;
}
if(p==1)
printf("Case #%d: %d is a Happy number.\n",i,n);
else
printf("Case #%d: %d is an Unhappy number.\n",i,n);
sum=0;
p=0;
}
return 0;
}
i have problem with input 7 what is the prob
#include<math.h>
int main()
{
int t,i;
scanf("%d",&t);
//int n,sum=0,j,p;
// p=sqrt(n);
for(i=1;i<=t;i++)
{
int n,sum=0,j,p,s;
scanf("%d",&n);
p=n;
while(p>=9)
{
for(j=p;j>0;j=j/10)
{
s=j%10;
sum=sum+s*s;
}
p=sum;
sum=0;
}
if(p==1)
printf("Case #%d: %d is a Happy number.\n",i,n);
else
printf("Case #%d: %d is an Unhappy number.\n",i,n);
sum=0;
p=0;
}
return 0;
}
i have problem with input 7 what is the prob
Code: Select all
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10591 - Happy Number
The problem is in this line
while(p>=9)
Your code doesn't enter the loop if the input <10.
while(p>=9)
Your code doesn't enter the loop if the input <10.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 21
- Joined: Mon Apr 08, 2013 8:38 am
Re: 10591 why WA !!!
#include <iostream>
using namespace std;
int main()
{
long long n,p,s,r,u,i,m,count=0;
cin>>u;
for(i=0; i<u; i++)
{
count++;
cin>>m;
s=0;
n=m;
while(1)
{
r=n%10;
p=n/10;
s=s+(r*r);
// cout<<s<<" "<<"p="<<p<<" n="<<n<<endl;
// break;
if(s==1||s==m)
{break;}
if(p==0)
{
n=0;
n=s;
s=0;
}
else
{
n=p;
}
}
if(s==1)
{
cout<<"Case #"<<count<<": "<<m<<" is a Happy number."<<endl;
}
else if(s==m)
{
cout<<"Case #"<<count<<": "<<m<<" is an Unhappy number."<<endl;
}
}
return 0;
}
I get WA but why?
using namespace std;
int main()
{
long long n,p,s,r,u,i,m,count=0;
cin>>u;
for(i=0; i<u; i++)
{
count++;
cin>>m;
s=0;
n=m;
while(1)
{
r=n%10;
p=n/10;
s=s+(r*r);
// cout<<s<<" "<<"p="<<p<<" n="<<n<<endl;
// break;
if(s==1||s==m)
{break;}
if(p==0)
{
n=0;
n=s;
s=0;
}
else
{
n=p;
}
}
if(s==1)
{
cout<<"Case #"<<count<<": "<<m<<" is a Happy number."<<endl;
}
else if(s==m)
{
cout<<"Case #"<<count<<": "<<m<<" is an Unhappy number."<<endl;
}
}
return 0;
}
I get WA but why?
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10591 - Happy Number
Input:AC output:
Code: Select all
1
2
Code: Select all
Case #1: 2 is an Unhappy number.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 8
- Joined: Sun Nov 17, 2013 9:55 pm
Re: 10591 - Happy Number
i cannot understand why i get wrong answer for 11011111
Code: Select all
/************************************10591_Happy_Number.cpp*************************************/
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
unsigned long long n, m, i, j, k, sum;
cin>>n;
j = 0;
while(n--)
{
cin>>k;
m = k;
while(1)
{
sum = 0;
while(k!=0)
{
i = k%10;
k = k/10;
sum = sum + i*i;
}
if(sum<10)
{
break;a
}
k = sum;
}
if(sum==1)
{
cout<<"Case #"<<++j<<": "<<m<<" is a Happy number.\n";
}
else
{
cout<<"Case #"<<++j<<": "<<m<<" is a Unhappy number.\n";
}
}
return 0;
}
Last edited by brianfry713 on Mon Dec 08, 2014 10:23 pm, edited 1 time in total.
Reason: Added code blocks
Reason: Added code blocks
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10591 - Happy Number
That code won't compile
Check input and AC output for thousands of problems on uDebug!
Re: 10591 - Happy Number
Code: Select all
Removed after AC
Last edited by imran_12 on Sun Dec 07, 2014 5:19 pm, edited 1 time in total.
Re: 10591 - Happy Number
Input
Acc Output
Code: Select all
1
1
Code: Select all
Case #1: 1 is a Happy number.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Re: 10591 - Happy Number
i got this..i edited my code and got AC..but i have a question...plz ans me..
my question is : in my code i put the values that rayan kamal given as critical input..in there, these cases give "happy number"
Case #28: 1111111 is a Happy number.
Case #29: 10111111 is a Happy number.
Case #30: 11110111 is a Happy number.
BUT, in my code it gives "unhappy number"
though i got AC.....WHY??
my question is : in my code i put the values that rayan kamal given as critical input..in there, these cases give "happy number"
Case #28: 1111111 is a Happy number.
Case #29: 10111111 is a Happy number.
Case #30: 11110111 is a Happy number.
BUT, in my code it gives "unhappy number"
though i got AC.....WHY??
Re: 10591 - Happy Number
It seems that judge's input doesn't contain such numbers. ![:)](./images/smilies/icon_smile.gif)
![:)](./images/smilies/icon_smile.gif)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman