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

Postby kolpobilashi » Fri Aug 18, 2006 12:50 pm

thanks a lot guys :D
btw....i have already spectacles....seems the power got increased :-?
Sanjana
kolpobilashi
Learning poster
 
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh

10591 WA!!

Postby Daniele Furlan » Fri Oct 06, 2006 11:17 am

Why this code is WA??? :cry: :cry: :cry:

[code]import java.io.*;

class Main {

int[] square = new int[10];
int[] mem = new int[1000];
int ris=0;

static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}


void TryHappyNumber() {

String input;
int val=0;
int count=0;
int step=0;
int res;

for (int i = 0; i < 10; i++) { //Calcolo quadrati "base"
square[i] = i * i;
}

mem[1]=2;

int k = Integer.parseInt(Main.ReadLn(255));
count=k;
val=0;

int init=0;
int[] valori = new int[count];

while (val<count) {
k = Integer.parseInt(Main.ReadLn(255));
valori[val]=k;
if (init==0) {
res = isHappy(sum_cifre(k));
val++;
if (res==2) {
init=1;
}
else {
init=1;
step++; //Comincio a stampare dal successivo caso
}
}
else {
init=1;
val++;
res = isHappy(sum_cifre(k));
}
}


for (int i=step; i<count; i++) {
if (mem[sum_cifre(valori[i])]==2) {
System.out.println("Case #" + (i+1-step) +": " + valori[i] + " is a Happy number.");
}
else {
System.out.println("Case #" + (i+1-step) +": " + valori[i] + " is a Unhappy number.");
}
}
}


int isHappy(int n) {

int k = sum_cifre(n);

if ((mem[n] == 0)) {
mem[n] = 3; //annoto che l'ho gi
Daniele Furlan
New poster
 
Posts: 2
Joined: Fri Oct 06, 2006 11:09 am

Postby Daniele Furlan » Fri Oct 06, 2006 7:28 pm

Hello!!!

Raiyan Kamal's sample inputs is correct for my program.
I submit it but got WA. :( :(
Could anyone help me???

[code]#include <stdio.h>

typedef long long int llint;

void TryHappyNumber();
llint isHappy(llint n);
llint sum_cifre(llint n);

llint square[10];
llint mem[811];
llint ris=0;


void main() {

llint count=0;
llint res;
llint k;
llint i;

for (i = 0; i < 10; i++) { //Calcolo quadrati "base"
square[i] = i * i;
}

mem[1]=2;

scanf( "%lld", &k );
count=k;


for (i=1; i<=count; i++) {
scanf( "%lld", &k );
res = isHappy(sum_cifre(k));
printf( "Case #%lld: ", i );
if (res==2) {
printf( "%lld is a Happy number.\n", k );
}
else {
printf( "%lld is a Unhappy number.\n", k );
}
}
}


//Trova se un numero
Daniele Furlan
New poster
 
Posts: 2
Joined: Fri Oct 06, 2006 11:09 am

Postby newton » Mon Mar 12, 2007 3:47 pm

dear beloni
your algorithm is so nice.

okey now try this input.

Code: Select all

 2
 7
 7



may it will be helpfull to you.






newton........................................... simply the best
User avatar
newton
Experienced poster
 
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh

Postby Saul Hidalgo » Sat Mar 01, 2008 8:59 am

Hi to all! :wink:

I got WA. I review the format output, and all, but i got wa too. Here is my code.

Code: Select all
AC. Thanks Helloneo



THanks to all. Bye
Last edited by Saul Hidalgo on Sat Mar 01, 2008 9:29 am, edited 1 time in total.
Saul Hidalgo
New poster
 
Posts: 18
Joined: Wed Jan 03, 2007 2:36 am
Location: Los Teques, Venezuela

Postby helloneo » Sat Mar 01, 2008 9:12 am

Try this input.. :-)

Code: Select all
1
4



My output..

Code: Select all
Case #1: 4 is an Unhappy number.



PS. Remove you code after AC.. :-)
helloneo
Guru
 
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Postby Saul Hidalgo » Sat Mar 01, 2008 9:28 am

helloneo wrote:Try this input.. :-)

Code: Select all
1
4



My output..

Code: Select all
Case #1: 4 is an Unhappy number.



PS. Remove you code after AC.. :-)


Hi Helloneo. Thanks for your quick answer. I got AC. :wink: :wink: Thanks.
Saul Hidalgo
New poster
 
Posts: 18
Joined: Wed Jan 03, 2007 2:36 am
Location: Los Teques, Venezuela

10591 - help plz -Happy Number

Postby sazzadcsedu » Wed Dec 31, 2008 11:32 pm

whats wrong with my code??
why Runtime error??

Code: Select all
removed
Last edited by sazzadcsedu on Thu Jan 01, 2009 9:00 pm, edited 1 time in total.
sazzadcsedu
Experienced poster
 
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.

Re: 10591 - Happy Number

Postby shiplu_1320 » Thu Jan 01, 2009 1:21 pm

You should get RTE not WA because of this
Code: Select all
int arr[2000];
............
..........
if(arr[n]==1)
...............

where n can be much bigger than 2000.
A learner......
shiplu_1320
New poster
 
Posts: 32
Joined: Sat Dec 29, 2007 9:08 pm
Location: CSEDU , Dhaka

Re: 10591 - Happy Number

Postby sazzadcsedu » Thu Jan 01, 2009 8:57 pm

thanx shiplu.
found the mistake & got accepted.
sazzadcsedu
Experienced poster
 
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.

Re: 10591 - Happy Number

Postby toru » Mon Jan 05, 2009 6:44 am

PLZZ HELP !!!!!!!!!!!!!!!!!

Hi, How can i check for the repetative number for finding the unhappy number...........

Thanxx in advance

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

Re: 10591 - Happy Number

Postby 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............ :)
Articuno
Learning poster
 
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm
Location: IUT-OIC, Dhaka, Bangladesh

10591 - Happy Number

Postby 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;
}
toru
New poster
 
Posts: 17
Joined: Tue Dec 30, 2008 10:38 am

Re: 10591 - Happy Number

Postby 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
MRH
Learning poster
 
Posts: 51
Joined: Mon Aug 11, 2008 9:09 pm

Re: 10591 - Happy Number

Postby 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);
    }
}
Martuza_iu
New poster
 
Posts: 4
Joined: Tue Sep 21, 2010 4:17 pm
Location: Islamic University, Kushtia

PreviousNext

Return to Volume CV

Who is online

Users browsing this forum: No registered users and 1 guest