UVA 10591 Happy Number
来源:互联网 发布:java前沿技术 2017 编辑:程序博客网 时间:2024/05/17 04:11
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.
题意:输入一个数字n。每次都把这个数字进行变换:把该数字变换成该数字每个位上数字的平方和。。如果变换过程中。出现重复的。这个数字就是Unhappy的。如果变换过程中出现1,这个数字就是Happy的。。
思路:完全可以用一个vis数组来标记遇到过的数字。。虽然题目中N最大可以为10^9。。但是经过第一次变换之后。。999999999的平方和是最大的。。为9 ^ 2 * 9 大概是700多 。。
所以vis只要开 1000就可以了。。。 然后进行模拟。。每次变换完进行判断。。注意:如果用这个方法。判断的时候 还要跟原来的n进行一次比较。
#include <stdio.h>#include <string.h>int t;int n;int vis[1000];int judge;int main(){ scanf("%d", &t); int tt = 1; while (t --) {memset(vis, 0 ,sizeof(vis));scanf("%d", &n);int sb = n;while (1){ int sum = 0; while (sb) {sum += (sb % 10) * (sb % 10);sb /= 10; } sb = sum; if (sum == 1) {judge = 0;break; } if (vis[sum] == 1 || sum == n) {judge = 1;break; } else {vis[sum] = 1; }}if (judge == 0) printf("Case #%d: %d is a Happy number.\n", tt ++, n);if (judge == 1) printf("Case #%d: %d is an Unhappy number.\n", tt ++, n); } 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 - 10591Happy Number
- uva 10591Happy Number
- UVa OJ 10591-Happy Number
- wince 关于Bin文件的解析
- Maya 2013 下载以及破解教程
- Objective-C之Foundation框架NSNumber、NSNull、NSValue、NSDate用法介绍
- Hibernate之session
- Eboot中nand flash控制器参数TACLS、TWRPH0和TWRPH1的确定(基于K9F2G08U0B)
- UVA 10591 Happy Number
- Windows多线程编程总结
- V4L2应用程序框架
- SimpleDateFormat使用详解
- 服务 线程传参object一个参数(object类来实现多参数)
- 单文档SDI,将视图的基类由CView变为CEditView的方法
- Android系统源码导入到eclipse
- HDU1013 考虑数论
- NSAutoReleasePool使用中drain和release的区别