Page 4 of 5

Re: 10591 - Happy Number

Posted: Mon Jan 05, 2009 1:48 pm
by Articuno
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.

10591 - Happy Number

Posted: Mon Jan 05, 2009 5:15 pm
by toru
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.

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

Posted: Mon Jan 05, 2009 5:28 pm
by MRH
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

Re: 10591 - Happy Number

Posted: Fri Oct 08, 2010 8:27 pm
by Martuza_iu
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);
    }
}

Posted: Fri Mar 18, 2011 6:46 am
by sn23581
a number is happy if it ends in 1 after any step of generating the square sum of the digits.
and a number is unhappy if it ends in 4 after any step of generating the square sum of the digits.

this optimisation gets AC :D

Re: 10591 - Happy Number

Posted: Sun Apr 01, 2012 7:36 pm
by Ekram Hossain
#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

Re: 10591 - Happy Number

Posted: Mon Apr 02, 2012 11:44 pm
by brianfry713
The problem is in this line
while(p>=9)
Your code doesn't enter the loop if the input <10.

Re: 10591 why WA !!!

Posted: Mon Apr 08, 2013 8:51 am
by Angry Bird
#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?

Re: 10591 - Happy Number

Posted: Tue Apr 09, 2013 3:59 am
by brianfry713
Input:

Code: Select all

1
2
AC output:

Code: Select all

Case #1: 2 is an Unhappy number.

Re: 10591 - Happy Number

Posted: Tue Dec 03, 2013 6:26 pm
by reza_cse08
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;
}

Re: 10591 - Happy Number

Posted: Wed Dec 04, 2013 12:02 am
by brianfry713
That code won't compile

Re: 10591 - Happy Number

Posted: Sun Dec 07, 2014 4:43 pm
by imran_12

Code: Select all

Removed after AC

Re: 10591 - Happy Number

Posted: Sun Dec 07, 2014 5:03 pm
by lighted
Input

Code: Select all

1
1
Acc Output

Code: Select all

Case #1: 1 is a Happy number.

Re: 10591 - Happy Number

Posted: Sun Dec 07, 2014 5:18 pm
by imran_12
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??

Re: 10591 - Happy Number

Posted: Sun Dec 07, 2014 5:43 pm
by lighted
It seems that judge's input doesn't contain such numbers. :)