做这个是真的不快乐,开个sta数组来验证是否重复即是否进入循坏
那个图灵班看来是指望不上了,太懒了。😴
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| int sum (int n) { int a=0; while(n>0){ a+=(n%10) *(n%10); n /=10; } return a; }
bool isHappy(int n){ int sta[10001]; memset(sta,0,10001); long long int a=0; if (n == 1) return 1; while (n>3){ if (sum (n)==1) return 1; else { if(sta[sum(n)]==1) return 0; n=sum(n); sta[n]=1; } } return 0; }
|