1006 Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day. Input Specification: Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format: ID_number Sign_in_time Sign_out_time where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters. Output Specification: For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space. Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment. 题解:根据输入,找出第一个到机房开门的人和最后一个离开机房关门的人的ID_number code:
//非结构体版
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
string max = "00:00:00", min = "24:00:00", id_unlock, id_luck; // 预设定最早最晚时间和存储id的变量
while(n--){
string id, come_time, out_time;
string early_time, last_time;
while(cin>>id>>come_time>>out_time){ //判断最早最晚时间并记录id
if(come_time < min){
min = come_time;
id_unlock = id;
}
if(out_time > max){
max = out_time;
id_luck = id;
}
}
cout<<id_unlock<<" "<<id_luck;
return 0;
}
}
1008 Elevator The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop. For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled. Input Specification: Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. Output Specification: For each test case, print the total time on a single line. 题解:计算电梯升、降、停的总时间。升一层6s,降一层4s,停一次5s。 core:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, now_l; cin>>n;
int sum = 0, l = 0;
while(n--){
cin>>now_l;
sum += (now_l-l > 0 ? (5+(now_l-l)*6) : (5+(l-now_l)*4));
l = now_l; // 更新当前层数
}
cout<<sum<<endl;
return 0;
}