1. 题目
2. 分析
题意:因为在设置密码时,0与O,l与1难以区分,所以要你根据现有的密码,把其中的0、1、l、O改成%、@、L、o,如果密码没改过根据账户的数量输出对应的语句。
3. 解题过程
- 这里提供两种思路:一是,根据给出的密码,逐个字符分析是否在修改的范围,若在则修改,不在则不修改。二是,判断每个字符是否在修改的范围,是则修改并记录,否则不记录。
- 请看两种代码:
#include<string>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int main()
{
int n; scanf("%d", &n);
int cnt = 0;
vector<string> st1, st2;
for(int i = 0; i < n; i++)
{
string a, b;
cin >> a >> b;
string c;
for(int j = 0; j < b.length(); j++)
{
if(b[j] == '1')
{
c += "@";
}
else if (b[j] == '0')
{
c += "%";
}
else if(b[j] == 'l')
{
c += "L";
}
else if(b[j] == 'O')
{
c += "o";
}
else c += b.substr(j, 1);
}
if(b == c) continue;
st1.push_back(a);
st2.push_back(c);
cnt++;
}
if(cnt != 0)
{
printf("%d\n", cnt);
for(int i = 0; i < st1.size(); i++)
{
printf("%s %s\n", st1[i].c_str(), st2[i].c_str());
}
}
else if(cnt == 0 && n != 1) printf("There are %d accounts and no account is modified", n);
else if(cnt == 0 && n == 1) printf("There is 1 account and no account is modified");
return 0;
}
第二种:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n; scanf("%d", &n);
vector<string> v;
for(int i = 0; i < n; i++)
{
string name, s;
cin >> name >> s;
int len = s.length(), flag = 0;
for(int j = 0; j < len; j++)
{
switch(s[j])
{
case '1':
s[j] = '@';
flag = 1;
break;
case '0':
s[j] = '%';
flag = 1;
break;
case 'l':
s[j] = 'L';
flag = 1;
break;
case 'O':
s[j] = 'o';
flag = 1;
break;
}
}
if(flag)
{
string temp = name + " " + s;
v.push_back(temp);
}
}
int cnt = v.size();
if(cnt != 0)
{
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++)
{
cout << v[i] << endl;
}
}
else 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);
return 0;
}
后者相对来说会更好,因为判断的次数少一点。