Z Z U L I 训练 : 数组和字符串专题 ZZULI训练: 数组和字符串专题 ZZULI训练:数组和字符串专题
void solve() {
map<char, int> 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<int> 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<int, int> 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<int> 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<int> 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<int> 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<string> 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的最后一个元素
}