2022.11.02 AM 09:00 by CBJ
來源 : https://zerojudge.tw/ShowProblem?problemid=a005 出題者 : POJ 標籤 : 運算式、條件判斷 難易度 : 0.1
解題想法 : 判斷輸入為等差or等比,並輸出第5項(題目有提到不是等差就是等比,所以可以直接用if...else來做,不用等差和等比都算一次(耗時間))
//C language
//solution link(含註解): https://github.com/CBJ0519/CBJsProgramDiary.com/blob/main/zj/a005.c
#include<stdio.h>
int main(){
int q[6];
int ans,n;
scanf("%d",&n);
while(n--){
for(int i=0;i<4;i++) scanf("%d",&q[i]);
if((q[1]-q[0]) == (q[2]-q[1]) && (q[2]-q[1]) == (q[3]-q[2])) ans=q[3]+(q[3]-q[2]);
else ans=q[3]*(q[3]/q[2]);
printf("%d %d %d %d %d\n",q[0],q[1],q[2],q[3],ans);
}
return 0;
}

//C++ language
//solution link(含註解): https://github.com/CBJ0519/CBJsProgramDiary.com/blob/main/zj/a005.cpp
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
while(n--){
int a,b,c,d;
int ans;
cin>>a>>b>>c>>d;
if((b-a)==(c-b) and (c-b)==(d-c)) ans=d+(d-c);
else ans=d*(d/c);
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<ans<<"\n";
}
return 0;
}

## Python language
## solution link(含註解): https://github.com/CBJ0519/CBJsProgramDiary.com/blob/main/zj/a005.py
n=int(input())
for i in range(n):
a,b,c,d=map(int,input().split())
if (b-a) == (c-b) == (d-c): print(a,b,c,d,d+(d-c))
else: print(a,b,c,d,d*(d//c))

發佈留言