今日刷题列表课程来源AcWing
其中AcWing中的题目为翻译好的中文题目
- 1001 A+B Format
- 1005 Spell It Right
- 1006 Sign In and Sign Out
- 1035 Password
- 1036 Boys vs Girls
- 1050 String Subtraction
- 1071 Speech Patterns
AcWing链接
PAT链接
英语单词
- commas 逗号
解析
- 计算A与B的和
- 转换为字符串
- 从尾开始遍历字符串,每三位输出一次逗号
- 其中i不能等于0, 会导致,999这种情况出现
- 其中i=1的时候如果c[i-1]='-'的情况也要排除, 会导致-,999的情况出现
- j % 3 == 0 && j && i && c[i - 1]!='-' 当i=0的时候c[i - 1]不会产生越界错误,因为 &&在遇到第一个假值的时候就回返回,因此不会执行到c[i - 1]
摘要
- to_string() 方法可以把一个int形的数字转换为字符串
# include1005 Spell It Rightusing namespace std; int main() { int a, b; cin >> a >> b; string c = to_string(a + b); string res; for (int i = c.size() - 1, j = 0; i >= 0; i--) { res = c[i] + res; j++; if (j % 3 == 0 && j && i && c[i - 1]!='-') res = "," + res; } cout << res << endl; return 0; }
AcWing链接
PAT链接
英语单词
- non-negative 非负的
- compute 计算
解析
- 遍历输入的字符串,使用num-'0'累加其数值到int形变量s上
- 将s装换为string类型
- 遍历s,输出对应的英文字母
摘要
- for (auto num:number)用来遍历number变量
- 题目要求结果中间留空格,末尾没有空格,因此先输出第一个结果,后面的按空格+结果输出即可达成目标
- 注: for (int i = 1; i < res.size(); ++i) 即使原本的结果为例如0这样的单个字符,循环中会因为res.size()不满足条件而直接退出,因此不会出问题
#include1006 Sign In and Sign Outusing namespace std; int main() { string number; int s; cin >> number; for (auto num:number) { s += num - '0'; } string res = to_string(s); string li[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", }; cout << li[res[0] - '0']; for (int i = 1; i < res.size(); ++i) { cout << " " << li[res[i] - '0']; } cout << endl; return 0; }
AcWing链接
PAT链接
英语单词
- records 记录
解析
- 使用unlock_number, lock_number保存最终的结果
- 使用first_in, last_out记录最先进入的时间和最后退出的时间
- 将每次输入的时间进行比较,满足条件则记录当前的id
摘要 - 本题的时间可以直接根据字典序来比较
#include1035 Passwordusing namespace std; bool compare_time(string a, string b) { if (a > b) return 1; return 0; } int main() { int m; cin >> m; string unlock_number, lock_number; string first_in, last_out; for (int i = 0; i < m; ++i) { string id_number, in_time, out_time; cin >> id_number >> in_time >> out_time; if (!compare_time(in_time, first_in) || !i) { // 大于返回1,否则返回0 first_in = in_time; unlock_number = id_number; } if (compare_time(out_time, last_out) || !i) { last_out = out_time; lock_number = id_number; } } cout << unlock_number << " " << lock_number << endl; return 0; }
AcWing链接
PAT链接
英语单词
- accounts 账户
解析
- need_modify判断是否需要修改
- change 返回修改后的结果
- 对每个当前输入的字段判断是否要修改,加入name和password列表中
- 最后按指定格式输出, 仔细即可
摘要
- 判断处的逻辑可以修改为 先string change_pass = change(pass)将每个输入的密码都进行修改,再if(change_pass==pass)来判断输入的密码是否经过修改,就省略了手写的need_modify步骤
#include1036 Boys vs Girlsusing namespace std; bool need_modify(string password) { for (auto c:password) { if (c == '1' || c == '0' || c == 'l' || c == 'O') return true; } return false; } string change(string pass) { string res; for (auto c:pass) { if (c == '1') res += '@'; else if (c == '0') res += '%'; else if (c == 'l') res += 'L'; else if (c == 'O') res += 'o'; else res += c; } return res; } int main() { int n; cin >> n; string name[1005], password[1005]; int con = 0; for (int i = 0; i < n; ++i) { string user, pass; cin >> user >> pass; if (need_modify(pass)) { name[con] = user; password[con] = change(pass); con++; } } if (!con) { if (n == 1) cout << "There is 1 account and no account is modified" << endl; else cout << "There are " << n << " accounts and no account is modified" << endl; } else { cout << con << endl; for (int i = 0; i < con; ++i) { cout << name[i] << " " << password[i] << endl; } } return 0; }
AcWing链接
PAT链接
解析
本题思路较为简单,仔细即可
摘要
- boy_name.empty() 方法判断一个字符串是否为空
#include1050 String Subtractionusing namespace std; int main() { int n; cin >> n; // 男生的最低成绩,女生的最高成绩 string girl_name, girl_id; int girl_grade = 0; string boy_name, boy_id; int boy_grade = 100; for (int i = 0; i < n; ++i) { string cur_name, cur_gender, cur_id; int cur_grade; cin >> cur_name >> cur_gender >> cur_id >> cur_grade; if (cur_gender == "M" && cur_grade <= boy_grade) { boy_name = cur_name; boy_id = cur_id; boy_grade = cur_grade; } if (cur_gender == "F" && cur_grade >= girl_grade) { girl_name = cur_name; girl_id = cur_id; girl_grade = cur_grade; } } if (!boy_name.empty() && !girl_name.empty()) { cout << girl_name << " " << girl_id << endl; cout << boy_name << " " << boy_id << endl; cout << girl_grade - boy_grade << endl; } else if (boy_name.empty()) { cout << girl_name << " " << girl_id << endl; cout << "Absent" << endl; cout << "NA" << endl; } else { cout << "Absent" << endl; cout << boy_name << " " << boy_id << endl; cout << "NA" << endl; } return 0; }
AcWing链接
PAT链接
英语单词
- Subtraction 减法
- respectively 分别的,依次的
- new line character 换行符
解析
- 暴力解, 两个遍历解决问题
- 将s2存储到哈希表中,因为哈希表的查找操作是O(1)
摘要
- getline(cin, s1);新的读取方法,因为此时的s1中包含了空格,所以要用这种方法
- hashmap的简单使用
- 导入 #include
- 创建 unordered_map
map; - 插入 类似map['c']=0,像python的字典一样
- 查找 map.count(c) 查找map中字符c出现的次数
- 导入 #include
- hashset 的插入是hash.insert(c)
#include解法2: 快速解(hashmap)using namespace std; bool judge(char s, string s2) { for (auto c:s2) { if (s == c) return true; } return false; } int main() { string s1, s2; getline(cin, s1); getline(cin, s2); string res; for (auto s:s1) { if (judge(s, s2)) continue; res += s; } cout << res << endl; return 0; }
#include解法3: 快速解(hashset)#include using namespace std; int main() { string s1, s2; getline(cin, s1); getline(cin, s2); unordered_map map; for (auto c:s2)map[c] = 1; for (auto c: s1) { if (!map.count(c)) cout << c; } cout << endl; return 0; } //They are students. //aeiou //Thy r stdnts.
#include1071 Speech Patterns#include using namespace std; int main() { string s1, s2; getline(cin, s1); getline(cin, s2); unordered_set hash; for (auto c:s2)hash.insert(c); for (auto c: s1) { if (!hash.count(c)) cout << c; } cout << endl; return 0; } //They are students. //aeiou //Thy r stdnts.
AcWing链接
PAT链接
英语单词
- preference 偏爱, 偏好
- synonyms 同义字, 同义词
- online avatar 在线头像
- lexicographically smallest one 字典序最小的那个
解析 - 使用双指针的方法读取一个字符串中的各个单词
- 读取的过程中将单词转为小写并且直接存入map中
- map[cur_s]++ 如果当前的map中没有cur_s也没关系
摘要
- tolower() 将字符转为小写
- unordered_map的遍历for (auto item:map)
- item.first指key
- item.seconde指value
#include#include using namespace std; bool check(char c) { if (c >= 'a' && c <= 'z') return true; if (c >= 'A' && c <= 'Z') return true; if (c >= '0' && c <= '9') return true; return false; } int main() { string s1; getline(cin, s1); unordered_map map; for (int i = 0; i < s1.size(); ++i) { if (check(s1[i])) { string cur_s; int j = i + 1; cur_s += tolower(s1[i]); while (j < s1.size()) { if (check(s1[j])) cur_s += tolower(s1[j++]); else break; } i = j; map[cur_s]++; } } string res; int value = 0; for (auto item:map) { if (item.second > value || (item.second == value && item.first < res)) { res = item.first; value = item.second; } } cout << res << " " << value << endl; return 0; } //Can1: "Can a can can a can? It can!" //can 5



