试题编号: | 201812-1 |
试题名称: | 小明上学 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
问题描述: | 题目背景 小明是汉东省政法大学附属中学的一名学生,他每天都要骑自行车往返于家和学校。为了能尽可能充足地睡眠,他希望能够预计自己上学所需要的时间。他上学需要经过数段道路,相邻两段道路之间设有至多一盏红绿灯。 问题描述 一次上学的路上,小明记录下了经过每段路的时间,和各个红绿灯在小明到达路口时的颜色和倒计时秒数。希望你帮忙计算此次小明上学所用的时间。 输入格式 输入的第一行包含空格分隔的三个正整数 r、y、g,表示红绿灯的设置。这三个数均不超过 106。 输出格式 输出一个数字,表示此次小明上学所用的时间。 样例输入 30 3 30 样例输出 70 样例说明 小明先经过第一段道路,用时 10 秒,然后等待 5 秒的红灯,再经过第二段道路,用时 11 秒,然后等待 2 秒的黄灯和 30 秒的红灯,再经过第三段、第四段道路,分别用时6、3秒,然后通过绿灯,再经过最后一段道路,用时 3 秒。共计 10 + 5 + 11 + 2 + 30 + 6 + 3 + 3=70 秒。 评测用例规模与约定 测试点 1, 2 中不存在任何信号灯。 |
java:
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int y = sc.nextInt();
int g = sc.nextInt();
int n = sc.nextInt();
int sum = 0;
int k = 0, t = 0;
for(int i = 0; i < n; i++) {
k = sc.nextInt();
t = sc.nextInt();
if(k == 0) {
sum += t;
}else if(k == 1) {
sum += t;
}else if(k == 2) {
sum += (t + r);
}else {
sum += 0;
}
}
System.out.println(sum);
}
}
c++:
#include <iostream>
using namespace std;
int main()
{
int r,y,g;cin>>r>>y>>g;
int n;cin>>n;
int sum=0;
int k,t;
for(int i=0;i<n;i++){
cin>>k>>t;
if(k==0)
sum+=t;
else if(k==1){
sum+=t;
}
else if(k==2){
sum+=t+r;
}
}
cout<<sum;
return 0;
}