10591 - Happy Number

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

Moderator: Board moderators

Re: 10591 why WA !!!

Postby Martin Macko » Thu Jun 01, 2006 5:55 am

There are already plenty of topics on this problem... Please, read them and use one of them to post your question instead of creating a new one. Eg., see http://online-judge.uva.es/board/viewtopic.php?t=6400.
User avatar
Martin Macko
A great helper
 
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Postby beloni » Thu Jun 01, 2006 1:05 pm

hello,
why the following code was WA ???

Code: Select all
#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;
}


thanks
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor
beloni
Learning poster
 
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

Postby Martin Macko » Fri Jun 02, 2006 8:51 pm

beloni wrote:hello,
why the following code was WA ???

Your code's not working for the following input
Code: Select all
2
10
10

The correct output is
Code: Select all
Case #1: 10 is a Happy number.
Case #2: 10 is a Happy number.
User avatar
Martin Macko
A great helper
 
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Postby beloni » Fri Jun 02, 2006 10:17 pm

thanks guy,

I've additioned the following function before each call to ishappy():
Code: Select all
void init_store()
{
        int w;
        for( w = 0; w < SIZE; w++ )
                store[w] = -1;
}


I got AC, thank you very much
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor
beloni
Learning poster
 
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

Postby Martin Macko » Sat Jun 03, 2006 2:51 am

beloni wrote:I got AC, thank you very much

As you've already got AC, please, remove your code from the previous post. So we won't have too much spoilers here.
User avatar
Martin Macko
A great helper
 
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

10591 CE! sumbuddy help me!

Postby kolpobilashi » Thu Jul 06, 2006 10:36 am

from some prev posts i got some sample input/output which are exactly same with my code, but still i got CE from the judge. i received the msg below.

Code: Select all
04704672_24.c: In function `int main()':
04704672_24.c:14: implicit declaration of function `int itoa(...)'



"implicit declaration"...what does it mean? is the itoa function restricted here? plz tell me. :(
Sanjana
kolpobilashi
Learning poster
 
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh

Postby ayon » Thu Jul 06, 2006 2:05 pm

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
ayon
Experienced poster
 
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Postby kolpobilashi » Thu Jul 06, 2006 8:26 pm

:)

thanx a lot..
but now i got WA.will any1 check out???

Code: Select all
#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;
}
Sanjana
kolpobilashi
Learning poster
 
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh

:)

Postby nymo » Fri Jul 07, 2006 11:40 am

Hi,
you declared m as int, still you read with ...
Code: Select all
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 :)
nymo
Experienced poster
 
Posts: 149
Joined: Sun Jun 01, 2003 8:58 am
Location: :)

Postby kolpobilashi » Fri Jul 07, 2006 4:04 pm

thanx a lot. now ive changed the code like below...and got WA :cry:
wats wrong....can any1 say....




Code: Select all
#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;
}
Sanjana
kolpobilashi
Learning poster
 
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh

10591::WA?????

Postby savage » Thu Aug 03, 2006 5:26 am

my code print right for every number but i got WA.plz check my code n
give me some clue why WA :(

#include <iostream>

using namespace std;

long long square_digit(long long a);

removed after ac :roll:
Last edited by savage on Sun Aug 13, 2006 3:18 pm, edited 1 time in total.
savage
New poster
 
Posts: 4
Joined: Sat Feb 11, 2006 4:07 pm
Location: Bangladesh

Postby asif_rahman0 » Thu Aug 03, 2006 1:36 pm

Code: Select all
Case #p: N is an Unhappy number.


May be need some spectacles. :wink:

rabbi
asif_rahman0
Experienced poster
 
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

Postby savage » Sun Aug 13, 2006 3:22 pm

thanks asif,
yeah need some spectacles :o
savage
New poster
 
Posts: 4
Joined: Sat Feb 11, 2006 4:07 pm
Location: Bangladesh

Postby savage » Sun Aug 13, 2006 3:32 pm

Hello sanjana,

Code: Select all
Case #p: N is an Unhappy number.

also u need sum spectacles :wink:
me also get WA for this line :roll:

rishan
savage
New poster
 
Posts: 4
Joined: Sat Feb 11, 2006 4:07 pm
Location: Bangladesh

Postby asif_rahman0 » Sun Aug 13, 2006 9:47 pm

i think you should just follow the
Code: Select all
Copy & Paste method from the Output Section of problem

if you are not attentive!!!!!!

I solved this problem just Checking out for 4!!!!.
asif_rahman0
Experienced poster
 
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

PreviousNext

Return to Volume CV

Who is online

Users browsing this forum: No registered users and 0 guests