一定要想明白需要存储哪些信息!
刚开始我不觉得要写两个数组,后来发现输出改过的信息确实要把答案存下来。
所以得 string name[N], pwd[N];
记录一下。
所以写题之前一定要想明白:需要输出什么信息,怎么输出,需要存储什么信息。
y总的判断条件是:不论啥先change()一下,然后和原来输入比一下,判断一下,不一样就把改的存下来
#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 ++ ;
}
}
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;
}
作者:yxc
而我写的是:扫一下这个字符串,有不合法的就把整个字符串改了,然后break;进入下一个循环
#include<iostream>
using namespace std;
const int N = 1010;
string id[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 cnt = 0; // 记录需要修改的数量
for (int i = 0; i < n; i ++ )
{
string cur_id, cur_pwd;
cin >> cur_id >> cur_pwd;
for (auto c : cur_pwd)
{
if (c == '1' || c == '0' || c == 'l' || c == 'O')
{
pwd[cnt] = change(cur_pwd);
id[cnt] = cur_id;
cnt ++ ;
break;
}
}
}
if (!cnt)
{
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);
}
else
{
cout << cnt << endl;
for (int i = 0; i < cnt; i ++ ) cout << id[i] << ' ' << pwd[i] << endl;
}
return 0;
}
新手学的比较吃力hh,如果有什么错误欢迎指出!