照題目的規則走,此處寫法較偷懶,因為題目說循環不超過100次,所以就看看 100 次內是否出現 1
#include <iostream>
using namespace std;
int main()
{
int num, tmp1, tmp2;
int i;
while(cin >> num){
tmp1 = num;
for(i = 0; i<100; i++){
tmp2 = 0;
while(tmp1){
tmp2 += (tmp1%10)*(tmp1%10);
tmp1/=10;
}
if(tmp2 == 1)
break;
tmp1 = tmp2;
}
if(i == 100)
cout << num << " is an unhappy number" << endl;
else
cout << num << " is a happy number" << endl;
}
return 0;
}
全站熱搜
留言列表