错误代码:
#include<iostream> using namespace std; int main() { int r,y,g; int n,k,t; int time=0; int temp,flag=0; cin>>r>>y>>g; cin>>n; for(int i=0;i<n;i++){ cin>>k>>t; if(flag==0){ if(k==0){ time+=t; } else if(k==1){ time+=t; } else if(k==2){ time+=t+r; } else if(k==3){ flag=1; temp=t; } } else if(flag==1){ flag=0; if(temp>=t){ time+=t; } else{ time+=y+r+g; } } } cout<<time<<endl; return 0; }大佬的代码: 【https://blog.csdn.net/Glasier/article/details/87855338】
#include <iostream> using namespace std; int main() { int r,y,g; int n,k,t; int sum=0; cin>>r>>y>>g>>n; while(n--) { cin>>k>>t; switch(k) { case 0: sum+=t; break; case 1: sum+=t; break; case 2: sum+=t+r; break; case 3: break; } } cout<<sum<<endl; return 0; }错误分析:对绿灯的情况考虑得太复杂。