2016-04-15 Problem Solving►UVa UVa 10591 - Happy Number Contents 1. Problem2. Solution3. Code Problem題目網址中文網址 Solution用 unordered_set 來判斷數字是否已出現過,如果已經出現過,代表接下來會繼續循環,不會產生 1 ,也就不是 happy number 了。 CodeUVa 10591UVa 10591 - Happy Number12345678910111213141516171819202122232425262728293031323334353637383940414243444546#include<cstdio>#include<unordered_set>using namespace std;int main(){ int Case; scanf("%d", &Case); unordered_set<int> s; for (int i = 1; i <= Case; i++) { s.clear(); int n, m; bool flag = false; scanf("%d", &n); m = n; while (!s.count(n))//判斷是否已經出現過 { if (n == 1) { flag = true; break; } s.insert(n); int sum = 0; //相加每位的平方 while (n) { int temp = n % 10; sum += temp*temp; n /= 10; } n = sum; } printf("Case #%d: %d is ", i, m); if (flag) puts("a Happy number."); else puts("an Unhappy number."); } return 0;} Newer UVa 10245 - The Closest Pair Problem Older UVa 10699 - Count the factors