高中数学?(Java)acm.sdut

Problem Description
高中数学大家都学过数列,其中一个重要的概念就是数列的通项,可以代表数列中每一项的一个表达式。
 今天我们的问题就跟通项有关系,说,给你一个数列的通项和数列中的前几项,希望你能求出它的第n项。
 通项表达式如下:
 F(1) = 0;
 F(2) = 1;
 F(n) = 4*F(n-1)-5*F(n-2);
Input
输入数据第一行是一个正整数T,T<100。接下来T行,每行一个整数n, 2<n<50。
Output
输出有T行,对于输入中每行中的n按照通项计算出F(n)。
Example Input
4
3
4
5
6
Example Output
4
11
24
41


注意:把给数组的赋值放在构造函数中(水题)


public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		for (int i = 0; i < t; i++) {
			int n=sc.nextInt();
			Series se=new Series(n);
			int result=se.result(n);
			System.out.println(result);
		}

		sc.close();
	}
}

class Series{
	int a[]=new int [55];
	int n;
	public Series(int n){
	    a[1]=0;
	    a[2]=1;
	    for(int i=3;i<=50;i++){
	    	a[i]=4*a[i-1]-5*a[i-2];
	    }
	    this.n=n;
	}
	public int result(int n){
	    a[1]=0;
	    a[2]=1;
	    for(int i=3;i<=50;i++){
	    	a[i]=4*a[i-1]-5*a[i-2];
	    }
		return a[n];
	}
}

构造函数:http://www.cnblogs.com/lovely/archive/2011/12/31/2309017.html(以后参考)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值