原题链接:1035 Password (20分)
题目大意:
1 -> @
0 -> %
l -> L
O -> o
让我们对输入的单词中,存在这些字母进行替换,如上。有这种字母的单词,就代表不合格的单词。
输出:如果更改了,就输出更改了的 username 和 password;如果没更改 就输出格式化的输出。
分析:
比较简单,直接模拟。重点是感受 string 在字符串处理中的灵活。
这里用到了 string 的 assign函数
name_out[j].assign(name);
代表在 name_out[ ] 数组的末尾拼接一个 string 类型的值。
满分代码:
#include <iostream>
#include <string.h>
#include <algorithm>
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;
const int MAXN = 1e3+10;
/*
1s -- 1e7 ~ 1e8
1 -> @
0 -> %
l -> L
O -> o
*/
string change(string str) {
string res;
for(auto c: str) {
// 直接拼接 string
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, ans = 0;
string name_out[1010], pwd_out[1010];
int j = 0;
cin >> n;
for(int i = 1; i <= n; i++) {
string name, pwd;
int flag = 0;
cin >> name >> pwd;
if(change(pwd) != pwd) {
ans++;
name_out[j].assign(name);
pwd_out[j].assign(change(pwd));
j++;
}
}
if(!ans) {
// 1 account and no account is modified
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 << ans << endl;
for(int k = 0; k < ans; k++) {
cout << name_out[k] << " " << pwd_out[k] << endl;
}
}
return 0;
}