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

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

Code: Select all

Case #p: N is an Unhappy number.
May be need some spectacles.

rabbi

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

savage
New poster
Posts: 4
Joined: Sat Feb 11, 2006 4:07 pm
Hello sanjana,

Code: Select all

Case #p: N is an Unhappy number.
also u need sum spectacles
me also get WA for this line

rishan

asif_rahman0
Experienced poster
Posts: 209
Joined: Sun Jan 16, 2005 6:22 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!!!!.

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

Daniele Furlan
New poster
Posts: 2
Joined: Fri Oct 06, 2006 11:09 am

### 10591 WA!!

Why this code is WA???

Code: Select all

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)
{
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) {
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
Hello!!!

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

Code: Select all

#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

newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:
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

Saul Hidalgo
New poster
Posts: 18
Joined: Wed Jan 03, 2007 2:36 am
Location: Los Teques, Venezuela
Hi to all!

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.

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea
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..

Saul Hidalgo
New poster
Posts: 18
Joined: Wed Jan 03, 2007 2:36 am
Location: Los Teques, Venezuela
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. Thanks.

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### 10591 - help plz -Happy Number

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.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359

shiplu_1320
New poster
Posts: 32
Joined: Sat Dec 29, 2007 9:08 pm
Location: CSEDU , Dhaka
Contact:

### Re: 10591 - Happy Number

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

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### Re: 10591 - Happy Number

thanx shiplu.
found the mistake & got accepted.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359

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

### Re: 10591 - Happy Number

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

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