题目:
为了准备 PAT,系统不得不为用户生成随机密码。
但是有时一些数字和字母之间总是难以区分,比如 1(数字一)和 l(L 的小写),0(数字零)和 O(o 的大写)。
一种解决办法是将 1(数字一)替换为 @,将 0(数字零)替换为 %,将 l(L 的小写)替换为 L,将 O(o 的大写)替换为 o。
现在,你的任务就是帮助系统检查这些用户的密码,并对难以区分的部分加以修改。
输入格式
第一行包含一个整数 N,表示用户数量。
接下来 N 行,每行包含一个用户名和一个密码,都是长度不超过 10 且不含空格的字符串。
输出格式
首先输出一个整数 M,表示已修改的用户密码数量。
接下来 M 行,每行输出一个用户名称和其修改后的密码。
用户的输出顺序和读入顺序必须相同。
如果没有用户的密码被修改,则输出 There are N accounts and no account is modified,其中 N 是用户总数。
如果 N=1,则应该输出 There is 1 account and no account is modified。
数据范围
1≤N≤1000
我的解决:
#include <iostream>
using namespace std;
int main(){
int time;
cin>>time;
string teamnum[1000],password[1000],answer[1000];
for(int i=0;i<time;i++)
{
cin>>teamnum[i]>>password[i];
}
int temp=0;
int time1=0;
int dut=0;
for(int m=0;m<time;m++)
{
string str=password[m];
for(int n=0;n<int(str.size());n++)
{
if(str[n]=='1'||str[n]=='0'||str[n]=='l'||str[n]=='O')
{
if(str[n]=='1')
{
str[n]='@';
temp=1;
}
if(str[n]=='0')
{
str[n]='%';
temp=1;
}
if(str[n]=='l')
{
str[n]='L';
temp=1;
}
if(str[n]=='O')
{
str[n]='o';
temp=1;
}
dut=1;
}
}
if(dut==1)
{
answer[time1]=teamnum[m]+' '+str;
time1++;
dut=0;
}
}
if(temp==0)
{
if(time==1)
{
cout<<"There is "<<time<<" account and no account is modified";
}
else{
cout<<"There are "<<time<<" accounts and no account is modified";
}
}
else
{
cout<<time1<<endl;
for(int y=0;y<time1;y++)
{
cout<<answer[y]<<endl;
}
}
}
题解:
#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;
}
作者:锦梨冲冲冲
链接:https://www.acwing.com/solution/content/10820/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
本文介绍了一种处理容易混淆的密码字符的方法,例如数字1和字母l等,并提供了两种不同的C++实现方案来修改密码中的这些字符。
1557

被折叠的 条评论
为什么被折叠?



