UVA - 10591 Happy Number

来源:互联网 发布:轻兵器杂志 知乎 编辑:程序博客网 时间:2024/05/16 18:11

题目大意:求一个数字是不是个happy number,happy number的定义是如果这个数的每位的平方加起来为1就是了,unhappy则是每位的平方加起来的数是以前出现过的

解体思路:暴力破解

#include<cstdio>int main() {int test;scanf("%d", &test);for(int i = 0; i < test; i++) {long long number;printf("Case #%d: ",i+1);scanf("%lld", &number);long long num = number;long long mark[1000];long long temp = 0;int count = 0;int mark_b = 0;while(1) {int t ;long long temp = 0;for(;;) {t = number % 10;t = t * t;temp = temp + t;number = number / 10;if(number == 0)break;}int j;if(temp == 1) {printf("%lld is a Happy number.\n",num);break;}for( j = 0; j < count; j++)if(mark[j] == temp) {printf("%lld is an Unhappy number.\n",num);mark_b = 1;break;}if(mark_b)break;if(j == count)mark[count++] = temp;number = temp;}}return 0;}


0 0
原创粉丝点击