Uvs-10591-Happy Number (STL)

Source: Internet
Author: User

Uvs-10591-Happy Number (STL)

UV-10591

Happy Number
Time Limit:3000 MS Memory Limit:Unknown 64bit IO Format:% Lld & % llu

 

Submit Status

Description

 

Problem C

Happy Number

Time Limit

1 Second

 

Let the sum of the square of the digits of a positive integerS0Be representedS1. In a similar way, let the sum of the squares of the digitsS1Be representedS2And so on. IfSi= 1 for someI3 1, then the original integerS0Is 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 integerNSmaller109.

 

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.

 

HerePStands for the case number (starting from 1). You shoshould print the first message if the numberNIs a happy number. Otherwise, print the second line.

 

Sample Input

Output for Sample Input

3

7

4

13

Case #1: 7 is a Happy number.

Case #2: 4 is an Unhappy number.

Case #3: 13 is a Happy number.

 

Problemsetter: Hammed Shamsul Alam

International Islamic University Chittagong

Special thanks to Muhammad Abul Hasan

Source

Root: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim): Mathematics: Cycle-Finding-Standard
Root: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim): Chapter 5. Mathematics: Sequences and Number Systems: Number Systems
Root: aoapc I: Beginning Algorithm Contests (Rujia Liu): Volume 3. Brute Force: Hashing/Sets
Root: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim): Chapter 5. Mathematics: Cycle-Finding
Root: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim): Mathematics: Cycle-Finding: Standard

Submit Status


 

 

 

Idea: A simple question. Just simulate it. Here we use set to record the occurrence of a certain Number. If it appears, we can determine that there is a ring, that is, it is not the Happy Number, at the beginning, I didn't want to clear the condition for loop end. The result was TLE once.

 

AC code:

 

#include 
 
  #include 
  
   #include #include 
   
    using namespace std;int main() {int N, cas = 1;scanf("%d", &N);while(N--) {int a, flag = 0;scanf("%d", &a);set
    
      num;num.insert(a);int t = a; while(1) {if(t == 1) {flag = 1;break;}int tmp = t;t = 0;while(tmp) {int b = tmp % 10;t += b * b;tmp /= 10;}if(num.count(t)) break;else num.insert(t);}if(flag) {printf("Case #%d: %d is a Happy number.\n", cas++, a);}else printf("Case #%d: %d is an Unhappy number.\n", cas++, a);}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.