PAT 习题库 整理

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

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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容