UVA 10591 Happy Number

Source: Internet
Author: User

Original question:
The sum of the square of the digits of a positive integer s 0 is represented by S 1. In a similar-a-to, let the sum of the squares of the digits of S 1 is represented by S 2 and so on. If S i = 1 for some i≥1 and then the original integer S 0 are said to be Happy number. A number, which is isn't happy, is called unhappy number. For example 7 is a Happy number since 7→49→97→130→10→1 and 4 are 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 is given in the first line of the input.
Each test case consists the 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 was an unhappy number.
Here P stands is the case number (starting from 1). You should print the first message if 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: Happy number.


English:
Give you a number, if you keep calculating the sum of the squares of all the digits of this number to replace this number, if this number has ever appeared to be an unhappy number, if this number finally becomes 1 is a happy number.


#include<bits/stdc++.h>
using namespace std;
unordered_set<long> us;
long fun(long x)
{
    long tmp=0;
    while(x!=0)
    {
        long res=x%10;
        tmp+=res*res;
        x/=10;
    }
    return tmp;
}
int main()
{
    ios::sync_with_stdio(false);
    int t,k=1;
    cin>>t;
    while(t--)
    {
        us.clear();
        long x;
        cin>>x;
        long ans=x;
        us.insert(ans);
        while(true)
        {
            if(ans==1)
            {
                cout<<"Case #"<<k++<<": "<<x<<" is a Happy number."<<endl;
                break;
            }
            ans=fun(ans);
            if(us.find(ans)!=us.end())
            {
                cout<<"Case #"<<k++<<": "<<x<<" is an Unhappy number."<<endl;
                break;
            }
            us.insert(ans);
        }
    }
    return 0;
}


Answer:



A very simple problem, you can use the STL to record the number of occurrences


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.