uva 10591 Happy Number(判重)
来源:互联网 发布:京都小众景点.知乎 编辑:程序博客网 时间:2024/06/14 05:05
description:
Problem C
Happy Number
Time Limit
1 Second
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 109.
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 should print the first message if the number N is 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: Mohammed Shamsul Alam
题目大意:根据所给的一个数,每次都取它的每个位上的数的平方和作为下一个数,如果可以推出1的话就是happy number,如果出现相同的就是 unhappy number。
解题思路:因为数字不超过10^9,所以这个数所形成的下一个数最大是 9 * 81 = 729;
而前面如果给出的数大于这个数,说明以后也不会出现这个数,所以可以不用记录进去。开个729的数组就可以完成判重了。
#include<stdio.h>#include<string.h>const int N = 800;int visit[N];int n;int main(){scanf("%d", &n);for(int i = 1; i <= n; i++){int num;memset(visit, 0, sizeof(visit));scanf("%d", &num);if(num == 1){printf("Case #%d: %d is a Happy number.\n", i, num);continue;}if(num <= 729)visit[num] = 1;int x = num;while(1) {int dight = 0; while(num){ dight += (num % 10) * (num % 10);num = num / 10;}if(dight == 1){printf("Case #%d: %d is a Happy number.\n", i, x);break;}if(visit[dight]){printf("Case #%d: %d is an Unhappy number.\n", i, x);break;}else visit[dight] = 1;num = dight;}}}
- uva 10591 Happy Number(判重)
- uva 10591 Happy Number(判重)
- UVa 10591 - Happy Number
- uva 10591 - Happy Number
- uva 10591 - Happy Number
- UVa 10591 - Happy Number
- uva-10591 - Happy Number
- UVa 10591 - Happy Number
- UVA 10591 Happy Number
- UVA 10591 - Happy Number
- uva 10591- Happy Number
- UVA 10591 Happy Number
- UVa 10591 - Happy Number
- UVA 10591 - Happy Number
- UVA - 10591 Happy Number
- UVA - 10591 Happy Number
- UVA - 10591 Happy Number
- UVA - 10591Happy Number
- rhel6.5 报unrecognized service错误
- 大数据起点
- android SQLite
- Android 入门 - 系统启动简介
- "Big Data"- Reporting Over Hadoop using Hive-Intellicus 5.2
- uva 10591 Happy Number(判重)
- 深入理解SELinux SEAndroid(最后部分)
- C# Panel实现多窗口切换
- 科大讯飞携手阿里云共筑移动互联生态圈
- RHEL6通过安装光盘制作本地yum源的方法
- I2C总线信号时序总结
- 在VS2010中应用SIFT(C)源码——from [FreedomShe]
- svn分支和合并实战(图解)
- SQL的EOF