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=;
while(b)
{
int t=b%;
s+=t*t;
b/=;
}
if(s<)return s;
else return f(s);
}
int main()
{
int T;
scanf("%d",&T);
for(int i=; i<=T; i++)
{
int x;
scanf("%d",&x);
printf("Case #%d: %d is a",i,x);
if(f(x)==)
printf(" Happy number.\n");
else printf("n Unhappy number.\n"); }
return ;
}

UVA - 10591 Happy Number的更多相关文章

  1. UVA 11882 Biggest Number(搜索+剪枝)

    You have a maze with obstacles and non-zero digits in it: You can start from any square, walk in the ...

  2. UVa 10624 - Super Number

    题目大意 给定两个数n和m,如果长度为m的数满足对于每个i(n<=i<=m),数字的前i位都能被i整除,那么这个数就是超级数,求出字典序最小的符合要求的超级数. 分析 直接暴力搜索 #in ...

  3. UVA 11734 Big Number of Teams will Solve This

    大水题,不解释啦! #include<cstdio> #include<cstring> #define maxn 50 using namespace std; char s ...

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

    题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...

  5. UVA 10909 Lucky Number(树状数组+二分+YY)

    此题测试时预处理等了很久,结果470ms过了...... 题意:开始不怎么懂,结果发现是这个: 波兰裔美国数学家斯塔尼斯拉夫·乌拉姆(Stanislaw Ulam)在20世纪50年代中期开发出了另一种 ...

  6. UVa 11651 Krypton Number System DP + 矩阵快速幂

    题意: 有一个\(base(2 \leq base \leq 6)\)进制系统,这里面的数都是整数,不含前导0,相邻两个数字不相同. 而且每个数字有一个得分\(score(1 \leq score \ ...

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

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  8. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  9. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

随机推荐

  1. [转]Java8 lambda表达式及新特新

    分享自:Vincent package info.liuwenjun.test; import org.junit.Test; import java.util.*; import java.util ...

  2. 《高性能JavaScript》 实用指南

    By XFE-堪玉 阅读<高性能javascript>后,对其内容的一个整理和精简 加载与执行 将script标签放在body结尾标签上面 控制script标签数量(每一次script解析 ...

  3. 浅析 var that = this;

    在阅读别人的代码时,发现别人写的代码中有这么一句:var that = this;,这代表什么意思呢?经过一番查阅,才明白是这么回事. 在JavaScript中,this代表的是当前对象. var t ...

  4. springMVC中jackson的使用(包含JsonFormat 时间格式)

    前台使用ajax,后台 springMVC Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行JSON和Ja ...

  5. JavaScript面试系列:JavaScript设计模式之桥接模式和懒加载

    我写的程序员面试系列文章 Java面试系列-webapp文件夹和WebContent文件夹的区别? 程序员面试系列:Spring MVC能响应HTTP请求的原因? Java程序员面试系列-什么是Jav ...

  6. myeclipse报错MA

    以下问题萌新问了我很多次了,无奈写个随笔.之后问的我都在这个随笔里补充. 断电/自动关机导致的问题: Could not open the editor: the file does not exis ...

  7. xcode或者mac自带颜色器选择rgb格式

    解决方法

  8. vector的基本用法

    #include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...

  9. 在idea下创建maven

    之前一直用eclipse,现在要用idea写一个安装过程玩玩 一:New Project 二:选择maven,在project SDK上选择你安装的jdk,默认安装在c:/Program Files ...

  10. caffe的pad的报错

    CHECK((!conv_param.has_stride() && conv_param.has_stride_h() && conv_param.has_strid ...