代码如下:
#include <iostream> using namespace std; int main() { //灯顺序为 红、绿、黄 循环 int red,yellow,green;//红、黄、绿灯的额定时间 int n;//经过道路/红绿灯总数 long long SumTime=0;//小学上学所用时间 cin>>red>>yellow>>green; cin>>n; for(int i=0;i<n;i++){ int k,t; cin>>k>>t; if(k==0){ SumTime=SumTime+t; } else if(k==1){//初始为红灯 int time=SumTime%(red+yellow+green); if(time<=t){SumTime=SumTime+t-time;}//此轮回没有走出红灯 else if(t+green<=time && time<t+green+yellow){SumTime=SumTime+(t+green+yellow-time)+red;}//到达时已变为绿灯变为黄灯 else if(t+green+yellow<=time && time<t+green+yellow+red){SumTime=SumTime+(t+green+yellow+red-time);}//此轮回出红灯后再次进入红灯 } else if(k==2){//初始为黄灯 int time=SumTime%(red+yellow+green); if(time<t){//没有走出黄灯 SumTime=SumTime+t-time+red; } else if(t<=time &&time<t+red ){//到达时为红灯 SumTime=SumTime+(t+red-time); } else if(t+red+green<=time && time<t+red+green+yellow){SumTime=SumTime+(t+green+yellow+red-time)+red;}//此轮回中出了黄灯再次进入黄灯 } else if(k==3){//初始为绿灯 int time=SumTime%(red+yellow+green); if(t<time &&time<t+yellow){//到达时为黄灯 SumTime=SumTime+(t+yellow-time)+red; } else if(t+yellow<time && time<t+yellow+red){//到达时为红灯 SumTime=SumTime+(t+yellow+red-time); } //此轮回第1、2次进入绿灯对SumTime没有影响 } } cout<<SumTime<<endl; return 0; }
题目如下:
编辑