poj 1658 Eva's Problem

来源:互联网 发布:淘宝能删除评价吗 编辑:程序博客网 时间:2024/04/23 22:55
Eva的家庭作业里有很多数列填空练习。填空练习的要求是:已知数列的前四项,填出第五项。因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习。

Input

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

Output

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

Sample Input

21 2 3 41 2 4 8

Sample Output

1 2 3 4 51 2 4 8 16

【分析】显然只能是两种数列中的一种,显然只需要一次简单的分析就行,但是这里有一个简单的讨论,就是我们只讨论是否是等差数列,如果不是那么就直接用等比数列就行了。

【代码】

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int main(){    int t;    scanf("%d",&t);    while(t--)    {        int a[4];        for(int i=0;i<4;i++)            scanf("%d",&a[i]);        for(int i=0;i<4;i++)            printf("%d ",a[i]);        if(2*a[1]-a[0]-a[2]==0&&2*a[2]-a[1]-a[3]==0)            printf("%d\n",2*a[3]-a[2]);        else            printf("%d\n",a[3]*a[3]/a[2]);    }    return 0;}