问题描述我引用一下别人的博客:https://blog.csdn.net/Q_M_X_D_D_/article/details/86617268
下面是我个人的思路:
1. 每个路口的灯的时间和状态只与消耗的总时间有关
2. 总时间取模后的值即能算出当前到达的路口的灯的状态以及时间
我自己用IDE写了之后反复检查都发现不出问题,在CCF练习的网站上面提交的话是解答错误。
下面贴我的代码:
import java.util.Scanner;
public class _12_2_B {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String[] lightTimes = sc.nextLine().split(" ");
int r = Integer.parseInt(lightTimes[0]);
int y = Integer.parseInt(lightTimes[1]);
int g = Integer.parseInt(lightTimes[2]);
int dataCounts = Integer.parseInt(sc.nextLine());
int allTimes = 0;
for(int i = 0;i<dataCounts;i++){
String[] tmp = sc.nextLine().split(" ");
int type = Integer.parseInt(tmp[0]);
int typeTime = Integer.parseInt(tmp[1]);
int moveTime = allTimes%(r+y+g); //计算一个周期后回到该位置后再移动的时间
if(type==0){ //路口
allTimes+=typeTime;
}else if(type == 1){ //红灯
if(moveTime<typeTime){ //比数字表上的数字小
allTimes+=typeTime-moveTime;
}else if(moveTime>=typeTime+g){ //比数字表上的数字大,并且大过了数字表+绿灯时间直接来到下一周期黄灯
int needTime = y+r-(moveTime-r-g);
allTimes+=needTime;
}
}else if(type == 2){ //黄灯
if(moveTime<typeTime+r){ //小于红灯+当前黄灯数字表的数
allTimes+=typeTime+r-moveTime;
}else if(moveTime>=typeTime+r+g){ //大于红灯+黄灯数字表上的时间+下一周期的绿灯时间,来到下一周期黄灯
allTimes+=r+y-(moveTime-typeTime-r-g);
}
}else if(type == 3){ //绿灯
if(moveTime>=typeTime && moveTime<r+y+typeTime){ //大于等于绿灯数字表的数字,并且小于同周期到红灯结束需要的时间
allTimes+=r+y-(moveTime-typeTime);
}
}
}
System.out.println(allTimes); //输出结果
}
}
希望有大神能帮帮我找出问题