pat 1005 - 1008
来源:互联网 发布:手机迅雷极速优化版 编辑:程序博客网 时间:2024/06/06 09:59
1005. Spell It Right (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 (<= 10100).
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:12345Sample Output:
one five
#include<iostream>#include<string>#include<sstream>#include<stdio.h>using namespace std;int main(){freopen("E://input.txt", "r", stdin);string s;int sum = 0;cin>>s;string str[10] = {"zero","one","two","three","four","five","six","seven","eight","nine",};for(int i = 0; i < s.length(); i ++)sum = sum + s[i] - '0';ostringstream oss;oss<<sum;s = oss.str();for(int i = 0; i < s.length(); i ++){if(i == 0)cout<<str[s[i] - '0'];elsecout<<" "<<str[s[i] - '0'];}cout<<endl;return 0;}
代码二:vector
#include<iostream>#include<string>#include<sstream>#include<stdio.h>#include<vector>#include<algorithm>using namespace std;int main(){freopen("E://input.txt", "r", stdin);string s;int sum = 0;cin>>s;string str[10] = {"zero","one","two","three","four","five","six","seven","eight","nine",};for(int i = 0; i < s.length(); i ++)sum = sum + s[i] - '0';if(sum == 0)cout<<"zero";vector<string> vs;while(sum){vs.push_back(str[sum%10]);sum /= 10;}reverse(vs.begin(), vs.end());vector<string>::iterator it;for(it = vs.begin(); it != vs.end(); it ++){if(it == vs.begin())cout<<*it;elsecout<<" "<<*it;}cout<<endl;return 0;}
1006. Sign In and Sign Out (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:3CS301111 15:30:28 17:00:10SC3021234 08:00:00 11:25:25CS301133 21:45:00 21:58:40Sample Output:
SC3021234 CS301133
#include<iostream>#include<string>#include<stdio.h>using namespace std;int main(){freopen("E://input.txt", "r", stdin);int M;string ID_number, Sign_in_time, Sign_out_time;cin>>M;cin>>ID_number>>Sign_in_time>>Sign_out_time;string open_number = ID_number;string close_number = ID_number;string open_time = Sign_in_time;string close_time = Sign_out_time;while(-- M){cin>>ID_number>>Sign_in_time>>Sign_out_time;if(Sign_in_time < open_time){open_time = Sign_in_time;open_number = ID_number;}if(Sign_out_time > close_time){close_time = Sign_out_time;close_number = ID_number;}}cout<<open_number<<" "<<close_number<<endl;return 0;}
1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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 -21Sample Output:
10 1 4
#include<stdio.h>int buf[10002];int main(){freopen("E://input.txt", "r", stdin);int n;scanf("%d", &n);for(int i = 0; i < n; i ++)scanf("%d", &buf[i]);bool flag = true;//是否全部是负数for(int i = 0; i < n; i ++){if(buf[i] >= 0){flag = false;break;}}if(flag){printf("0 %d %d\n", buf[0], buf[n-1]);return 0;}int start = 0, end;int max = -1, sum = 0;//找到右坐标 for(int i = 0; i < n; i ++){sum += buf[i];if(sum > max){max = sum;end = i;}else if(sum < 0){sum = 0;start = i+1;}}max = -1; sum = 0;//找到左坐标 for(int i = end; i >= 0; i --){sum += buf[i];if(sum > max){max = sum;start = i;}else if(sum < 0)sum = 0; }printf("%d %d %d\n", max, buf[start], buf[end]);return 0;}
1008. Elevator (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 1Sample Output:
41
#include<stdio.h>int main(){freopen("E://input.txt", "r", stdin);int buf[100];int n;int count = 0;scanf("%d", &n);for(int i = 0; i < n; i ++)scanf("%d", &buf[i]);count = (buf[0] - 0)*6+5;for(int i = 1; i < n; i ++){if(buf[i] > buf[i-1])count = count + (buf[i]-buf[i-1])*6 + 5;elsecount = count + (buf[i-1]-buf[i])*4 + 5;}printf("%d\n", count);return 0;}
- pat 1005 - 1008
- PAT甲级1005-1008
- 浙大PAT考试1005~1008
- PAT 1008
- PAT 1008
- pat 1008
- PAT 1008
- PAT 1008
- PAT 1008
- PAT 1008
- pat 1008
- PAT 1008
- PAT 1008
- PAT 1008
- PAT 1008
- PAT 1008
- PAT 1008
- pat 1005
- servlet与jsp的生命周期
- Apache Thrift - 可伸缩的跨语言服务开发框架
- Java ClassLoader基础及加载不同依赖 Jar 中的公共类
- windows下编译Matconvnet的方法(CPU和GPU)
- PHP判断数组内容是否都为一样的值
- pat 1005 - 1008
- xindeDao
- 子串分离
- postfix 部署
- android APP集成支付宝支付
- java发送post请求代码
- bzoj4347 Nim z utrudnieniem 动态规划
- java加密解密ECDH笔记
- org.apache.subversion.javahl.ClientException: Previous operation has not finished