您的位置:首页 > 其它

uva 11375 Matches (递推)

2013-07-25 21:00 274 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2370

  一道递推的题。

  这道题的递推方程很容易可以想到,是枚举加上哪一个数字,把方法数累加起来。这道题主要是要注意前缀0的问题,可以通过枚举第一个数字不是一的所有情况,然后最后询问大于6的时候就加一。

代码如下(JAVA):

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
final BigInteger ONE = new BigInteger("1");
final int[] dg = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
final int N = 2222;
BigInteger pre[] = new BigInteger
;
for (int i = 1; i < N; i++) pre[i] = new BigInteger("0");
pre[0] = ONE;
for (int i = 0; i < 2100; i++) {
for (int j = 0; j < 10; j++) {
if (i == 0 && j == 0) continue;
pre[i + dg[j]] = pre[i + dg[j]].add(pre[i]);
}
if (i > 0) pre[i] = pre[i].add(pre[i - 1]);
}
for (int i = 0; i < 6; i++) {
pre[i] = pre[i].subtract(ONE);
}
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
System.out.println(pre
);
}
}
}


View Code

——written by Lyon
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: