CCF 认证 2018-12

第一题:模拟题,直接按照输入顺序模拟即可

#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;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章