// Run time: 0.000
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char str[10];
int test(int len);
int main()
{
    int i, j;
    int n;
    int tmp;
    int len;
    int num;
    int cnt = 0;
    scanf("%d", &n);
    while (n--)
    {
        scanf("%s", str);
        tmp = atoi(str);
        len = strlen(str);
        num = test(len);
        if (num)
            printf("Case #%d: %d is a Happy number.\n", ++cnt, tmp);
        else
            printf("Case #%d: %d is an Unhappy number.\n", ++cnt, tmp);
    }
    return 0;
}
int test(int len)
{
    int i;
    int sum = 0;
    for ( i = 0; i < len; i++)
        sum += (str[i]-'0')*(str[i]-'0');
    if (sum == 1)
        return 1;
    if (sum >= 10)
    {
        snprintf(str, sizeof(str), "%d\0", sum);
        len = strlen(str);
        return test(len);
    }
    return 0;
}
 
沒有留言:
張貼留言