天梯赛刷题(也就写写水题骗骗自己了)
目录L1-002 打印沙漏L1-003 个位数统计L1-005 考试座位号L1-006 连续因子L1-009 N个数求和L1-011 A-BL1-017 到底有多二L1-002 打印沙漏拉了坨大的#include bits/stdc.h using namespace std; int n; char c; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin n c; int len 1; int sum 1; for (len 1;; len 2) { sum (len 2) * 2; if (sum n) break; } vectorvectorchar ans(len 1, vectorchar(len 1, )); int cnt 0; for (int i 1; i len; i) { int left min(i, len - i 1); int right max(i, len - i 1); for (int j left; j right; j) { ans[i][j] c; cnt; } } for (int i 1; i len; i) { for (int j 1; j max(i, len - i 1); j) { cout ans[i][j]; } cout \n; } cout n - cnt; return 0; }L1-003 个位数统计又拉了坨大的#include bits/stdc.h using namespace std; string s; bool com(const pairint, int x, const pairint, int y) { return x.first y.first; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin s; unordered_mapchar, int map; for (char c : s) map[c]; vectorpairint, int a; for (auto [key, val] : map) a.push_back({key - 0, val}); sort(a.begin(), a.end(), com); for (int i 0; i a.size(); i) { cout a[i].first : a[i].second \n; } return 0; }L1-005 考试座位号用map存三个数值#include bits/stdc.h using namespace std; int n; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin n; unordered_mapint, pairstring, int map; for (int i 0; i n; i) { string a; int b, c; cin a b c; map[b] {a, c}; } int m; cin m; while (m--) { int t; cin t; cout map[t].first map[t].second \n; } return 0; }L1-006 连续因子枚举每个i后面的长度#include bits/stdc.h using namespace std; #define int long long int n; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin n; int maxLen 0; int start 0; for (int i 2; i * i n; i) { int tem 1; int len 0; // len指验证成功的个数j从i开始验证 for (int j i;; j) { tem * j; if (n % tem) break; len; if (len maxLen) { maxLen len; start i; } } } if (!maxLen) cout 1 \n n; else { cout maxLen \n; for (int i 0; i maxLen; i) { if (i) cout *; cout i start; } } return 0; }L1-009 N个数求和依次处理边加边约分很好的模拟题在分数计算和输出的地方卡住了#include bits/stdc.h using namespace std; #define int long long int n; int gcd(int x, int y) { if (x y) swap(x, y); while (x) { int t y % x; y x; x t; } return y; } signed main() { scanf(%lld, n); int a 1; int b 0; while (n--) { int c, d; scanf(%lld/%lld, c, d); b b * d a * c; a a * d; int k gcd(a, b); a / k; b / k; } if (b 0) { printf(0); return 0; } if (b 0) { printf(-); b -b; } if (b % a 0) { printf(%lld, b / a); } else { if (b a) { printf(%lld %lld/%lld, b / a, b % a, a); } else { printf(%lld/%lld, b, a); } } return 0; }L1-011 A-B整行读入getline(cin,s);如果上一行用cin读入了末尾会有换行符不能直接用getline应该先写cin.ignore();#include bits/stdc.h using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string a, b; getline(cin, a); getline(cin, b); unordered_mapchar, int map; for (char c : b) map[c]; for (char c : a) { if (map[c] 0) cout c; } return 0; }L1-017 到底有多二关闭流同步后cout和printf不能混用#include bits/stdc.h using namespace std; string n; int main() { cin n; int len n.size(); int two 0; int ou 0; for (int i 0; i n.size(); i) { if (!i n[i] -) len--; if (n[i] 2) two; if (i n.size() - 1 (n[i] - 0) % 2 0) { ou 1; } } double ans (double)two * 100.0 / (double)len; if (len ! n.size()) ans * 1.5; if (ou) ans * 2; printf(%.2f, ans); cout %; return 0; }