第一題:模擬題,直接按照輸入順序模擬即可
#include<iostream>
using namespace std;
const int N=105;
int red,yellow,green;
//紅->綠->藍
int main(){
cin>>red>>yellow>>green;
int n,k,t,ans=0;
cin>>n;
while(n--){
cin>>k>>t;
switch(k){
case 0:ans+=t;
break;
case 1:ans+=t;
break;
case 2:ans+=t+red;
break;
case 3: break;
}
}
cout<<ans;
return 0;
}
第二題:處理技巧爲將所有的燈信號向紅燈一開始的時間進行對齊。
#include<iostream>
using namespace std;
typedef long long LL;
LL red,yellow,green;
//紅->綠->藍
//把所有的基準調至紅燈剛開始
LL costTime(LL color){
if(color<=red){
return red-color;
}
else if(color<=red+green){
return 0;
}
else{
return red+green+yellow-color+red;//黃燈剩餘以及紅燈
}
}
int main(){
cin>>red>>yellow>>green;
LL n,k,t,ans=0,cycle,time=0,color,wait;
cin>>n;
cycle=red+yellow+green;
while(n--){
cin>>k>>t;
switch(k){
case 0:time+=t;
break;
case 1:color=(red-t+time)%cycle;
time+=costTime(color);
break;
case 2:color=(cycle-t+time)%cycle;
time+=costTime(color);
break;
case 3: color=(red+green-t+time)%cycle;
time+=costTime(color);
break;
}
}
cout<<time;
return 0;
}