10591 - Happy Number

All about problems in Volume 105. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

Re: 10591 - Happy Number

Post by Articuno » Mon Jan 05, 2009 1:48 pm

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............ :)

toru
New poster
Posts: 17
Joined: Tue Dec 30, 2008 10:38 am

10591 - Happy Number

Post by toru » Mon Jan 05, 2009 5:15 pm

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;
}

MRH
Learning poster
Posts: 51
Joined: Mon Aug 11, 2008 9:09 pm

Re: 10591 - Happy Number

Post by MRH » Mon Jan 05, 2009 5:28 pm

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

Martuza_iu
New poster
Posts: 4
Joined: Tue Sep 21, 2010 4:17 pm
Location: Islamic University, Kushtia
Contact:

Re: 10591 - Happy Number

Post by Martuza_iu » Fri Oct 08, 2010 8:27 pm

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);
    }
}

sn23581
New poster
Posts: 2
Joined: Thu Jul 29, 2010 2:02 pm

Post by sn23581 » Fri Mar 18, 2011 6:46 am

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

Ekram Hossain
New poster
Posts: 1
Joined: Sat Mar 17, 2012 12:26 pm

Re: 10591 - Happy Number

Post by Ekram Hossain » Sun Apr 01, 2012 7:36 pm

#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

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10591 - Happy Number

Post by brianfry713 » Mon Apr 02, 2012 11:44 pm

The problem is in this line
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!

Angry Bird
New poster
Posts: 21
Joined: Mon Apr 08, 2013 8:38 am

Re: 10591 why WA !!!

Post by Angry Bird » Mon Apr 08, 2013 8:51 am

#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?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10591 - Happy Number

Post by brianfry713 » Tue Apr 09, 2013 3:59 am

Input:

Code: Select all

1
2
AC output:

Code: Select all

Case #1: 2 is an Unhappy number.
Check input and AC output for thousands of problems on uDebug!

reza_cse08
New poster
Posts: 8
Joined: Sun Nov 17, 2013 9:55 pm

Re: 10591 - Happy Number

Post by reza_cse08 » Tue Dec 03, 2013 6:26 pm

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

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10591 - Happy Number

Post by brianfry713 » Wed Dec 04, 2013 12:02 am

That code won't compile
Check input and AC output for thousands of problems on uDebug!

imran_12
New poster
Posts: 9
Joined: Fri Sep 19, 2014 7:56 am

Re: 10591 - Happy Number

Post by imran_12 » Sun Dec 07, 2014 4:43 pm

Code: Select all

Removed after AC
Last edited by imran_12 on Sun Dec 07, 2014 5:19 pm, edited 1 time in total.

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10591 - Happy Number

Post by lighted » Sun Dec 07, 2014 5:03 pm

Input

Code: Select all

1
1
Acc Output

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

imran_12
New poster
Posts: 9
Joined: Fri Sep 19, 2014 7:56 am

Re: 10591 - Happy Number

Post by imran_12 » Sun Dec 07, 2014 5:18 pm

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??

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10591 - Happy Number

Post by lighted » Sun Dec 07, 2014 5:43 pm

It seems that judge's input doesn't contain such numbers. :)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

Post Reply

Return to “Volume 105 (10500-10599)”