• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LyonLys
愿意在角落唱沙哑的歌 再大声也都是给你 请用心听 不要说话 Contact me via E-mail: lyon.lys@gmail.com
博客园    首页    新随笔    联系   管理    订阅  订阅

uva 11375 Matches (递推)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2370

  一道递推的题。

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

代码如下(JAVA):

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6         final BigInteger ONE = new BigInteger("1");
 7         final int[] dg = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
 8         final int N = 2222;
 9         BigInteger pre[] = new BigInteger[N];
10         for (int i = 1; i < N; i++) pre[i] = new BigInteger("0");
11         pre[0] = ONE;
12         for (int i = 0; i < 2100; i++) {
13             for (int j = 0; j < 10; j++) {
14                 if (i == 0 && j == 0) continue;
15                 pre[i + dg[j]] = pre[i + dg[j]].add(pre[i]);
16             }
17             if (i > 0) pre[i] = pre[i].add(pre[i - 1]);
18         }
19         for (int i = 0; i < 6; i++) {
20             pre[i] = pre[i].subtract(ONE);
21         }
22         Scanner in = new Scanner(System.in);
23         while (in.hasNext()) {
24             int n = in.nextInt();
25             System.out.println(pre[n]);
26         }
27     }
28 }
View Code

 

 

——written by Lyon

 

posted @ 2013-07-25 21:00  LyonLys  阅读(360)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3