Code: Select all
Case #p: N is an Unhappy number.
![:wink:](./images/smilies/icon_wink.gif)
rabbi
Moderator: Board moderators
Code: Select all
Case #p: N is an Unhappy number.
Code: Select all
Copy & Paste method from the Output Section of problem
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)
{
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
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
Code: Select all
2
7
7
Code: Select all
AC. Thanks Helloneo
Code: Select all
1
4
Code: Select all
Case #1: 4 is an Unhappy number.
Hi Helloneo. Thanks for your quick answer. I got AC.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..
Code: Select all
int arr[2000];
............
..........
if(arr[n]==1)
...............