TopCoder 250 points Tournaments 153-TCCC 07 Round 1C DIV 1 78.15/250 31.26%

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            -30
Given 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

 
Class:DifDif
Method:predict
Parameters:int[]
Returns:int
Method signature:int predict(int[] seq)
(be sure your method is public)
 
 

Constraints

-seq will contain between 1 and 10 elements, inclusive.
-Each element of seq will be between -1000 and 1000, inclusive.

Examples

0) 
 
{5,-4, 12, 23}
Returns: -1
This is the example given above.
1) 
 
{100}
Returns: 100
The first difference sequence of 100,100 is a sequence consisting of one 0.
2) 
 
{1,4,9,16,25,36}
Returns: 49
3) 
 
{-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000}
Returns: 1023000

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();
		}

	}
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值