pat 1005 - 1008

来源:互联网 发布:手机迅雷极速优化版 编辑:程序博客网 时间:2024/06/06 09:59

1005. Spell It Right (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:
12345
Sample Output:

one five

题目是简单的字符串操作,题目意思是给一个数字字符串,计算各位之和sum,并将sum各位从高位到低位每位用英文表示,在计算sum都可以做到,然后就在于用英文如何去表示了,我用了两种方法,一种讲int类型转换为string,另外一种,用vector。代码分别如下:

代码一:int类型转换为string类型

#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)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:40
Sample 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)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 -21
Sample Output:
10 1 4

题目要求去求最大子段和,注意一个点,就是如果所有的数都是负数的话,就打印0 第一个数 最后一个数。如果不都为负数,则打印最大值和起始位置的值。也就是找到start和end。找end我们可以从左往右进行扫描一篇。sum += buf[i], max初始为-1,让max与当前的sum进行比较,如果max小于sum,则把sum赋值给max,并保存此时end=i.依次往后进行扫描,如果sum<0则把sum赋值为0;找start的位置可以同理。从右往左进行一次扫描就可以。

#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)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

这道题我想应该不用解释了,小学数学题、

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


0 0