題目連結 http://zerojudge.tw/ShowProblem?problemid=a005,
這題套用等差公式(下圖取自 Wikipedia 等差數列)
或等比公式(下圖取自 Wikipedia 等比數列)
先判斷數列是等差數列還是等比數列後,在求出第五項就可以了。
程式碼如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() { | |
short t; | |
cin >> t; | |
while(t-- > 0){ | |
int a, b, c, d, e, r; | |
cin >> a >> b >> c >> d; | |
r = b - a; | |
if( d == a + 3*r ) | |
{ | |
e = a + 4 * r; | |
} | |
else | |
{ | |
r = b / a; | |
r = r * r; | |
r = r * r; | |
e = a * r; | |
} | |
cout << a << " " << b << " " | |
<< c << " " << d << " " | |
<< e << endl; | |
} | |
return 0; | |
} |
沒有留言:
張貼留言