思路:题目比较简单,先定义两个字符串数组来存储修改后的账号与密码,修改密码我是将字符串循环发现有要修改的字符修改,用count计数,当每次循环快结束时判断是否要存储,最后判断count是否为0和n是否为1来输出结果
#include<iostream>
#include <string>
using namespace std;
int main()
{
int n;
cin >> n;
string id, pass_id;
string new_id[1001], new_pass[1001];
int count = 0;
int j = 0;
for(int x=0;x<n;x++)
{
cin >> id >> pass_id;
string s= pass_id;
int i = 0;
int flag = 1;
while (pass_id[i] != '\0')
{
if (pass_id[i] == '1')
pass_id[i] = '@';
else if (pass_id[i] == '0')
pass_id[i] = '%';
else if (pass_id[i] == 'l')
pass_id[i] = 'L';
else if (pass_id[i] == 'O')
pass_id[i] = 'o';
i++;
}
if (s != pass_id)
{
count++;
new_id[j] = id;
new_pass[j++] = pass_id;
}
}
if (count)
{
cout<<count<<endl;
for (int i = 0; i < count; i++)
{
cout << new_id[i] << " " << new_pass[i] << endl;
}
}
else if(n>1)
cout << "There are " << n << " accounts and no account is modified";
else
{
cout<<"There is 1 account and no account is modified";
}
return 0;
}
更优解:
我觉得自己定义变量时总会不把能相同计数的变量减少,看了y总的代码觉得count和j可以当成同一个,并且可以定义一个函数来实现修改字符串的功能,在修改字符串时我用的是遍历字符串,看y总的是采取连接字符串来修改。(这里就没去重新写代码)
for(auto c:s)
res+=c