PAT甲级第二次真题练习

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

时间: 2024-07-29 11:10:04

PAT甲级第二次真题练习的相关文章

2019冬季PAT甲级第二题

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 typedef struct{ 5 int add,data,nex; 6 }Node; 7 Node node[100007],ans[100007],ans2[100007]; 8 map<int,int>mp; 9 int main(){ 10 //ios::sync_with_stdio(false); 11 /

PAT乙级(Basic Level)真题训练

写在前面:PAT冬季赛马上就要开始了!??这一次先报一个乙级冲鸭!我感Jio乙级里面还是有蛮多水题的,也有些题虽然看上去是水题,但是真正用代码实现起来的话会卡你那么一下,比如第5题数素数真的神打脸. 天上不会掉馅饼的,好好学习,努力奋斗才能梦想成真. 1. D进制的A + B(20) 题目描述: 输入两个非负10进制整数A和B(<= 230-1),输出A + B的D(1 <D <= 10)进制数. 输入描述: 输入在一行中依次给出3个整数A,B和D. 输出描述: 输出A + B的D进制数

PAT乙级(Basic Level)真题,福尔摩斯的约会

题目描述 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”.大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母'D',代表星期四:第2对相同的字符是'E',那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9.以及大写字母A到N表示):后面两字符串第1对相

1054. 求平均值 (20)-PAT乙级真题

今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org/course/zju-93001#/info :然后就是跟着<算法之美>也要同步看完. 然后就在PAT上随便做一道题,这是第一次通过AC,发现了两个比较好的博客主页:http://www.liuchuo.net/  和  https://www.joyhwong.com/   都总结了刷题的过程

PAT 乙级真题 1002.数字分类

PAT 乙级真题 1002.数字分类 题目描述 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...: A3 = 被5除后余2的数字的个数: A4 = 被5除后余3的数字的平均数,精确到小数点后1位: A5 = 被5除后余4的数字中最大数字. 输入格式 每个输入包含1个测试用例.每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的

PAT 乙级真题 1006.1016.部分A+B

PAT 乙级真题 1006.1016.部分A+B 题目描述 正整数A的"DA(为1位整数)部分"定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的"6部分"PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编写程序计算PA + PB. 输入格式 输入在一行中依次给出A.DA.B.DB,中间以空格分隔,其中0 < A, B < 1010. 输出格式 在一行中输出PA + PB的值. 输入样例 386276

PAT 乙级真题 1008.锤子剪刀布

PAT 乙级真题 1008.锤子剪刀布 题目描述 大家应该都会玩"锤子剪刀布"的游戏: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式 输入第1行给出正整数N(<=105),即双方交锋的次数.随后N行,每行给出一次交锋的信息,即甲.乙双方同时给出的的手势.C代表"锤子".J代表"剪刀".B代 表"布",第1个字母代表甲方,第2个代表乙方,中间有1个空格. 输出格式 输出

PAT 乙级真题 1012.D进制的A+B

PAT 乙级真题 1012.D进制的A+B 题目描述 输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数. 输入格式 输入在一行中依次给出3个整数A.B和D. 输出格式 输出A+B的D进制数. 输入样例 123 456 8 输出样例 1103 题目思路 #include<bits/stdc++.h> #define ll long long int using namespace std; int main() { ll a,b

PAT 乙级真题 1013.组个最小数

PAT 乙级真题 1013.组个最小数 题目描述 给定数字0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意0不能做首位).例如: 给定两个0,两个1,三个5,一个8,我们得到的最小的数就是10015558. 现给定数字,请编写程序输出能够组成的最小的数. 输入格式 每个输入包含1个测试用例.每个测试用例在一行中给出10个非负整数,顺序表示我们拥有数字0.数字1.--数字9的个数.整数间用一个空 格分隔.10个数字的总个数不超过50,且至少拥有1个