PAT甲級1001,1002,1005,1006解題報告
1001.
1001 A+B Format (20 分)
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
Sample Output:
-999,991
題目大意:輸入兩個數,輸出兩個數的和。本來是挺簡單的。。但是要求要標準的格式化輸出,就是銀行賬單一樣每三位數字夾一個逗號。
解題思路:簡單的把字串處理一下就好了。先算出位數對3的餘數,然後輸出那幾位加個逗號,剩下的除了最後一組全部輸出多一個逗號即可。實現程式碼如下。
#include<iostream> #include<string> using namespace std; int main() { long long int a, b; cin >> a >> b; long long int c = a + b; string res = to_string(c); int len; if (c >= 0) { len = res.length(); } else { len = res.length() - 1; } if (len < 4) { cout << res << endl; } else { if (len % 3 == 0) { if (c > 0) { for (int i = 0; i < len / 3; i++) { if (i != len / 3 - 1) cout << res.substr(i * 3, 3) + ","; else cout << res.substr(i * 3, 3) << endl; } } else { cout << "-"; for (int i = 0; i < len / 3; i++) { if (i!=len/3-1) { cout << res.substr(i * 3+1, 3) + ","; } else cout << res.substr(i * 3+1, 3) << endl; } } } else { if (c > 0) { cout << res.substr(0, len % 3)+","; res = res.substr(len % 3 , len - len%3); for (int i = 0; i < len/3; i++) { if(i!=len/3-1) cout << res.substr(i*3, 3)+","; else { cout << res.substr(i * 3, 3) << endl; } } } else { cout << "-" + res.substr(1,len%3)+","; res = res.substr(1 + len % 3 , len - len % 3); for (int i = 0; i < len / 3; i++) { if (i != len / 3 - 1) { cout << res.substr(i * 3, 3)+","; } else { cout << res.substr(i * 3, 3) << endl; } } } } } return 0; }
1002.
1002 A+B for Polynomials (25 分)
This time, you are supposed to find A+B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N1 aN1 N2 aN2 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK<⋯<N2<N1≤1000.
Output Specification:
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 2 1.5 1 2.9 0 3.2
題目大意:模擬一下多項式相加,每一位的係數要保留一位小數。
解題思路:一種是連結串列,一種是直接用一維陣列用下標表示項數的次數,為了熟悉一下stl,我用了vector容器,將兩個多項式的項依次讀入,每次檢查容器裡有沒有次數相同,如果相同就相加一下,如果沒有就重新加入容器。需要注意的是,相加之後可能會等於0,這時候就要erase這個項。最後容器按次數排序輸出就可
程式碼如下
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
struct poly {
int count;
double num;
};
bool comp(poly a,poly b){
return a.count >= b.count;
}
int main()
{
vector<poly> res;
int k, p;
cin >> k;
for (int i = 0; i < k; i++) {
poly a;
cin >> a.count >> a.num;
res.push_back(a);
}
cin >> p;
for (int i = 0; i < p; i++) {
poly a;
cin >> a.count >> a.num;
bool flag = false;
for (auto j = res.begin(); j != res.end(); j++) {
if ((*j).count==a.count)
{
flag = true;
double tmp = (*j).num + a.num;
if (tmp != 0)
(*j).num = tmp;
else
res.erase(j);
break;
}
}
if (!flag) {
res.push_back(a);
}
}
sort(res.begin(), res.end(), comp);
if (res.size() != 0) {
cout << res.size() << " ";
for (auto iter = res.begin(); iter != res.end(); iter++) {
if (iter != res.end() - 1)
cout << (*iter).count << " " << setiosflags(ios::fixed)
<< setprecision(1)<<(*iter).num << " ";
else
cout << (*iter).count << " " << setiosflags(ios::fixed)
<< setprecision(1)<<(*iter).num << endl;
}
}
else
cout << 0 << endl;
return 0;
}
1005.
1005 Spell It Right (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 (≤10100).
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
題目大意:輸入一個數字,輸出這個數字各位的和,本來是挺簡單的,但它要用英文輸出這個數字的各位和。
解題思路:很簡單粗暴的string讀進來,然後遍歷這個string把每個字元變成數字後相加,得到了各位和,然後把這個和通過to_string全域性函式變成string,再次遍歷,把每一位用英文單詞輸出一下就好了。
程式碼如下
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
string cur;
cin >> cur;
int sum = 0;
for (int i = 0; i < cur.size(); i++) {
sum = sum + (cur[i] - '0');
}
string res = to_string(sum);
for (int i = 0; i < res.size(); i++) {
switch (res[i])
{
case '1': cout << "one"; break;
case '2':cout << "two"; break;
case '3':cout << "three"; break;
case '4':cout << "four"; break;
case '5':cout << "five"; break;
case '6':cout << "six"; break;
case '7':cout << "seven"; break;
case '8':cout << "eight"; break;
case '9':cout << "nine"; break;
case '0':cout << "zero"; break;
default:
break;
}
if (i != res.size() - 1)cout << " ";
else
cout << endl;
}
return 0;
}
1006:
1006 Sign In and Sign Out (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
題目大意:大概就是記錄了每個人簽到和簽退時間,每次最早到的人是開門的,最晚走的是鎖門的,那麼輸出開門的人和鎖門的人。
解題思路:其實問題的核心在於比較兩個時間的先後,我覺得用java的話可能比較方便,因為提供了時間先後的比較函式,C++我不知道有沒有,可能有吧,因為不知道嗎,所以用字串解析也行,就是依次比較兩位數字的先後即可。按時分秒的順序比較下去。有個atoi的庫函式可以把string轉換成int,當然用流實現也可以。
程式碼如下:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
using namespace std;
struct sign{
string id;
string starttime;
string endtime;
};
bool cmp(string a,string b) {
int ahour = atoi(a.substr(0, 2).c_str());
int amin = atoi(a.substr(3, 2).c_str());
int asec = atoi(a.substr(6, 2).c_str());
int bhour = atoi(b.substr(0, 2).c_str());
int bmin = atoi(b.substr(3, 2).c_str());
int bsec = atoi(b.substr(6, 2).c_str());
if (ahour > bhour) {
return true;
}
else if (ahour < bhour) {
return false;
}
else {
if (amin > bmin) {
return true;
}
else if (amin < bmin) {
return false;
}
else {
if (asec > bsec) {
return true;
}
else if (asec <= bsec) {
return false;
}
}
}
}
sign exam[100005];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> exam[i].id >> exam[i].starttime >> exam[i].endtime;
}
string unlock=exam[0].starttime;
string lock= exam[0].endtime;
string resunlock = exam[0].id;
string reslock = exam[0].id;
for (int i = 0; i < n; i++) {
if (cmp(unlock, exam[i].starttime)) {
unlock = exam[i].starttime;
resunlock = exam[i].id;
}
if (!cmp(lock, exam[i].endtime)) {
lock = exam[i].endtime;
reslock = exam[i].id;
}
}
cout << resunlock << " " << reslock << endl;
return 0;
}