PAT 習(xí)題庫(kù) 整理

都是網(wǎng)上找的資料授艰,如有侵權(quán)兜看,請(qǐng)聯(lián)系我踏揣。

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.大神解答

主要掌握兩個(gè)分?jǐn)?shù)求和就行了靶溜,
a / b + c / d = (a * d + b * c) / (b * d)
每次約分一下梭依,求最大公約數(shù)(gcd)就好了稍算。我保證了分母總是正數(shù),分子任意……
還有建議用long long因?yàn)槌朔赡芎艽蟮摹?/p>

最終輸出是帶分?jǐn)?shù)役拴,可能整數(shù)部分是0糊探, 也可能分?jǐn)?shù)部分是0,要詳細(xì)判斷一下河闰。

代碼:

#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.大神解答

不難科平,但很麻煩。用中文讀一個(gè)數(shù)姜性。
數(shù)不超過(guò)9位瞪慧。
我是補(bǔ)足12位的,首位補(bǔ)0部念。
按照我們讀數(shù)的方法弃酌,4位一級(jí)。
(xxxx)(yyyy)(zzzz)
xxxx是億
yyyy是萬(wàn)
zzzz是個(gè)儡炼,不用讀妓湘。
話雖如此,細(xì)節(jié)很多乌询。
首先榜贴,讀4位數(shù),先寫(xiě)好妹田,要注意
零什么時(shí)候讀唬党,什么時(shí)候不讀鹃共,什么時(shí)候讀一個(gè)……
只有第一次遇到0(前面非0),才讀而且值讀一個(gè)0驶拱。

讀好4位數(shù)霜浴,我的算法是讀3個(gè)4位數(shù)就好了。
但是仍然要注意0的問(wèn)題蓝纲。
全是0的時(shí)候坷随,之前沒(méi)讀過(guò)東西就不讀,否則要讀一個(gè)0驻龟。
還有如果這個(gè)數(shù)< 1000,也就是不足4位缸匪,之前有東西的時(shí)候翁狐,還是要讀一個(gè)0。
每讀一個(gè)四位數(shù)加上一個(gè)單位凌蔬,億或者萬(wàn)露懒,全0又不加……
細(xì)節(jié)規(guī)則,慢慢處理砂心。

代碼:

#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.大神解答

簡(jiǎn)單題懈词,給定每個(gè)人的分?jǐn)?shù),姓名辩诞,id坎弯,再給一個(gè)分?jǐn)?shù)區(qū)間,按照分?jǐn)?shù)遞減的順序輸出這個(gè)分?jǐn)?shù)區(qū)間的所有人译暂。
題目告訴我們每個(gè)分?jǐn)?shù)最多只有一個(gè)人抠忘,于是我們根本不用排序,直接用一個(gè)數(shù)組記錄這個(gè)分?jǐn)?shù)的人的id就好了——類(lèi)似計(jì)數(shù)排序外永。
其實(shí)如果每個(gè)分?jǐn)?shù)的人不只一個(gè)也沒(méi)關(guān)系崎脉,可以用vector<int> grade[102]存每個(gè)分?jǐn)?shù)的所有人。
另外不知道是不是所有查詢的分?jǐn)?shù)區(qū)間都在[0..100]內(nèi)伯顶,所以我取了一個(gè)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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末囚灼,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子祭衩,更是在濱河造成了極大的恐慌灶体,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件汪厨,死亡現(xiàn)場(chǎng)離奇詭異赃春,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)劫乱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)织中,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)锥涕,“玉大人,你說(shuō)我怎么就攤上這事狭吼〔阕梗” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵刁笙,是天一觀的道長(zhǎng)破花。 經(jīng)常有香客問(wèn)我,道長(zhǎng)疲吸,這世上最難降的妖魔是什么座每? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮摘悴,結(jié)果婚禮上峭梳,老公的妹妹穿的比我還像新娘。我一直安慰自己蹂喻,他們只是感情好葱椭,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著口四,像睡著了一般孵运。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蔓彩,一...
    開(kāi)封第一講書(shū)人閱讀 52,328評(píng)論 1 310
  • 那天治笨,我揣著相機(jī)與錄音,去河邊找鬼粪小。 笑死大磺,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的探膊。 我是一名探鬼主播杠愧,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起赢笨,我...
    開(kāi)封第一講書(shū)人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎绳瘟,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體姿骏,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡糖声,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蘸泻。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡琉苇,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出悦施,到底是詐尸還是另有隱情并扇,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布抡诞,位于F島的核電站穷蛹,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏昼汗。R本人自食惡果不足惜肴熏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望顷窒。 院中可真熱鬧扮超,春花似錦、人聲如沸蹋肮。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)坯辩。三九已至,卻和暖如春崩侠,著一層夾襖步出監(jiān)牢的瞬間漆魔,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工却音, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留改抡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓系瓢,卻偏偏與公主長(zhǎng)得像阿纤,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子夷陋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容