【PTA刷题】乙级 1086 To 1095

    技术2024-10-01  51

    B1086.就不告诉你 3min 签到题

    /*B1086*/ #include <iostream> #include <string> #include <algorithm> #include <stdio.h> #include <cctype> using namespace std; int main() { int a, b; cin >> a >> b; string str = to_string( a*b ); reverse(str.begin(), str.end()); cout << stoi(str); return 0; }

    B1087.有多少种不同的值 5min 签到题 我用的map,柳神用的set。利用了set中每个元素唯一的特性。

    /*B1087*/ #include <iostream> #include <algorithm> #include <stdio.h> #include <map> using namespace std; int getsum(int n) { return (n / 2) + (n / 3) + (n / 5); } int main() { int N; cin >> N; map<int, int> m; for (int i = 1; i <= N; i++) { int sum = getsum(i); m[sum]++; } cout << m.size(); return 0; }

    B1088.三人行 27min 有两个点卡了好久。 主要原因是一开始看错题,把NXY三个整数看成了甲乙丙必须都是整数。实际上丙可以是小数。 我一开始用的i从10到99正向查找遍历,这样会有几次甲的更新,这样要写好几个continue导致最后甲数字固定了,乙却自己会变动到99。这样的话,到最后还要写两行代码算乙和丙的值,很不划算。还不如用柳神的方法从99遍历到10.

    /*B1088*/ #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stdio.h> #include <cctype> #include <math.h> #include <map> using namespace std; void OutPut(int M,double n) { if (M > n) cout << " Gai"; else if (M < n) cout << " Cong"; else cout << " Ping"; } int main() { int M, X, Y; cin >> M >> X >> Y; int jia = 0, yi = 0; double bing; for (int i = 99; i >= 10; i--) { jia = i; yi = 10 * (i % 10) + i / 10; bing = abs(i - yi) * 1.0 / X; if (bing * Y == yi) { cout << jia; OutPut(M, jia); OutPut(M, yi); OutPut(M, bing); return 0; } } cout << "No Solution"; return 0; }

    B1090.危险品装箱 40min 乍一看肯定会超时,其实并没有。和柳神逻辑一模一样。用MAP下面value挂一个vector即可。 1.注意不兼容是双向不兼容。要做两次puch_back。 2.push_back的时候直接 map[root].push_back(value) 即可。

    /*B1090*/ #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stdio.h> #include <map> using namespace std; int main() { int N, M; cin >> N >> M; map<string, vector<string> > m; for (int i = 0; i < N; i++) { string root, info; cin >> root >> info; m[root].push_back(info); m[info].push_back(root);//互相不兼容 } for (int cnt = 0; cnt < M; cnt++) { int K,flag = 1; cin >> K; vector<string> vec;//对应每个集装箱 for (int i = 0; i < K; i++) { string check; cin >> check; vec.push_back(check); } for (int i = 0; i < K; i++) { for (int j = 0; j < m[vec[i]].size(); j++) { if (find(vec.begin(), vec.end(), m[vec[i]][j]) != vec.end()) { cout << "No\n"; flag = 0; break; } } if (flag == 0) break; } if(flag == 1) cout << "Yes\n"; } return 0; }

    B1091.N-自守数 17min 注意读题。题目两点要读清 1.N是小于10的。 2.原本K是几位,就要到末尾找几位。 注意这两点就很容易了

    /*B1091*/ #include <iostream> #include <string> #include <algorithm> #include <stdio.h> using namespace std; const int MaxRound = 10; int IsZishou(int K) { string str = to_string(K); int len = str.length(); int tmp = 1; for (int i = 0; i < len; i++) tmp = tmp * 10; for (int i = 1; i < MaxRound; i++) { int n = i * K * K; if (K == n % tmp) return i; } return -1; } int main() { int M; cin >> M; for (int i = 0; i < M; i++) { int K,N; cin >> K; N = IsZishou(K); if (N != -1) cout << N << " " << N * K * K << "\n"; else cout << "No\n"; } return 0; }

    B1092.最好吃的月饼 10min 签到题

    /*B1092*/ #include <iostream> #include <stdio.h> using namespace std; int MoonCake[1005] = { 0 }; int main() { int N, M; cin >> N >> M; for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { int sale; cin >> sale; MoonCake[j] += sale; } } int Max = -1; for (int i = 0; i < N; i++) { if (MoonCake[i] > Max) Max = MoonCake[i]; } cout << Max << "\n"; int flag = 0; for (int i = 0; i < N; i++) { if (MoonCake[i] == Max) { if (flag == 0) { cout << i+1; flag = 1; } else cout << " " << i+1; } } return 0; }

    B1093.字符串A+B 10min 签到题

    /*B1093*/ #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stdio.h> #include <cctype> #include <math.h> using namespace std; int main() { string A, B; getline(cin, A); getline(cin, B); vector<char> appear; for (int i = 0; i < A.length(); i++) { if (find(appear.begin(), appear.end(), A[i]) == appear.end())//在已出现里面没找到 { cout << A[i]; appear.push_back(A[i]); } else continue; } for (int i = 0; i < B.length(); i++) { if (find(appear.begin(), appear.end(), B[i]) == appear.end()) { cout << B[i]; appear.push_back(B[i]); } else continue; } return 0; }

    B1094.谷歌的招聘 16min 挂了两个点扣了4分,和柳神代码几乎一模一样,原因未知。 虽然一开始没有考虑进去1和0不是素数,但是加上了也没啥不同。

    注意一点:str.substr(开始未知,你要截取的个数),第二个参数直接给个个数就行,不用指针加上偏移量。

    /*B1094*/ #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stdio.h> #include <cctype> #include <math.h> using namespace std; bool IsPrime(int n) { if (n == 0 || n == 1) return false; for (int i = 2; i * i < n; i++) { if (n % i == 0) return false; } return true; } int main() { string ori; int L, K; cin >> L >> K >> ori; int flag = 0; for (int i = 0; i < L-K; i++) { string tmp = ori.substr(i, K); if (IsPrime(stoi(tmp))) { cout << tmp; flag = 1; break; } else continue; } if (flag == 0) cout << 404<<"\n"; return 0; }

    B1095.解码PTA准考证 78min 非常繁琐。只过了一个点,拿了15分。答案错误两个点,超时两个点。 需要注意一点。map.find()里面的查找,是查找某个键值有没有出现过,然后返回一个迭代器。 所以写法是 map.find(你要查找的key) != map.end(); 此外强调,map的迭代只能用迭代器做。因为map自己加进去的时候就排序了。要想排序还是得用vector,如果要处理的数据复杂,就用vector包结构体。

    /*B1095*/ #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stdio.h> #include <cctype> #include <math.h> #include <map> using namespace std; struct Data { string level; string count; string testday; string id; int score; }; map < string, Data> m; struct CountInfo { string id; int cnt; }; bool cmp(string a,string b) { if (m[a].score != m[b].score) return m[a].score > m[b].score ? true : false;//分数大的在前面 else return a < b ? true : false;//准考证号小的在前面 } bool cmp3(CountInfo a,CountInfo b) { if (a.cnt != b.cnt) return a.cnt > b.cnt ? true : false; else return a.id < b.id ? true : false; } int main() { int N,M; cin >> N >> M; for (int i = 0; i < N; i++) { string str; int Score; cin >> str >> Score; Data d; d.level = str[0]; d.count = str.substr(1, 3); d.testday = str.substr(4, 6); d.id = str.substr(10, 3); d.score = Score; m[str] = d; } for (int i = 1; i <= M; i++) { int todo; cin >> todo; if (todo == 1) { int flag = 0; string CheckLevel; cin >> CheckLevel; if (CheckLevel == "T" || CheckLevel == "A" || CheckLevel == "B") flag = 1; vector<string> vec;//存放map的key即可 for (auto it = m.begin(); it != m.end(); it++) { if (it->second.level == CheckLevel) vec.push_back(it->first); } sort(vec.begin(), vec.end(), cmp); cout << "Case " << i << ": " << todo << " " << CheckLevel<<"\n"; if (flag == 1) { for (int j = 0; j < vec.size(); j++) cout << vec[j] << " " << m[vec[j]].score << "\n"; } else if (flag == 0) cout << "NA\n"; } else if (todo == 2) { int flag = 0; string CheckCount; cin >> CheckCount; int StuNum = 0, SumScore = 0; for (auto it = m.begin(); it != m.end(); it++) { if (it->second.count == CheckCount) { StuNum++; SumScore += it->second.score; flag = 1; } } cout << "Case " << i << ": " << todo << " " << CheckCount<< "\n"; if (flag == 1) cout << StuNum << " " << SumScore << "\n"; else if (flag == 0) cout << "NA\n"; } else if (todo == 3) { int flag = 0; string CheckDay; cin >> CheckDay; map<string, int> CountM; vector<CountInfo> res; for (auto it = m.begin(); it != m.end(); it++) { if (it->second.testday == CheckDay) { CountM[it->second.count]++;//在MAP中对应考场人数加一 flag = 1;//这个日期是存在的 } } for (auto it = CountM.begin(); it != CountM.end(); it++)//遍历中间map存入res中 { res.push_back({ it->first,it->second }); } sort(res.begin(), res.end(), cmp3); cout << "Case " << i << ": " << todo << " " << CheckDay << "\n"; if(flag == 1) for (int j = 0; j < res.size(); j++) cout << res[j].id << " " << res[j].cnt << "\n"; else if(flag == 0) cout << "NA\n"; } } return 0; }
    Processed: 0.012, SQL: 9