Ultraviolet A 10591-Happy Number

Source: Internet
Author: User

Type: hash table

Original question:
Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let the sum of the squares of the digits of S1 be represented by S2 and so on. if Si = 1 for some I ≥ 1, then the original integer S0 is said to be Happy number. A number, which is not happy, is called Unhappy number. for example 7 is a Happy number since 7-> 49-> 97-> 130-> 10-> 1 and 4 is an Unhappy number since 4-> 16-> 37-> 58-> 89-> 145-> 42-> 20-> 4.

Input
The input consists of several test cases, the number of which you are given in the first line of the input. each test case consists of one line containing a single positive integer N smaller than 10 ^ 9.

Output
For each test case, you must print one of the following messages:
Case # p: N is a Happy number.
Case # p: N is an Unhappy number.
Here p stands for the case number (starting from 1). You shoshould print the first message if
Number N is a happy number. Otherwise, print the second line.

Sample input:
3
7
4
13

Sample output:
Case #1: 7 is a Happy number.
Case #2: 4 is an Unhappy number.
Case #3: 13 is a Happy number.

Question:
The so-called Happy number is to give a positive number s, then calculate the sum of squares on each bit of it, get its next number, then the next number continues and select the sum of squares on each ...... If we continue to calculate and there is no previous Number but 1, congratulations, this is a Happy Number.
If a previous occurrence occurs during the calculation process, it will not be Happy.

Ideas and summary:
This is the simplest hash application. The academic point is to directly address a table.
The maximum number of digits for this question is 10 ^ 9. If the maximum number of digits is 9, the sum of squares is 9*9*9 = 729, therefore, you only need to open an array of more than 730.
For the number that appears, the element marked as true in the corresponding array subscript

[Cpp]
/*
* UV 10591-Happy Number
* Time: 0.008 s (ultraviolet)
* Author: D_Double
*/
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Int hash [810];
 
Inline int getSum (int n ){
Int sum = 0;
While (n ){
Int t = n % 10;
Sum + = t * t;
N/= 10;
}
Return sum;
}
 
Int main (){
Int T, cas = 1;
Scanf ("% d", & T );
While (T --){
Int N, M;
Memset (hash, 0, sizeof (hash ));
Scanf ("% d", & N );
M = N;
Bool flag = false;
Int cnt = 1;
While (M = getSum (M )){
If (M = 1) {flag = true; break ;}
Else if (hash [M] | M = N) {break ;}
Hash [M] = 1;
}
 
Printf ("Case # % d:", cas ++ );
If (flag) printf ("% d is a Happy number. \ n", N );
Else printf ("% d is an Unhappy number. \ n", N );
}
Return 0;
}


Author: shuangde800

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.