都是網(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;
}