标签:space algo string end c代码 name cto bsp namespace
AC代码
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<string> #include<queue> #include<map> #include<stack> using namespace std; int a[505][505]; int main() { //错误点1:黄灯后还要等红灯,忘记加上 //错误点2:int会爆 //错误点3:红绿灯边界条件判断 ,用(sum-time)%红绿灯周期 , long long r,y,g,time,temp; cin>>r>>y>>g; int n,type; cin>>n; long long sum=0; //目前已经花费的时间 for(int i=0;i<n;i++) { cin>>type>>time; //time 倒计时 if(type == 0) sum = sum + time; else if(type == 1) // 红灯 { temp = sum%(r+y+g); if(temp < time) { sum = sum + time - temp; } else if(temp<time+g) { continue; } else if(temp < time+g+y) //此时黄灯 { sum = sum + g+y-temp+r+time; //错误点:黄灯后还要等红灯,忘记加上 } else { sum = sum + g+y+r-temp+time; } } else if(type == 2) // 黄灯 { temp = sum%(r+y+g); //sum-time 此时表示红灯刚亮的时候, temp表示此时的灯已过去的秒数 if(temp < time) { sum = sum + time - temp + r; } else if(temp<time+r) //红灯 { sum = sum + r-temp+time; } else if(temp<time+r+g) { continue; } else //黄灯 { sum = sum + g+y+r-temp+r+time; } } else if(type == 3) // 绿灯 { temp = sum%(r+y+g); //sum-time 此时表示黄灯刚亮的时候, temp表示此时的灯已过去的秒数 if(temp < time) { continue; } if(temp<time+y) //黄灯 { sum = sum + y-temp+r+time; } else if(temp<time+r+y) //红灯 { sum = sum + r+y-temp+time; } else { continue; } } //cout<<temp<<endl; } cout<<sum; return 0; }
错误代码
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<string> #include<queue> #include<map> #include<stack> using namespace std; int a[505][505]; int main() { //错误点1:黄灯后还要等红灯,忘记加上 //错误点2:int会爆 long long r,y,g,time,temp; cin>>r>>y>>g; int n,type; cin>>n; long long sum=0; //目前已经花费的时间 for(int i=0;i<n;i++) { cin>>type>>time; //time 倒计时 if(type == 0) sum = sum + time; else if(type == 1) // 红灯 { temp = (sum-time)%(r+y+g); if(temp>g && temp<=g+y) //此时黄灯 { sum = sum + g+y-temp+r; //错误点:黄灯后还要等红灯,忘记加上 } else if(temp>g+y && temp<=g+y+r) //红灯 { sum = sum + g+y+r-temp; } } else if(type == 2) // 黄灯 { temp = (sum-time)%(r+y+g); //sum-time 此时表示红灯刚亮的时候, temp表示此时的灯已过去的秒数 if(temp>0 && temp<=r) //红灯 { sum = sum + r-temp; } else if(temp>g+r && temp<=g+y+r) //黄灯 { sum = sum + g+y+r-temp+r; } } else if(type == 3) // 绿灯 { temp = (sum-time)%(r+y+g); //sum-time 此时表示黄灯刚亮的时候, temp表示此时的灯已过去的秒数 if(temp>0 && temp<=y) //黄灯 { sum = sum + y-temp+r; } else if(temp>y && temp<=r+y) //红灯 { sum = sum + r+y-temp; } } //cout<<temp<<endl; } cout<<sum; return 0; }
标签:space algo string end c代码 name cto bsp namespace
原文地址:https://www.cnblogs.com/fzuhyj/p/10525065.html