Problem C | Happy Number |
Time Limit | 1 Second |
解题思路:WA了我一次,没看Sample直接敲了!!忽略了第一个要输入的Case数量
1 #include<iostream> 2 #include<cstdio> 3 #include<set> 4 using namespace std; 5 set<int>num; 6 bool Traverse(int cur){ 7 int temp = cur, sum = 0; 8 for(; temp != 0;){ 9 sum += (temp%10)*(temp%10); 10 temp /= 10; 11 } 12 if(sum == 1) return true; 13 else if(num.find(sum) == num.end()){ 14 num.insert(sum); 15 return Traverse(sum); 16 } 17 else return false; 18 } 19 20 int main(){ 21 #ifndef ONLINE_JUDGE 22 freopen("input.txt", "r", stdin); 23 #endif 24 int n, T; 25 cin>>T; 26 for(int i=1; i<=T; ++i){ 27 cin>>n; 28 num.clear(); 29 num.insert(n); 30 if(Traverse(n)) cout<<"Case #"<<i<<": "<<n<<" is a Happy number."<<endl; 31 else cout<<"Case #"<<i<<": "<<n<<" is an Unhappy number."<<endl; 32 } 33 return 0; 34 }