1035. Password 发表于 2021-01-13 分类于 algorithm-practice , PTA 本文字数: 104 阅读时长 ≈ 1 分钟水题123456789101112131415161718192021222324252627282930313233343536#include <iostream>#include <sstream>using namespace std;int main(){ int n; cin >> n; int mod = 0; ostringstream out; for (int i = 0; i < n; i++) { string name, pass; int flag = 0; cin >> name >> pass; for (auto &&ch : pass) { switch (ch) { case 'l': ch = 'L'; flag = 1; break; case '1': ch = '@'; flag = 1; break; case '0': ch = '%'; flag = 1; break; case 'O': ch = 'o'; flag = 1; break; default: break; } } if (flag) { mod++; out << name << " " << pass << endl; } } if (mod) { cout << mod << endl << out.str(); } else { cout << "There " << (n == 1 ? "is" : "are") << " " << n << " account" << (n == 1 ? "" : "s") << " and no account is modified" << endl; } return 0;}
1151. LCA in a Binary Tree 发表于 2021-01-12 分类于 algorithm-practice , PTA 本文字数: 305 阅读时长 ≈ 1 分钟二叉树上寻找最近公共祖先需要注意节点的key不一定连续
1036. Boys vs Girls 发表于 2021-01-11 分类于 algorithm-practice , PTA 本文字数: 100 阅读时长 ≈ 1 分钟送分题123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#include <iostream>using namespace std;struct student { string name; string major;};int main(){ int n; cin >> n; student s, male_min, female_max; int score, male_score_min = 101, female_score_max = -1; string gender; for (int i = 0; i < n; i++) { cin >> s.name >> gender >> s.major >> score; if (gender == "M") { if (score < male_score_min) { male_min = s; male_score_min = score; } } else { if (score > female_score_max) { female_max = s; female_score_max = score; } } } if (female_score_max != -1) { cout << female_max.name << " " << female_max.major << endl; } else { cout << "Absent" << endl; } if (male_score_min != 101) { cout << male_min.name << " " << male_min.major << endl; } else { cout << "Absent" << endl; } if (female_score_max != -1 && male_score_min != 101) { cout << female_score_max - male_score_min << endl; } else { cout << "NA" << endl; } return 0;}
1202. 交换字符串中的元素 发表于 2021-01-11 分类于 algorithm-practice , leetcode 本文字数: 243 阅读时长 ≈ 1 分钟#题意给定一些可以任意交换位置的组, 求整个字符串的最小字典序
1140. Look-and-say Sequence 发表于 2021-01-10 分类于 algorithm-practice , PTA 本文字数: 95 阅读时长 ≈ 1 分钟#题意求一个数字用行程编码压缩n-1次的结果
228. 汇总区间 发表于 2021-01-10 分类于 algorithm-practice , leetcode 本文字数: 71 阅读时长 ≈ 1 分钟水题, 注意边界情况12345678910111213141516171819class Solution {public: vector<string> summaryRanges(vector<int>& nums) { int n = nums.size(); int last = (n>0?nums[0]:0); vector<string> res; for(int i=1;i<=n;i++){ if(i==n || nums[i]!=nums[i-1]+1){ if (last==nums[i-1]){ res.push_back(to_string(last)); }else{ res.push_back(to_string(last)+"->"+to_string(nums[i-1])); } if(i<n) last=nums[i]; } } return res; }};