美文网首页
PAT 习题库 整理

PAT 习题库 整理

作者: JaiUnChat | 来源:发表于2016-08-04 00:44 被阅读168次

都是网上找的资料,如有侵权,请联系我。

No.1001 Rational Sum(20)

1.题目描述

Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.

2.输入描述

Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers "a1/b1 a2/b2 ..." where all the numerators and denominators are in the range of "long int". If there is a negative number, then the sign must appear in front of the numerator.

3.输出描述

For each test case, output the sum in the simplest form "integer numerator/denominator" where "integer" is the integer part of the sum, "numerator" < "denominator", and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

4.输入例子

5
2/5 4/15 1/30 -2/60 8/3

5.输出例子

3 1/3

6.大神解答

主要掌握两个分数求和就行了,
a / b + c / d = (a * d + b * c) / (b * d)
每次约分一下,求最大公约数(gcd)就好了。我保证了分母总是正数,分子任意……
还有建议用long long因为乘法可能很大的。

最终输出是带分数,可能整数部分是0, 也可能分数部分是0,要详细判断一下。

代码:

#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

char s[111];

long long gcd(long long x,long long y) {
    return y?gcd(y, x % y):x;
}

int main() {
long long a = 0, b = 1; //a / b
int n;
    for (scanf("%d",&n);n;--n) {
        scanf("%s",s);
        char *t = strstr(s,"/");
        if (t) {
            *t = ' ';
        }
        long long c, d;
        sscanf(s,"%lld%lld",&c,&d);
        // a / b + c / d
        long long aa = a * d + b * c;
        long long bb = b * d;
        long long g = gcd((aa < 0)?(-aa):aa, bb);
        a = aa / g;
        b = bb / g;
    }
    long long x = a / b, y = a % b;
    if (y == 0) {
        printf("%lld\n",x);
    }
    else {
        if (x) {
            printf("%lld ",x);
        }
        printf("%lld/%lld\n",y,b);
    }
    return 0;
}

No.1002 Read Number in Chinese (25)

1.题目描述

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu". Note: zero ("ling") must be handled correctly according to the Chinese tradition. For example, 100800 is "yi Shi Wan ling ba Bai".

2.输入描述

Each input file contains one test case, which gives an integer with no more than 9 digits.

3.输出描述

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

4.输入例子

-123456789

5.输出例子

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

6.大神解答

不难,但很麻烦。用中文读一个数。
数不超过9位。
我是补足12位的,首位补0。
按照我们读数的方法,4位一级。
(xxxx)(yyyy)(zzzz)
xxxx是亿
yyyy是万
zzzz是个,不用读。
话虽如此,细节很多。
首先,读4位数,先写好,要注意
零什么时候读,什么时候不读,什么时候读一个……
只有第一次遇到0(前面非0),才读而且值读一个0。

读好4位数,我的算法是读3个4位数就好了。
但是仍然要注意0的问题。
全是0的时候,之前没读过东西就不读,否则要读一个0。
还有如果这个数< 1000,也就是不足4位,之前有东西的时候,还是要读一个0。
每读一个四位数加上一个单位,亿或者万,全0又不加……
细节规则,慢慢处理。

代码:

#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
const string number[] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
const string weight[] = {"Qian","Bai","Shi",""};
const string we[] = {"Yi","Wan",""};

string readnumber(const char *s) {
    int i;
    for (i = 0; (i < 4) && (s[i] == '0'); ++i)
        ;
    if (i >= 4) {
        return number[0];
    }
    string answer = number[s[i] - '0'];
    if (i < 3) {
        answer += " " + weight[i];
    }
    bool mark = false;
    for (++i; i < 4; ++i) {
        if (s[i] == '0') {
            mark = true;
        }
        else {
            if (mark) {
                answer += " " + number[0];
                mark = false;
            }
            answer += " " + number[s[i] - '0'];
            if (i < 3) {
                answer += " " + weight[i];
            }
        }
        
    }
    return answer;
}

int main() {
    char s[100];
    bool sign = true;
    scanf("%s",s);
    string num;
    if (s[0] == '-') {
        sign = false;
        num = s + 1;
    }
    else if (s[0] == '+') {
        num = s + 1;
     }
    else {
        num = s;
    }
    while (num.size() < 12) {
        num = "0" + num;
    }
    bool mark = false;
    string answer = "";
    for (int i = 0, j = 0; i < 3; ++i) {
        int x = (num[j] - '0') * 1000 + (num[j + 1] - '0') * 100 + (num[j + 2] - '0') * 10 + num[j + 3] - '0';
        if (x == 0) {
            if (!answer.empty()) {
                mark = true;
            }
        }
        else {
            if ((!answer.empty()) && (mark || (x < 1000))) {
                answer += " " + number[0];
                mark = false;
            }
            if (!answer.empty()) {
                answer += " ";
            }
            answer += readnumber(num.c_str() + j);
            if (i < 2) {
                answer += " " + we[i];
            }
        }
        j += 4;
    }
    if (answer.empty()) {
        puts(number[0].c_str());
    }
    else {
        if (!sign) {
            printf("Fu ");
        }
        puts(answer.c_str());
    }
    return 0;
}

No.1001 Rational Sum(20)

1.题目描述

Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval.

2.输入描述

Each input file contains one test case. Each case is given in the following format:
N
name[1] ID[1] grade[1]
name[2] ID[2] grade[2]
... ...
name[N] ID[N] grade[N]
grade1 grade2

where name[i] and ID[i] are strings of no more than 10 characters with no space, grade[i] is an integer in [0, 100], grade1 and grade2 are the boundaries of the grade's interval. It is guaranteed that all the grades are distinct.

3.输出描述

For each test case you should output the student records of which the grades are in the given interval [grade1, grade2] and are in non-increasing order. Each student record occupies a line with the student's name and ID, separated by one space. If there is no student's grade in that interval, output "NONE" instead.

4.输入例子

4
Tom CS000001 59
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
60 100

5.输出例子

Mike CS991301
Mary EE990830
Joe Math990112

6.大神解答

简单题,给定每个人的分数,姓名,id,再给一个分数区间,按照分数递减的顺序输出这个分数区间的所有人。
题目告诉我们每个分数最多只有一个人,于是我们根本不用排序,直接用一个数组记录这个分数的人的id就好了——类似计数排序。
其实如果每个分数的人不只一个也没关系,可以用vector<int> grade[102]存每个分数的所有人。
另外不知道是不是所有查询的分数区间都在[0..100]内,所以我取了一个from = max(0, from), to = min(100, to);
代码:
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;

char name[102][12],id[102][12];
int grade[102];

int main() {
int n;
    memset(grade,0xff,sizeof(grade));
    scanf("%d",&n);
    for (int i = 0; i < n; ++i) {
        int x;
        scanf("%s%s%d",name[i],id[i],&x);
        grade[x] = i;
    }
    int from,to;
    scanf("%d%d",&from,&to);
    bool have = false;
    from = max(0, from);
    to = min(100, to);
    for (int i = to; i >= from; --i) {
        if (grade[i] >= 0) {
            have = true;
            printf("%s %s\n",name[grade[i]],id[grade[i]]);
        }
    }
    if (!have) {
        puts("NONE");
    }
    return 0;
}

相关文章

  • PAT 习题库 整理

    都是网上找的资料,如有侵权,请联系我。 No.1001 Rational Sum(20) 1.题目描述 Given...

  • 各大OJ各种类型算法题汇总-持续更新中

    S0. PAT真题分类 柳神博客整理(附链接):PAT甲级真题分类 S1. 图论 网友整理(无链接版)图论500题...

  • 题库整理(二)

    1.mach-0文件的结构都有哪些,iOS程序main()函数之前和之后分别都做了些什么? Mach-O文件格式是...

  • 题库整理(一)

    1.atomic和nonatomic的区别?atomic一定是线程安全的吗?atomic如何实现atomic? a...

  • IOS整理题库

    IOS中导航设计模式有几种 分别是什么? 答:平铺导航( UITabbarController ) 标签导航( U...

  • Android 题库整理

    一.图片 1、图片库对比 2、LRUCache原理 3、图片加载原理 4、自己去实现图片库,怎么做? 5、Glid...

  • 选题库是分类,标题库是整理

    五词故事:选题库、标题库、故事库、金句库、思维模型库。 选题库是分类,标题库是整理,故事库是素材,金句库是加分,思...

  • woop

    W,今天整理标题库选品库 O,整理之后,效率更高 O,拖延,小我 P,完成给予奖励

  • C++排序函数sort()

    最近在刷PAT题库时遇到这样一个题宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,...

  • 习茶之八

    用15分钟快速平复情绪,获得重启的力量 Pat.1通过习茶获得平复与重启的能力 习茶,是一件虚实结合、圆融共通的事...

网友评论

      本文标题:PAT 习题库 整理

      本文链接:https://www.haomeiwen.com/subject/zwlcsttx.html