UVa944 - Happy Numbers(动态规划)

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 rst 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 rst 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 de nition 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

import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Arrays;
import java.util.HashSet;

public class Main implements Runnable
{
	private static final boolean DEBUG = false;
	private static final int N = 100000;
	private PrintWriter cout;
	private Scanner cin;
	private int l, h;
	private int cas = 1;
	private int[] f;
	private HashSet<Integer> hs = new HashSet<Integer>();
	
	private void init()
	{
		try {
			if (DEBUG) {
				cin = new Scanner(new BufferedInputStream(new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new Scanner(new BufferedInputStream(System.in));
			}
			
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			f = new int[N];
			Arrays.fill(f, -1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private boolean input()
	{
		if (!cin.hasNextInt()) return false;
		
		l = cin.nextInt();
		h = cin.nextInt();
		
		return true;
	}

	private int dp(int x)
	{
		if (f[x] != -1) return f[x];
		
		if (x == 1) return 1;
		
		int sum = 0;
		int tmp = x;
	
		while (tmp != 0) {
			int rem = tmp % 10;
			sum += rem * rem;
			tmp /= 10;
		}
		
		if (hs.contains(sum)) return f[x] = 0;
		else {
			hs.add(sum);
			tmp = dp(sum);
			if (tmp == 0) return f[x] = 0;
			else return f[x] = 1 + tmp;
		}
	}
	
	private void solve()
	{
		if (cas++ > 1) {
			cout.println();
		}
		
		for (int i = l; i <= h; i++) {
			hs.clear();
			int tmp = dp(i);
			if (tmp > 0) {
				cout.println(i + " " + tmp);
			}
		}

		cout.flush();
	}
	
	public void run()
	{
		init();
		
		while (input()) {
			solve();
		}
	}
	
	public static void main(String[] args)
	{
		new Thread(new Main()).start();
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值