24h購物| | PChome| 登入
2012-07-09 20:05:41| 人氣453| 回應0 | 上一篇 | 下一篇

[UVA][Java] 10198 - Counting

推薦 0 收藏 0 轉貼0 訂閱站台


 Problem E: Counting 

The Problem

Gustavo knows how to count, but he is now learning how write numbers. As he is a very good student, he already learned 1, 2, 3 and 4. But he didn't realize yet that 4 is different than 1, so he thinks that 4 is another way to write 1. Besides that, he is having fun with a little game he created himself: he make numbers (with those four digits) and sum their values. For instance:

132 = 1 + 3 + 2 = 6
112314 = 1 + 1 + 2 + 3 + 1 + 1 = 9 (remember that Gustavo thinks that 4 = 1)
After making a lot of numbers in this way, Gustavo now wants to know how much numbers he can create such that their sum is a number n. For instance, for n = 2 he noticed that he can make 5 numbers: 11, 14, 41, 44 and 2 (he knows how to count them up, but he doesn't know how to write five). However, he can't figure it out for n greater than 2. So, he asked you to help him.

The Input

Input will consist on an arbitrary number of sets. Each set will consist on an integer n such that 1 <= n <= 1000. You must read until you reach the end of file.

The Output

For each number read, you must output another number (on a line alone) stating how much numbers Gustavo can make such that the sum of their digits is equal to the given number.

Sample Input

2
3

Sample Output

5
13

dp[i] = dp[i-1]*2 + dp[i-2] + dp[i-3];
dp[0] = 1;

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

public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger[] dp = new BigInteger[1001];
dp[0] = new BigInteger("1");
for(int i = 1; i <= 1000; i++) {
dp[i] = new BigInteger("0");
if(i-1 >= 0) {
dp[i] = dp[i].add(dp[i-1]);
dp[i] = dp[i].add(dp[i-1]);
}
if(i-2 >= 0)
dp[i] = dp[i].add(dp[i-2]);
if(i-3 >= 0)
dp[i] = dp[i].add(dp[i-3]);
}
while(cin.hasNextInt()) {
int n = cin.nextInt();
System.out.println(dp[n].toString());
}
}
}

台長: Morris
人氣(453) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Java] 10494 - If We Were a Child Again
此分類上一篇:[UVA] 1226 - Numerical surprises

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文