1. 程式人生 > >upc 9313 happy number

upc 9313 happy number

原題連結

題意:如果經過題目中的迴圈能到達一,就是快樂數,否則,不是 題解:太菜了,看到資料範圍是1e9;就慌的一批,其實,中規中矩的算,不過迴圈9次,應該是秒答的題

#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
    scanf("%d", &n);
    int m = n;
    while(m != 1 && m != 4){
        int p = m;
        int sum = 0;
        while(p != 0){
            sum += (p % 10) * (p % 10);
            p /= 10;
        }
        m = sum;
        //if(m == 4 || m == 16 || m == 37 || m == 58 || m == 89 || m == 145 || m == 42 || m == 20)break;


    }
    if(m == 1)printf("HAPPY\n");
     else printf("UNHAPPY\n");


return 0;
}