题目来源:PAT (Advanced Level) Practice
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1
(one) from l
(L
in lowercase), or 0
(zero) from O
(o
in uppercase). One solution is to replace 1
(one) by @
, 0
(zero) by %
, l
by L
, and O
by o
. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N (≤1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.
Output Specification:
For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified
where N
is the total number of accounts. However, if N
is one, you must print There is 1 account and no account is modified
instead.
Sample Input 1:
3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa
Sample Output 1:
2
Team000002 RLsp%dfa
Team000001 R@spodfa
Sample Input 2:
1
team110 abcdefg332
Sample Output 2:
There is 1 account and no account is modified
Sample Input 3:
2
team110 abcdefg222
team220 abcdefg333
Sample Output 3:
There are 2 accounts and no account is modified
words:
confusing 难以理解的,不清楚的 distinguish 区分,辨别 replace 替换,取代
modify 修改,调整 judge 判断,评判 consists of 由…组成 corresponding 相应的
思路:
1. 对于密码中含有难以识别的的字符时用其他字符替代;1用@、l 用L、0用%、O用o替换;
2. 把替换后的密码和其用户名保存到一个数组p中,并用k记录个数,若没有任何替换则不保存;
3.若所有的输入都没有发生替换,则当n==1时,输出“There is 1 account and no account is modified
”,当n>1时输出“There are N accounts and no account is modified"
;
4.否则有发生替换则首先输出替换的密码个数k,然后按输入顺序替输出替换后的密码和用户名,即数组p中的元素;(没有替换的元素则无需输出)
//PAT ad 1035 Password
#include <iostream>
using namespace std;
#define N 1000
pair<string,string> p[N]; //记录发生替换的用户名和密码
int k=0;
void needToModify(string name,string password) //检测密码是否需要修改并修改
{
int c=0,i;
for(i=0;i<password.size();i++)
{
switch(password[i])
{
case '1':password[i]='@';break;
case '0':password[i]='%';break;
case 'l':password[i]='L';break;
case 'O':password[i]='o';break;
default:c++;
}
}
if(c==i) return ; //没有发生替换
p[k++]={name,password};
}
int main()
{
int n,i;
cin>>n;
string name,password;
for(i=0;i<n;i++) //输入
{
cin>>name>>password;
needToModify(name,password);
}
if(k==0) //没有要修改时的输出
{
if(n==1)
cout<<"There is 1 account and no account is modified"<<endl;
else
cout<<"There are "<<n<<" accounts and no account is modified"<<endl; //大于1用单词用复数形式
}
else //有修改时的输出
{
cout<<k<<endl;
for(i=0;i<k;i++)
cout<<p[i].first<<" "<<p[i].second<<endl;
}
return 0;
}