uva 10591 Happy Number(判重)
来源:互联网 发布:杭州mac专柜地址 编辑:程序博客网 时间:2024/05/21 04:18
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
解题思路:对应任何一个数,经过变换后的值最大为9 * 9 * 9, 所以开一个1000的数组就可以将所有情况标记(注意开始输入的值没有必要标记,也开不了那么大的数组),用一个循环对数进行变换,碰到已经出现过的数值就可以跳出,跳出后判断1是否遍历即可(注意输出时a和an).
#include <stdio.h>#include <string.h>const int N = 10005;int count(int cur) { int sum = 0, t; while (cur){ t = cur % 10;sum = sum + t * t;cur = cur / 10; } return sum;}int main() { int cas = 1, n, m, cur; int v[N]; scanf("%d", &m); while (m--) {scanf("%d", &n);memset(v, 0, sizeof(v));cur = n;while (1) { cur = count(cur); if (v[cur])break; v[cur] = 1;}printf("Case #%d: %d is %s number.\n", cas++, n, v[1]?"a Happy":"an Unhappy"); } return 0;}
- 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
- OpenStack安装过程中的一些问题记录
- 第七章 复用类
- 桌面环境与桌面搜索Desktop Search tools
- 第八章 多态
- tcp connection setup的实现(二)
- uva 10591 Happy Number(判重)
- Vim 练级攻略
- 字节序转换问题
- 第九章 接口
- 库和框架的理解(lib和framework)
- android中发送邮件
- 僵尸进程的产生与处理
- tcp connection setup的实现(三)
- 第十章 内部类