AcWing 1519. 密码
原题链接
简单
自己写的
#include <bits/stdc++.h>
using namespace std;
typedef pair<string, string> PII;
const int N = 1010;
int n;
vector<PII> res;
bool mod[N];
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ )
{
string a, b;
cin >> a >> b;
res.push_back({a, b});
}
for (size_t i = 0; i < res.size(); i ++ )
{
string k = res[i].second;
string re;
for (int j = 0; j < k.size(); j ++ )
{
if (k[j] == '1') re += '@', mod[i] = true;
else if (k[j] == '0') re += '%', mod[i] = true;
else if (k[j] == 'l') re += 'L', mod[i] = true;
else if (k[j] == 'O') re += 'o', mod[i] = true;
else re += k[j];
}
res[i].second = re;
}
int cnt = 0;
for (int i = 0; i < n; i ++ )
if (mod[i]) cnt ++ ;
if (cnt == 0)
{
if (n == 1) printf("There is 1 account and no account is modified");
else printf("There are %d accounts and no account is modified", n);
}
else
{
cout << cnt << endl;
for (int i = 0; i < n; i ++ )
if (mod[i])
cout << res[i].first << ' ' << res[i].second << endl;
}
return 0;
}
y总优美代码
题解
#include <iostream>
using namespace std;
const int N = 1010;
string name[N], pwd[N];
string change(string str)
{
string res;
for (auto c : str)
if(c == '1') res += '@';
else if(c == '0') res += '%';
else if(c == 'l') res += 'L';
else if(c == 'O') res += 'o';
else res += c;
return res;
}
int main()
{
int n;
cin >> n;
int m = 0;
for(int i = 0; i < n; i ++ )//从前往后读入每个同学的信息
{
string cur_name, cur_pwd;
cin >> cur_name >> cur_pwd;//读入当前用户名和当前密码
//判断是否修改密码(判断和修改可同时放在一个函数中)→利用函数修改掉密码中所有有歧义的字符
string changed_pwd = change(cur_pwd);//求出更新后密码
if(changed_pwd != cur_pwd)//如果密码被修改了
{
name[m] = cur_name;
pwd[m] = changed_pwd;
m++;
}
}
// !m <=> m == 0
if(!m)//如果密码没被修改过
{
if(n == 1) puts("There is 1 account and no account is modified");
else printf("There are %d accounts and no account is modified\n", n);
}
else//密码被修改过,输出名字及密码
{
cout << m << endl;
for (int i = 0; i < m; i ++ ) cout << name[i] << ' ' << pwd[i] << endl;
}
return 0;
}