UVA - 10591 Happy Number

Happy Number

UVA - 10591

Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let the sum of the squares of the digits of S1 be represented by S2 and so on. If Si = 1 for some i ≥ 1, then the original integer S0 is said to be Happy number. A number, which is not happy, is called Unhappy number. For example 7 is a Happy number since 7 → 49 → 97 → 130 → 10 → 1 and 4 is an Unhappy number since 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4.

Input

The input consists of several test cases, the number of which you are given in the first line of the input. Each test case consists of one line containing a single positive integer N smaller than 109.

Output

For each test case, you must print one of the following messages:

Case #p: N is a Happy number.

Case #p: N is an Unhappy number.

Here p stands for the case number (starting from 1).

You should print the first message if the number N is a happy number. Otherwise, print the second line.

Sample Input

3

7

4

13

Sample Output

Case #1: 7 is a Happy number.

Case #2: 4 is an Unhappy number.

Case #3: 13 is a Happy number.

这道题的递归或者说是循环我感觉挺好的,说实话第一遍我都没看懂这个题,就是给你一个数,如果这个数每个数字的平方和是1就是happy的,手动试了下,小于10的只有1满足,所以我只要把它平方和到一位数就行了,要不然这个循环走不出来,然后继续循环递归就行了

#include <stdio.h>
int f(int b)
{
    int s=0;
    while(b)
    {
        int t=b%10;
        s+=t*t;
        b/=10;
    }
    if(s<10)return s;
    else return f(s);
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int i=1; i<=T; i++)
    {
        int x;
        scanf("%d",&x);
        printf("Case #%d: %d is a",i,x);
        if(f(x)==1)
            printf(" Happy number.\n");
        else printf("n Unhappy number.\n");

    }
    return 0;
}
时间: 2024-08-26 06:34:42

UVA - 10591 Happy Number的相关文章

UVA 10909 - Lucky Number(树状数组)

UVA 10909 - Lucky Number 题目链接 题意:问一个数字能否由两个lucky num构造出来,lucky num根据题目中的定义 思路:利用树状数组找前k大的方法可以构造出lucky num的序列,然后每次查找n,就从n / 2开始往下查找即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 2000001

uva 11651 - Krypton Number System(矩阵快速幂)

题目链接:uva 11651 - Krypton Number System 题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和. 解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算. #include <cstdio> #

UVA 11651 - Krypton Number System(DP+矩阵快速幂)

UVA 11651 - Krypton Number System 题目链接 题意:给一个进制base,一个分数score求该进制下,有多少数满足一下条件: 1.没有连续数字 2.没有前导零 3.分数为score,分数的计算方式为相邻数字的平方差的和 思路:先从dp入手,dp[i][j]表示组成i,最后一个数字为j的种数,然后进行状态转移,推出前面一步能构成的状态,也就是到dp[(b - 1) * (b - 1)][x]. 然后可以发现后面的状态,都可以由前面这些状态统一转移出来,这样就可以利用

UVa 11946 - Code Number

题目:两个小朋友写信让父母传递,为了不让父母看信里的内容,对信里面的内容加密, 请对加密后的信解密. 分析:字符串,数论.只有10个字母被选择称为0~9的映射,直接转化即可. 说明:╮(╯▽╰)╭UVa又打不开了╮(╯▽╰)╭. #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> char buf[81]; char map[11] = "OIZEASGT

UVa 11734 - Big Number of Teams will Solve This

题目:一个ACM的判题的小程序,两组字符全相同,为正确,比标准多输出空格,为格式错误,其他为错误. 分析:字符串.从前向后扫描,如果两字符不同,若A串当前字符不是空格,则错误: 若是空格,则一定不会是正确,滤过空格,看剩余部分,如果剩下字符相同则格式错误: 否则,一定错误: 说明:注意结束位置的空格.想起几年前开发自己OJ的日子了. #include <iostream> #include <cstdlib> #include <string> #include <

UVA - 11882 Biggest Number(dfs+bfs+强剪枝)

题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的点的个数,若当前数字的长度加上个数仍小于目前最优答案的长度,则剪去:若长度相等,则将所有还能到达的数字按从大到小排序后连到当前数字上,如果还比目前最优解小,则减去.找出所有还能到达的点的过程用BFS实现. 1 #pragma comment(linker, "/STACK:1024000000,10

uva live 7638 Number of Connected Components (并查集)

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5660 题意: 每两个点如果他们的gcd大于1的话就可以连一条边,问在这些数里面有多少个联通块. 题解: 我们可以用筛法倍数.然后用并查集将他们连通起来,2 3 6 本来2 和3的gcd 为1,但是他们可以通过6使得连通. 还有就是要注意 15 25 35 这个数据.

UVA - 10624 Super Number

题目大意: 给定两个数 n 和 m ,如果长度为 m 的数满足对于每个 i (n <= i <= m),数字的前 i 位都能被 i 整除,那么这个数就是超级数,求出字典序最小的符合要求的超级数. 解题思路:直接暴力就行,如果每次进行整除判断的时候,对当前数每位都进行取余运算,那么将会超时,因此每 18 位进行一次取余(long long的数据范围为:-9223372036854775808..9223372036854775807,这样在 1S 左右就可以AC了. #include <c

UVA 11882 Biggest Number 深搜 剪枝

如图,给一个图,每个格子只能走一次,起点终点任意,求按路径顺序将格子里的数字拼在一起,最大的数字. 很容易想到用暴力求解的方法,但最多会有 30 个格子,O(2^30) 肯定超时了. 考虑剪枝,设当前已经找到一个 ans,若当前路径的最大长度小于 ans 的长度,则不可能比 ans 大,剪掉, 若等于,则考虑当前路径已经形成的数字(假设 n 位),若小于 ans 的前 n 位,则剪掉. 代码: #include <iostream> #include <cstring> #incl