uva 10591 - Happy Number
Happy Number
Time Limit
1 Second
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
Output for Sample Input
3 7 4 13
Case #1: 7 is a Happy number. Case #2: 4 is an Unhappy number. Case #3: 13 is a Happy number.
Problemsetter: Mohammed Shamsul Alam
International Islamic University Chittagong
Special thanks to Muhammad Abul Hasan
99999999->81*8
표기수 그룹 판정 순환 최대 9*9*8
#include<stdio.h>
#include<string.h>
int sort(int x)
{int y=0;
while (x>0)
{y=y+(x%10)*(x%10);
x=x/10;
}
return y;
}
int main()
{int n,m,t,i,visit[1000];
scanf("%d",&t);
for (i=1;i<=t;i++)
{scanf("%d",&n);
memset(visit,0,sizeof(visit));
m=n;
while (n)
{m=sort(m);
if (m==1) {printf("Case #%d: %d is a Happy number.
",i,n);break;}
if (visit[m]) {printf("Case #%d: %d is an Unhappy number.
",i,n);break;}
visit[m]=1;
}
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
application ---- enter and save the file codeInput file name and content - input_content.html Receive content and save file and content - input_content01.jsp...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.