WUSTOJ 1323: Repeat Number(Java)规律统计

108 篇文章 0 订阅
80 篇文章 18 订阅

题目链接:1323: Repeat Number

Description

Definition: a+b = c, if all the digits of c are same ( c is more than ten),then we call a and b are Repeat Number. My question is How many Repeat Numbers in [x,y].

定义:a + b = c,如果 c 的每一位数字相同(c > 10),那么我们就把 a 和 b 称为重复数,问题是在区间 [x, y] 中有多少对这样的 a,b 使得 c 满足上述条件。

Input

There are several test cases.
Each test cases contains two integers x, y(1<=x<=y<=1,000,000) described above.
Proceed to the end of file.

多组测试数据。
每组数据输入两个整数 x,y (1 <= x <= y <= 1000000)。

Output

For each test output the number of couple of Repeat Number in one line.

每组数据,输出有多少对满足条件的 a,b。

Sample Input

1 8
1 10
1 12
1 18
1 25
10 100
20 1000
30 10000
40 100000
50 1000000

Sample Output

3
5
7
15
29
213
2868
31461
320892
3220161

HINT

If a equals b, we can call a, b are Repeat Numbers too, and a is the Repeat Numbers for itself.

分析?

下面二维数组表示 c 的值,且 c = a + b,数组 array[a][b] = c,下面只列出一部分。

1234567891011121314151617
111
211
311
411
51122
622
722
822
922
1022
1122
12
13
14
15
1633
17

给定的 x,y 就相当于在数组中取出一块正方形区域,统计其中的 c 的个数。比如给定 x = 10,y = 12,那么取出的区域如下所示:

101112
1022
1122
12

因此结果为2。

由于数据较大,我们肯定不能按照二维数组逐位统计,那样很可能超时。

观察符合条件的 c ,会发现 c 的取值并不多。如数组 fc[6][9] 所示:

012345678
0112233445566778899
1111222333444555666777888999
2111122223333444455556666777788889999
3111112222233333444445555566666777778888899999
4111111222222333333444444555555666666777777888888999999
51111111

y 取最大值 1000000 时,a + b 最大为 2000000 ,符合条件的 c 最大为 1111111 。上面的数组 fc[6][9] 由init()方法得到。

代码

/**
 * Time 306ms
 * @author wowpH
 * @version 2.2
 * @date 2019年6月24日下午5:05:25
 * Environment:	Windows 10
 * IDE Version:	Eclipse 2019-3
 * JDK Version:	JDK1.8.0_112
 */

import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	private int[][] fc = new int[6][9];// 保存46个满足条件的c的取值

	public Main() {
		init();// 初始化
		Scanner sc = new Scanner(new InputStreamReader(System.in));
		while (sc.hasNext()) {
			int x = sc.nextInt();
			int y = sc.nextInt();
			System.out.println(count(x, y));
		}
		sc.close();
	}

	private void init() {// 初始化数组fc
		int d = 1;// 公差
		for (int i = 0; i < 6; ++i) {
			d = d * 10 + 1;// 位数增加
			fc[i][0] = d;
			for (int j = 1; j < 9; ++j) {
				fc[i][j] = fc[i][j - 1] + d;
			}
		}
	}

	private int count(int minI, int maxI) {// 统计满足条件的取值个数
		int ans = 0;
		int minC = minI * 2;
		int maxC = maxI * 2;
		for (int i = 0; i < 6; ++i) {
			for (int j = 0; j < 9; ++j) {
				// 满足条件就统计取值个数
				if (fc[i][j] >= minC && fc[i][j] <= maxC) {
					int a = fc[i][j] / 2;// 一维下标
					int b = fc[i][j] - a;// 二维下标
					ans = ans + Math.min(a - minI, maxI - b) + 1;
				}
			}
		}
		return ans;
	}

	public static void main(String[] args) {
		new Main();
	}
}

版权声明

  1. 转载、参考、引用必须在首页添加如下文字:
    [WUSTOJ 1323: Repeat Number(Java)规律统计——wowpH](https://blog.csdn.net/pfdvnah/article/details/93490424)
  2. 代码原创,公开引用不能删除首行注释(作者,版本号,时间等信息);
  3. 如果有疑问欢迎评论区留言,尽量解答;
  4. 如果有错误,还望大侠评论区指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值