1035 Password (20分)
题目链接:PAT A 1035
题目大意:输入第一行给出一个正整数n,随后n行每行给出用户名和密码(均为不含空格的字符串)。要求统计含有1,0,l,O的字符串的个数,并且将1替换成@,将0替换成%,将l替换成L,将O替换成o。如果没有能够替换的,则输出题目要求输出的句子(如果n等于1,句子中的are应该改为is)。
思路分析:非常简单的一道题,只需要建立一个结构体数组并且咋结构体中定义一个变量flag,如果需要替换,flag就为true,最后根据flag的结果进行输出即可。
#include<iostream>
#include<vector>
using namespace std;
struct id{
string account, password; //用户名和密码
bool flag = false;
};
int main() {
int n, cnt = 0; //cnt用来统计需要替换的字符串的个数
cin >> n;
vector<id> v(n);
for(int i = 0; i < n; i++) {
cin >> v[i].account >> v[i].password;
for(int j = 0; j < v[i].password.size(); j++) {
if(v[i].password[j] == '1' || v[i].password[j] == '0' || v[i].password[j] == 'l' || v[i].password[j] == 'O') {
v[i].flag = true;
if(v[i].password[j] == '1')
v[i].password[j] = '@';
else if(v[i].password[j] == '0')
v[i].password[j] = '%';
else if(v[i].password[j] == 'l')
v[i].password[j] = 'L';
else
v[i].password[j] = 'o';
}
}
if(v[i].flag == true)
cnt++;
}
if(cnt == 0) {
if(n == 1) //there后面的are变为is
cout << "There is 1 account and no account is modified";
else
cout << "There are " << n << " accounts and no account is modified";
}
else {
cout << cnt << endl;
for(int i = 0; i < n; i++) {
if(v[i].flag == true)
cout << v[i].account << " " << v[i].password << endl;
}
}
return 0;
}