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
题意:
给出N及N行信息,包括队名和密码(均不超过10位),对密码中的字符,用@替代数字1,%替代数字0,大写L替代其小写,小写o替代其大写,以作区分,输出需改变的个数即改变后的结果,或输出无需改变
分析:
代码1:分别建两个二维字符数组存放队名和密码,然后对密码数组进行检查,若有需要改变则按要求输出,需注意若用getchar读取字符则队名数组的首位是换行符,输出时应跳过
代码2:用string会方便很多
代码1://最后一个测试点有时能过有时不能过
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main()
{
int N;
cin>>N;
char T[1001][12], P[1001][12];//队名和密码
for(int i=0;i<N;i++){
int j=0;
while(1){//读入队名
T[i][j++] = getchar();
if(T[i][j-1]==' ') break;//每个队名最后一位为空格
}
j=0;
while(1){//读入密码
P[i][j++] = getchar();
if(P[i][j-1]=='\n') break;//每个密码最后一位为换行符
}
}
int change[1001], k=0;//需改变的信息编号,需改变的信息个数
for(int i=0;i<N;i++){
int flag = 0;//标记当前信息是否需改变
for(int j=0;j<11;j++)
if(P[i][j]=='1'||P[i][j]=='0'||P[i][j]=='l'||P[i][j]=='O'){
flag = 1;
if(P[i][j]=='1') P[i][j] = '@';
if(P[i][j]=='0') P[i][j] = '%';
if(P[i][j]=='l') P[i][j] = 'L';
if(P[i][j]=='O') P[i][j] = 'o';
}
if(flag) change[k++] = i;//若需改变则记录当前编号
}
if(!k){//如果没有需改变的信息
if(N==1) cout<<"There is 1 account and no account is modified"<<endl;//N为1则用is而非are
else printf("There are %d accounts and no account is modified\n",N);
}else{
cout<<k<<endl;
for(int i=0;i<k;i++){
int j=0;
if(change[i]==0){//队名数组第一个位置是换行符
j++;//跳过第一个字符
while(1){
cout<<T[change[i]][j++];
if(T[change[i]][j-1]==' ') break;
}
}else{//若不是第一队则正常输出
while(1){
cout<<T[change[i]][j++];
if(T[change[i]][j-1]==' ') break;
}
}
j=0;
while(1){
cout<<P[change[i]][j++];
if(P[change[i]][j-1]=='\n') break;
}
}
}
return 0;
}
代码2://应该是完全正确
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main()
{
int N;
cin>>N;
string *T=new string[N], *P=new string[N];//根据N为队名和密码分配空间
for(int i=0;i<N;i++) cin>>T[i]>>P[i];//读入整体字符串
int change[1001], k=0;
for(int i=0;i<N;i++){
int flag = 0;
for(int j=0;j<11;j++)
if(P[i][j]=='1'||P[i][j]=='0'||P[i][j]=='l'||P[i][j]=='O'){
flag = 1;
if(P[i][j]=='1') P[i][j] = '@';
if(P[i][j]=='0') P[i][j] = '%';
if(P[i][j]=='l') P[i][j] = 'L';
if(P[i][j]=='O') P[i][j] = 'o';
}
if(flag) change[k++] = i;
}
if(!k){
if(N==1) cout<<"There is 1 account and no account is modified"<<endl;
else printf("There are %d accounts and no account is modified\n",N);
}else{
cout<<k<<endl;
for(int i=0;i<k;i++)
cout<<T[change[i]]<<' '<<P[change[i]]<<endl;
}
return 0;
}