PAT グレード A テスト記録-(AcWing)-Day13(文字列)
このコースは AcWing から提供されており、
AcWing のタイトルは中国語のタイトルに翻訳されています。
ブラシリスト
1060 それらは等しいですか
英単語
分析する
substr
の使用法
string s = “0123456789”;
string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = “56789”
string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = “567”
find()
の使用法
str.find(str, beg=0, end=len(string);
- 文字列 :
a.find('.');
- beg : 開始インデックス、デフォルトは 0 です。
- end : 終了インデックス、デフォルトは文字列の長さです
>>> info = 'abca'
>>> print(info.find('a')) # 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
0
>>> print(info.find('a', 1)) # 从下标1开始,查找在字符串里第一个出现的子串:返回结果3
3
>>> print(info.find('3')) # 查找不到返回-1
-1
>>>
注意点
入力された数値が0であるとすると、出力は0.000*10^0
#include <iostream>
#include <cstring>
using namespace std;
int n;
string a, b;
string change(string x) {
int k = x.find('.'); // 前移之后的位数
if (k == -1) x += '.', k = x.find('.');// 传入的数字不含小数点,及小数点在最后一位
string res = x.substr(0, k) + x.substr(k + 1); //去掉小数点,指数已经记录在k里面
while (res[0] == '0' && res.size() > 1) res = res.substr(1), k--; // 去除前导0
if (res == "0") k = 0;
if (res.size() > n) res = res.substr(0, n);// 删除前导0后,检查长度与n的关系,删除超出部分
while (res.size() < n) res += "0"; // 补全末尾的0
return "0." + res + "*10^" + to_string(k);
}
int main() {
cin >> n >> a >> b;
a = change(a);
b = change(b);
if (a == b) cout << "YES " << a;
else cout << "NO " << a << " " << b;
return 0;
}
1073 科学表記法
英単語
- 整数部分 整数部分
分析する
注意点は、
小数点を最初の0
位に移動してから、次k
の値に応じて調整します。
#include <iostream>
#include <cstring>
using namespace std;
string a;
int main() {
cin >> a;
if (a[0] == '-') cout << '-';
a = a.substr(1, a.size() - 1);
string body = a[0] + a.substr(2, a.find("E") - 2);
int k = stoi(a.substr(a.find("E") + 1, a.size())); // 后面可以写大
k++; // 小数点本来在第一位,现在让他到第0位去
if (k < 0) body = string(abs(k), '0') + body, cout << "0." << body;
else if (k > 0) {
// 先让小数点往后移动
if (k >= body.size()) body = body + string(k - body.size(), '0');
else body = body.substr(0, k) + "." + body.substr(k, body.size());
cout << body;
} else {
cout << "0." << body;
}
return 0;
};
1077 Kuchiguse
英単語
分析する
直接列挙すると、
#include <iostream>
#include <cstring>
using namespace std;
const int N = 110;
string words[N];
int n;
int main() {
cin >> n;
getchar();
for (int i = 0; i < n; ++i)getline(cin, words[i]);
int k;
for (k = words[0].size(); k > 0; k--) {
string kuchiguise = words[0].substr(words[0].size() - k);
bool success = true;
for (int i = 1; i < n; ++i) {
if (k > words[i].size()) {
success = false;
break;
}
if (words[i].substr(words[i].size() - k) != kuchiguise) {
success = false;
break;
}
}
if (success) {
cout << kuchiguise;
break;
}
}
if (k == 0) cout << "nai";
return 0;
}
!!! 1082 中国語の数字を読む
英単語
数値を 4 桁个十百千
ごとに解析して出力します
- 4桁以上の場合は10,000を出力
- 8桁以上の場合は1億を出力します。
- 現在の 4 桁の数値が 1,000 未満の場合は、先頭にゼロを追加します
- 2 つの連続するゼロがある場合は、削除します。
- 80000 のような数字の場合、最終判定では末尾のゼロをすべて削除する必要があります。
注意点
#include <iostream>
#include <vector>
using namespace std;
string num1[] = {
"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
bool check(string s) {
// 判断末尾是否是零
return s.size() >= 5 && s.substr(s.size() - 5) == "ling ";
}
string word(int n) {
vector<int> nums;
while (n) nums.push_back(n % 10), n /= 10;
string num2[] = {
"", "Shi", "Bai", "Qian"};
string res;
for (int i = nums.size() - 1; i >= 0; i--) {
int t = nums[i];
if (t) res += num1[t] + ' ';
else if (!check(res)) res += "ling ";
if (t && i) res += num2[i] + ' ';
}
if (check(res)) res = res.substr(0, res.size() - 5);
return res;
}
int main() {
int n;
cin >> n;
if (!n) puts("ling");
else {
if (n < 0) cout << "Fu ", n = -n;
vector<int> nums;
string num3[] = {
"", "Wan", "Yi"};
while (n) nums.push_back(n % 10000), n /= 10000;
string res;
for (int i = nums.size() - 1; i >= 0; i--) {
int t = nums[i];
if (res.size() && t < 1000 && !check(res)) {
res += "ling ";
}
if (t) res += word(t);
if (t && i)res += num3[i] + ' ';
}
while (check(res)) res = res.substr(0, res.size() - 5);
res.pop_back();
cout << res << endl;
}
return 0;
}
1084 壊れたキーボード
#include<iostream>
#include<unordered_set>
using namespace std;
bool visit[100];
int main() {
string a, b;
b += '#';
cin >> a >> b;
for (int i = 0, j = 0; i < a.size(); ++i) {
char x = toupper(a[i]), y = toupper(b[j]);
if (x == y) j++;
else {
if (!visit[x]) cout << x;
visit[x] = true;
}
}
return 0;
}
1108 平均値を求める
注意点
size_t size;
x = stof(num, &size);
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int n;
cin >> n;
string num;
double res = 0, x = 0;
int cnt = 0;
while (n--) {
cin >> num;
bool suc = true;
try {
size_t size;
x = stof(num, &size);
if (size != num.size()) suc = false;
if (x < -1000 || x > 1000) suc = false;
int k = num.find('.');
if (k != -1 && num.substr(k).size() > 3) suc = false;
} catch (...) {
suc = false;
}
if (suc) res += x, cnt++;
else printf("ERROR: %s is not a legal number\n", num.c_str());
}
if (cnt == 0) puts("The average of 0 numbers is Undefined");
if (cnt == 1)printf("The average of 1 number is %.2lf\n", res / cnt);
if (cnt > 1) printf("The average of %d numbers is %.2lf\n", cnt, res / cnt);
return 0;
}
1124 Weiboフォロワーのための抽選
#include <iostream>
#include <unordered_set>
using namespace std;
const int N = 1010;
string name[N];
unordered_set<string> set;
int main() {
int m, n, s;
cin >> m >> n >> s;
for (int i = 1; i <= m; ++i)cin >> name[i];
if (s > m) puts("Keep going...");
else {
while (s <= m) {
if (set.count(name[s])) s++;
else{
set.insert(name[s]);
cout << name[s] << endl;
s += n;
}
}
}
return 0;
}
1141 PAT 機関ランキング
英単語
分析する
注意点
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
const int N = 100010;
int n;
struct School {
string name;
double total;
int cnt;
School() : total(0), cnt(0) {
}
bool operator<(const School &t) const {
if ((int)total != (int)t.total) return (int)total > (int)t.total;
if(cnt != t.cnt) return cnt < t.cnt;
return name < t.name;
}
};
unordered_map<string, School> map;
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
string id, school;
string stu;
int score;
cin >> id >> score >> school;
for (auto &c:school) stu += tolower(c);
map[stu].name = stu, map[stu].cnt++;
double grade = score;
if (id[0] == 'B') grade /= 1.5;
else if (id[0] == 'T') grade *= 1.5;
map[stu].total += grade;
}
printf("%d\n", map.size());
vector<School> res;
for (auto &item:map)res.push_back(item.second);
sort(res.begin(), res.end());
int rank = 1;
auto stu = res[0];
printf("1 %s %d %d\n", stu.name.c_str(), (int) stu.total, stu.cnt);
for (int i = 1; i < res.size(); ++i) {
auto stu = res[i];
if ((int) res[i].total < (int) res[i - 1].total)rank = i + 1;
printf("%d %s %d %d\n", rank, stu.name.c_str(), (int) stu.total, stu.cnt);
}
return 0;
}
1153 PAT の登録カードをデコードする
カスタムvector<pair<string, int>>
ソート
bool cmp(const pair<string, int> &p1, const pair<string, int> &p2) {
if (p1.second != p2.second) return p1.second > p2.second;
return p1.first < p2.first;
}
詳細な質問
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 10010;
int n, m;
struct Student {
string id;
int score;
bool operator<(const Student &s) const {
if (score != s.score) return score > s.score;
return id < s.id;
}
} stu[N];
bool cmp(const pair<string, int> &p1, const pair<string, int> &p2) {
if (p1.second != p2.second) return p1.second > p2.second;
return p1.first < p2.first;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> stu[i].id >> stu[i].score;
}
for (int i = 1; i <= m; ++i) {
int t;
string op;
cin >> t >> op;
printf("Case %d: %d %s\n", i, t, op.c_str());
if (t == 1) {
// 输出所有人的编号和成绩,按分数从高到底,编号从小到大
vector<Student> res;
// 遍历所有的学生,找到相同考试的
for (int j = 0; j < n; ++j) {
if (stu[j].id[0] == op[0]) {
res.push_back(stu[j]);
}
}
if (res.size() == 0)puts("NA");
else {
sort(res.begin(), res.end());
for (auto &item:res) {
printf("%s %d\n", item.id.c_str(), item.score);
}
}
} else if (t == 2) {
// op 为考场号,在id中的234位
int cnt = 0, sum = 0;
for (int j = 0; j < n; ++j) {
if (stu[j].id.substr(1, 3) == op) {
cnt++;
sum += stu[j].score;
}
}
if (!cnt) puts("NA");
else {
printf("%d %d\n", cnt, sum);
}
} else {
// t = 3 输入时间, 找该时间内的考场,以及考场中的考生数量
// 输出的格式 考生量从大到小, 考场编号从小到大
unordered_map<string, int> map;
vector<pair<string, int>> res;
for (int j = 0; j < n; ++j) {
if (stu[j].id.substr(4, 6) == op) {
string site = stu[j].id.substr(1, 3);
map[site]++;
}
}
if (map.size() == 0) puts("NA");
else {
for (auto &item:map) {
res.push_back({
item.first, item.second});
}
sort(res.begin(), res.end(), cmp);
for (auto &item:res) {
printf("%s %d\n", item.first.c_str(), item.second);
}
}
}
}
return 0;
}