1005 Spell It Right (20)(20 分)提问
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (<= 10^100^).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five
作者: CHEN, Yue
单位: PAT联盟
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB
#include<cstring> #include<algorithm> #include<iostream> #include<cstdio> #include<cstring> using namespace std; const char* num[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"}; int main(void) { char s[1000]; int ans[100]; scanf("%s",s); int len = strlen(s),sum=0; for(int i=0;i<len;i++) sum+=s[i]-48; if(sum==0) { cout << "zero"; return 0; } //cout << sum; int length=0; while(sum) { ans[++length]=sum%10; sum/=10; } for(int i=length;i>=1;i--) { printf("%s",num[ans[i]]); if(i!=1) cout <<" "; } return 0; }
1006 Sign In and Sign Out (25)(25 分)提问
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.
Sample Input:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output:
SC3021234 CS301133
作者: CHEN, Yue
单位: PAT联盟
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB
#include<iostream> #include<cstdio> #include<algorithm> #include<set> #include<vector> #include<cstring> using namespace std; int M; typedef struct Point{ string name; int hour; int minute; int second; }P; vector<P>box; set<string>S; vector<string>locked_people; bool cmp(P x,P y) { if(x.hour!=y.hour) return x.hour<y.hour; else if(x.minute!=y.minute) return x.minute<y.minute; else return x.second < y.second; } int main(void) { cin >> M; for(int i=1;i<=M;i++) { P temp1,temp2; string name1,name2; int hour1,hour2; int minute1,minute2; int second1,second2; cin >> name1; scanf("%d:%d:%d %d:%d:%d",&hour1,&minute1,&second1,&hour2,&minute2,&second2); name2=name1; temp1.name=name1; temp1.hour=hour1; temp1.minute=minute1; temp1.second=second1; temp2.name=name2; temp2.hour=hour2; temp2.minute=minute2; temp2.second=second2; box.push_back(temp1); box.push_back(temp2); } sort(box.begin(),box.end(),cmp); for(int i=0;i<(int)box.size();i++) { //cout << box[i].name <<endl; string name = box[i].name; if(S.empty()) { locked_people.push_back(name); S.insert(name); } else if(S.find(name)==S.end()) { S.insert(name); } else { set<string>::iterator it = S.find(name); S.erase(it); if(S.empty()) locked_people.push_back(name); } } cout << box[0].name << ‘ ‘ << box[box.size()-1].name; return 0; }
1007 Maximum Subsequence Sum (25)(25 分)
Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, ..., N~j~ } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
作者: CHEN, Yue
单位: PAT联盟
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; const int maxn = 10010; typedef long long LL; LL a[maxn],k,Max,First,End; int main(void) { //freopen("D:\\input.txt","r",stdin); int negative = true; cin >> k; for(int i=1;i<=k;i++) { cin >> a[i]; if(a[i]>=0) negative = false; } if(negative==true) { cout << 0 << ‘ ‘ << a[1] << ‘ ‘ << a[k]; return 0; } int first,end; LL sum=0; first = 1; Max = -0X3fffffff; int i=1; while(i<=k) { end = i; sum += a[i]; if(sum>Max) { Max = sum; First = first; End = end; } if(sum<0) { sum = 0 ; first = end = i+1; } i++; } cout << Max << " " << a[First] << " " << a[End]; return 0; }
1008 Elevator (20)(20 分)
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.
Sample Input:
3 2 3 1
Sample Output:
41
作者: CHEN, Yue
单位: PAT联盟
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> using namespace std; int n; int main(void) { vector<int> box; cin >> n; for(int i=0;i<n;i++) { int x; cin >> x; box.push_back(x); } int pre=0,sum=0; for(int i=0;i<(int)box.size();i++) { int now = box[i]; //if(now!=pre) sum+=5; sum+=5; if(now>pre) sum+=(now-pre)*6; else sum+=(pre-now)*4; pre = now ; } cout << sum; return 0; }
原文地址:https://www.cnblogs.com/zuimeiyujianni/p/9388229.html