Problem Statement | |||||||||||||
Given a sequence of integers, s[0],s[1],..,s[n] we can define its difference sequence as the sequence s[1]-s[0], s[2]-s[1], ..., s[n]-s[n-1]. We can similarly generate its second difference sequence as the difference sequence of its difference sequence, and continue generating deeper difference sequences until we get one with length 1. Here is an example: seq: 5 -4 12 23 1stdifseq -9 16 11 2nddifseq 25 -5 3rddifseq -30Given a sequence of integers, one useful way to predict the next value in the sequence is by choosing the one that will make the bottom difference of the enlarged sequence be 0. In the example, we would predict -1 as the next value in the sequence -- this would extend the first difference sequence to end with -1 - 23 = -24, the second to end with -35, and the third to end with -30. This would make the single value in the fourth sequence be 0. Given int[]seq, return the predicted value. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | seq will contain between 1 and 10 elements, inclusive. | ||||||||||||
- | Each element of seq will be between -1000 and 1000, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
我的Java解答,,,做得太慢了,只有79分
public class DifDif {
private static int t[][] = new int[11][11];
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int t[] = { -1000, 1000, -1000, 1000, -1000, 1000, -1000, 1000, -1000,
1000 };
int x = predict(t);
System.out.println(x);
}
public static int predict(int[] seq) {
cal();
int len = seq.length;
int sum = 0;
int array[] = new int[len];
int store[] = new int[10];
if (len == 1)
return seq[0];
for (int i = len; i > 0; i--) {
sum += t[len][i] * seq[len - i];
}
sum = -sum;
return sum;
}
private static void cal() {
t[0][0] = 1;
t[1][0] = 1;
t[1][1] = 1;
for (int i = 2; i <= 10; i++) {
t[i][0] = 1;
t[i][i] = 1;
for (int j = 1; j < i; j++) {
t[i][j] = t[i - 1][j - 1] + t[i - 1][j];
}
}
for (int i = 0; i <= 10; i++) {
for (int j = 0; j <= 10; j++) {
int x = j % 2 == 0 ? 1 : -1;
t[i][j] = x * t[i][j];
System.out.print(t[i][j] + " ");
}
System.out.println();
}
}
}