PAT(A) 1035. Password (20)

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
#include <cstdio>
#include <cstring>

struct node{        //name, password 同属相同用户,可想到用结构体char name[15], password[15];
    bool ischange;  //ischange==true 表示 password 已修改
}user[1005];

//judge()判断pp的password是否需要修改,是的话对其进行修改并令cnt+1
void judge(node& u, int& cnt){   //参数使用引用&, 则可被修改int len=strlen(u.password);
    for(int i=0; i<len; i++){
        //判断password中是否有字母满足修改的条件
        if(u.password[i]=='1'){
            u.password[i]='@';
            u.ischange=true;
        }
        else if(u.password[i]=='0'){
            u.password[i]='%';
            u.ischange=true;
        }
        else if(u.password[i]=='l'){
            u.password[i]='L';
            u.ischange=true;
        }
        else if(u.password[i]=='O'){
            u.password[i]='o';
            u.ischange=true;
        }
    }
    //如果u的password已修改,令计数器cnt+1
    if(u.ischange==true)
        cnt++;
}

int main()
{
    int n, cnt=0;  //cnt记录需要修改的password的个数
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        scanf("%s %s", user[i].name, user[i].password);
        /* 注:以下几种输入都会出错(段错误/结果错误)
        fgets(user[i].name, 10, fp);    //输入指定位数,但位数未知
        fgets(user[i].password, 10, fp);

        gets(user[i].name);             //输入一整行,包括了空格,故出错
        gets(user[i].password);
        */
        user[i].ischange=false;        //初始化所有密码为:未修改
    }

    //判断user[i].password是否需要修改,需要则对其进行修改
    for(int i=0; i<n; i++)
        judge(user[i], cnt);

    if(cnt==0){      //没有password需要修改
        //注:输出时注意:account的单复数,以及is/are的使用
        if(n==1)
            printf("There is %d account and no account is modified", n);
        else
            printf("There are %d accounts and no account is modified", n);
    }
    else{            //有password需要修改
        printf("%d
", cnt);    //修改了的password个数
        for(int i=0; i<n; i++){
            //如果user[i].password需要修改,则输出它的name和password
            if(user[i].ischange==true)
                printf("%s %s
", user[i].name, user[i].password);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/claremore/p/6548891.html