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.
为了准备 PAT,系统不得不为用户生成随机密码。但是有时一些数字和字母之间总是难以区分,比如 1
(数字一)和 l
(L 的小写),0
(数字零)和 O
(o 的大写)。一种解决办法是将 1
(数字一)替换为 @
,将 0
(数字零)替换为 %
,将 l
(L 的小写)替换为 L
,将 O
(o 的大写)替换为 o
。现在,你的任务就是帮助系统检查这些用户的密码,并对难以区分的部分加以修改。
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.
第一行包含一个整数 N,表示用户数量。接下来 N 行,每行包含一个用户名和一个密码,都是长度不超过 10 且不含空格的字符串。
Output Specification:
For each test case, first print the number M of accounts that have been modified, then print in the following Mlines 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.
首先输出一个整数 M,表示已修改的用户密码数量。接下来 M 行,每行输出一个用户名称和其修改后的密码。用户的输出顺序和读入顺序必须相同。如果没有用户的密码被修改,则输出 There are N accounts and no account is modified
,其中 N 是用户总数。如果 N=1,则应该输出 There is 1 account and no account is modified
。
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
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
解析:
本题我们可以通过使用一个结构体存储变量,然后对密码进行暴力循环,从而判断是否有对应需要修改的字符,输出对应答案。需要注意最后输出的单数和复数形式,笔者在这里被坑惨了。
我们首先定义一个结构体,其中flag用于判断密码是否被修改,真为1,假为0:
struct user
{
string name,psw;
int flag;
}u[1001];
然后我们在主函数中定义两个计数器,分别用来计算被修改密码的个数和未被修改密码的个数,并输入用户数量:
int n;
int count1=0;
int count2=0;
cin>>n;
利用for循环我们对用户信息进行输入,同时为flag赋初值:
for(int i=0;i<n;i++){
cin>>u[i].name>>u[i].psw;
u[i].flag=0;
在第二个for循环里,我们对每位用户的密码进行是否需要修改的判断,若是需要则将其按题目要求进行修改,并将flag设为1:
for(int j=0;j<u[i].psw.size();j++){
if(u[i].psw[j]=='1') {
u[i].psw[j]='@';
u[i].flag=1;
}
if(u[i].psw[j]=='0') {
u[i].psw[j]='%';
u[i].flag=1;
}
if(u[i].psw[j]=='l') {
u[i].psw[j]='L';
u[i].flag=1;
}
if(u[i].psw[j]=='O') {
u[i].psw[j]='o';
u[i].flag=1;
}
}
if(u[i].flag==1) count1+=1;
if(u[i].flag==0) count2+=1;
最后,如果密码被修改,则输出被修改密码的数量,以及修改后的密码;如果密码未被修改,则根据单复数形式输出对应语句:
for(int i=0;i<n;i++){
if(u[i].flag==1){
cout<<u[i].name<<" "<<u[i].psw<<endl;
}
if(count2==n&&count2==1){
cout<<"There is "<<count2<<" account and no account is modified";
break;
}
if(count2==n&&count2!=1){
cout<<"There are "<<count2<<" accounts and no account is modified";
break;
}
}
完整代码:
#include<bits/stdc++.h>
using namespace std;
struct user
{
string name,psw;
int flag;
}u[1001];
int main(){
int n;
int count1=0;
int count2=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>u[i].name>>u[i].psw;
u[i].flag=0;
for(int j=0;j<u[i].psw.size();j++){
if(u[i].psw[j]=='1') {
u[i].psw[j]='@';
u[i].flag=1;
}
if(u[i].psw[j]=='0') {
u[i].psw[j]='%';
u[i].flag=1;
}
if(u[i].psw[j]=='l') {
u[i].psw[j]='L';
u[i].flag=1;
}
if(u[i].psw[j]=='O') {
u[i].psw[j]='o';
u[i].flag=1;
}
}
if(u[i].flag==1) count1+=1;
if(u[i].flag==0) count2+=1;
}
for(int i=0;i<n;i++){
if(u[i].flag==1){
cout<<count1<<endl;
break;
}
}
for(int i=0;i<n;i++){
if(u[i].flag==1){
cout<<u[i].name<<" "<<u[i].psw<<endl;
}
if(count2==n&&count2==1){
cout<<"There is "<<count2<<" account and no account is modified";
break;
}
if(count2==n&&count2!=1){
cout<<"There are "<<count2<<" accounts and no account is modified";
break;
}
}
return 0;
}