ZZULI训练:数组和字符串专题ZZULI训练: 数组和字符串专题ZZULI训练:数组和字符串专题
void solve() {map mp;string s; cin >> s;for (auto c: s) mp[c] ++;for (auto [c, cnt]: mp) {printf("%c:%d\n", c, cnt);}
}
void print(int x) { // 把输出写成外函数, 看起来清楚一点if (x == 0) cout << "Your password is wan mei.\n";if (x == 1) cout << "Your password is tai duan le.\n";if (x == 2) cout << "Your password is tai luan le.\n";if (x == 3) cout << "Your password needs shu zi.\n";if (x == 4) cout << "Your password needs zi mu.\n";
}void solve() {string s;getline(cin, s);if (s.size() < 6) {print(1);return;}int nums = 0, alp = 0; // 统计数字和字母的个数for (auto c: s) {if (c == '.') continue; // '.'特判if (isdigit(c)) nums ++;else if (isalpha(c)) alp ++;else { // 出现非法字符print(2);return;}}// 题目保证不会出现数字和字母都不存在的情况if (!nums) print(3);else if (!alp) print(4);else print(0);
}
int n, base;void solve() {cin >> n >> base;vector ne;while (n) {ne.push_back(n % base);n /= base;}reverse(ne.begin(), ne.end()); // 由于储存是逆向的, 需要反转for (auto x: ne) cout << x;cout << '\n'; // 题目要求末尾输出换行
}
void solve() {map mp;cin >> n;for (int i = 0; i < n; i ++) {int x; cin >> x;mp[x] ++;if (mp[-x]) m ++; // 如果他的相反数存在, 那么答案 + 1}cout << m;
}
void solve() {string s, t;cin >> s >> t;int cnt = 0;for (int i = 0; i < s.size(); i ++)if (tolower(s[i]) != tolower(t[i]))cnt ++;cout << cnt;
}
void solve() {set st;int x;while (cin >> x, x) st.insert(x); // 输入小技巧, 当输入的 x 是 0 时, 停止这次输入while (cin >> x, x) st.insert(x);for (auto x: st) cout << x << " ";
}
void solve() {cin >> n >> m;set st;while (n --) {int x; cin >> x;st.insert(x);} while (m --) {int x; cin >> x;if (st.count(x)) st.erase(x);}for (auto x: st) cout << x << " ";if (st.empty()) cout << 0;
}
int base = 16;int calc(string s) {int res = 0;for (auto c: s) {if (isdigit(c)) res = res * base + c - '0';else res = res * base + c - 'A' + 10; // 计算技巧, 快学一下吧}return res;
}void solve() {string s;while (cin >> s) {if (s[0] != '-') cout << calc(s) << "\n";else cout << -calc(s.substr(1)) << "\n";}
}
int n, base;void solve() {cin >> n >> base;if (n < 0) return;else if (n == 0) {cout << 0;return;}vector ne;while (n) {ne.push_back(n % base);n /= base;}reverse(ne.begin(), ne.end());for (auto x: ne) {if (x < 9) cout << x;else cout << (char)(x - 10 + 'A'); // 转换成对应的字母}
}
void solve() {vector v;string s;while (cin >> s) v.push_back(s);sort(v.begin(), v.end());int n = v.size();for (int i = 0; i < n - 1; i ++)cout << v[i] << " ";cout << v.back(); // 可以获取vector的最后一个元素
}