此題為出自zerojidge 基礎題005
以下是我寫出來的答案
#include <iostream>
using namespace std;
int main()
{
//先確認會有幾個數列
int n;
cin >> n;
//依次輸入每個數列的四個數字
for (int i = 0; i < n; i++)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
// 判斷數列類型
if (b - a == c - b && c - b == d - c)
{
// 等差數列
int diff = b - a;
cout << a << " " << b << " " << c << " " << d << " " << d + diff << endl;
}
else if (b / a == c / b && c / b == d / c)
{
// 等比數列
int ratio = b / a;
cout << a << " " << b << " " << c << " " << d << " " << d * ratio << endl;
}
}
return 0;
}
全站熱搜
留言列表