Let the sum of the squares 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. For example, starting with 7 gives the sequence
7, 49(= 7 ∧ 2), 97(= 4 ∧ 2 + 9 ∧ 2), 130(= 9 ∧ 2 + 7 ∧ 2), 10(= 1 ∧ 2 + 3 ∧ 2), 1(= 1 ∧ 2),
so 7 is a happy number.
The first few happy numbers are 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, . . . The number of iterations i required for these to reach 1 are, respectively, 1, 6, 2, 3, 5, 4, 4, 3, 4, 5, 5, 3, . . .
A number that is not happy is called unhappy. Once it is known whether a number is happy (unhappy), then any number in the sequence s1, s2, s3, . . . will also be happy (unhappy). Unhappy numbers have eventually periodic sequences of si which do not reach 1 (e.g., 4, 16, 37, 58, 89, 145, 42, 20, 4, . . .).
Any permutation of the digits of a happy (unhappy) number must also be happy (unhappy). This follows from the fact that addition is commutative. Moreover, the product of a happy (unhappy) number by any power of ten is a happy (unhappy) number. Example: 58 is an unhappy number; then, so are 85, 580, 850, 508, 805, 5800, 5080, 5008, 8050, 8500, and so on.
Decide which numbers, in a given closed interval, are happy numbers.
Input
The input has n lines each of them corresponding to test case. Every line contains two positive integers between 1 and 99999 each; the first integer, L, is the low limit of the closed interval; the second one, H, is the high limit (L ≤ H).
Output
The output is composed of the happy numbers that lie in the interval [L, H], together with the number of iterations required for the corresponding sequences of squares to reach 1.
There must be a line for each happy number containing the happy number followed by a space and the number of iterations required for the sequence of squares to reach 1.
Print a blank line between two consecutive test cases.
Note: The definition of happy numbers is from MathWorld - http://mathworld.wolfram.com/
Sample Input
5 28
233 250
Sample Output
7 6
10 2
13 3
19 5
23 4
28 4
236 6
239 6
问题链接:UVA944 Happy Numbers
问题简述:(略)
问题分析: 某一个正整数n,对其各位数字分别平方再求和得到一个新数,重复同样的计算,最终和变成1,则称n为快乐数;如果出现循环变不成1则不是快乐数。
这个问题打表是必要的。
程序说明:
给出两个程序,后一个程序限制算快乐数是最多为16次,否则就不是快乐数,这个做法不够逻辑严密。
前一个程序使用set实现,逻辑比较严密。
然而,上述的两个程序虽然都AC,但是还是有改进的余地,需要进一步考虑如何去除重复计算。
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* UVA944 Happy Numbers */
#include <bits/stdc++.h>
using namespace std;
const int N = 99999;
int ans[N + 1];
int ishn(int n) {
set<int> s;
int step = 1;
while (n != 1) {
step++;
int sum = 0;
while (n) {
int d = n % 10;
sum += d * d;
n /= 10;
}
n = sum;
if (s.count(n)) break;
else s.insert(n);
}
return n == 1 ? step : 0;
}
void maketable()
{
ans[1] = 1;
for(int i=2; i<=N; i++)
ans[i] = ishn(i);
}
int main()
{
maketable();
int l, h, caseno = 0;
while(~scanf("%d%d", &l, &h)) {
if(caseno++ > 0)
putchar('\n');
for(int i=l; i<=h; i++)
if(ans[i])
printf("%d %d\n", i, ans[i]);
}
return 0;
}
AC的C++语言程序(用变量limit限制循环16次)如下:
/* UVA944 Happy Numbers */
#include <bits/stdc++.h>
using namespace std;
const int N = 99999;
int ans[N + 1];
void maketable()
{
ans[1] = 1;
for(int i=2; i<=N; i++) {
int step = 1, tmp = i, limit = 0;
ans[i] = 0;
for(;;) {
step++;
int sum = 0;
while(tmp) {
int d = tmp % 10;
sum += d * d;
tmp /= 10;
}
tmp = sum;
if(tmp == 1) {
ans[i] = step;
break;
}
if(++limit >= 16)
break;
}
}
}
int main()
{
maketable();
int l, h, caseno = 0;
while(~scanf("%d%d", &l, &h)) {
if(caseno++ > 0)
putchar('\n');
for(int i=l; i<=h; i++)
if(ans[i])
printf("%d %d\n", i, ans[i]);
}
return 0;
}