UVa 10591:happy Number

Source: Internet
Author: User
Tags cas hash integer printf

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1532

Type: Hash table

Original title:

Let the sum of the square of the digits of a positive integer S0 is represented by S1. In a similar way, let the sum of the squares of the digits of S1 is represented by S2 and. If Si = 1 for some i≥1, then the original integer S0 was said to be Happy number. A number, which is isn't happy, is called unhappy number. For the example 7 is a Happy number since 7->-> 1-> 130-> Ten-> 4 is a unhappy number since 4-& Gt -> Notoginseng->->--> 145->--> 4.

Input

The input consists of several test cases, the number of which you are given in the the the input. Each test case consists the 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 was an unhappy number.
Here is P stands for the "Case number" (starting from 1). You should print the
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 are an unhappy number.

Case #3: a Happy number.

The main effect of the topic:

The so-called happy number, is to give a positive s, and then calculate it each bit on the square sum, get its next number, and then the next number to continue and select the square sum of each bit ... If it has been counted down, there have been no previous numbers and appeared 1, so congratulations, this is a happy number.

If there is a previous occurrence in the process, then it is not happy.

Ideas and Summary:

This question is the simplest kind of hash application, the academic point is the direct addressing table

The biggest number is 10^9, then each digit is the largest word is 9 9, squared and 9*9*9 = 729, so just open a 730+ array.

For the number that appears, mark the corresponding array with the element flag as True

 
 * * UVA  10591-happy number 
 * time:0.008s (UVA) 
 * 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/=;  
    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;  
            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 a unhappy number.\n", n);  
    }  
    return 0;  
}
Related Article

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.