首页 > 专题系列 > Java解POJ > POJ 1658 Eva’s Problem [解题报告] Java
2013
11-10

POJ 1658 Eva’s Problem [解题报告] Java

Eva’s Problem

问题描述 :

Eva的家庭作业里有很多数列填空练习。填空练习的要求是:已知数列的前四项,填出第五项。因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习。

输入:

第一行是数列的数目t(0 <= t <= 20)。以下每行均包含四个整数,表示数列的前四项。约定数列的前五项均为不大于10^5的自然数,等比数列的比值也是自然数。

输出:

对输入的每个数列,输出它的前五项。

样例输入:

2
1 2 3 4
1 2 4 8

样例输出:

1 2 3 4 5
1 2 4 8 16

解题代码:

//* @author 洪晓鹏<[email protected]>
import java.util.Scanner;


public class Main {
 public static void main(String[] args)
 {
	Scanner in = new Scanner(System.in);
	int n = in.nextInt();
	for(int i = 0; i < n; i++)
	{
          int one = in.nextInt();
	  int two = in.nextInt();
	  int three = in.nextInt();
	  int four = in.nextInt();
	  int d1 = two - one;
	  int d2 = three - two;
	  int d3 = four - three;
	 if((d1 == d2)&&(d3 == d2))
	  {
	   int five = four + d1;
	   System.out.println(one+" "+two+" "+three+" "+four+" "+five);
	  }
	 else
	 {
	  int five = four * (two/one);
	  System.out.println(one+" "+two+" "+three+" "+four+" "+five);
	}
      }
   }
}

  1. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge

  2. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。