天天看點

PAT A1035 Password

題目描述:

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 (≤), 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組資料,第一個字元串為資料編号,第二個字元串為需要修改的密碼。如果第二個字元串中有1就替換成@,0替換成%,l替換成L,O替換成o

如果有密碼被修改,輸出修改的數量已經修改過的密碼的編号和修改後的密碼。如果沒有修改,則需要輸出(注意輸出的單複數!)

There is 1 account and no account is modified          或者      
There are N accounts and no account is modified      

解題思路:

  (1)将編号和密碼用一個結構體來封裝,同時包含一個是否修改的标志。

  (2)對輸入的資料進行處理,遇到需要替換的就替換,并把修改标志改為true,否則的話修改标志位false。

  (3)用一個計數器記錄修改的數量,初值為n,遇到一個未修改的,就将num--,最終num如果為0,說明沒有元素被修改,num不為0時,輸出num,并且輸出修改标志位為true的資料。

#include<iostream>

using namespace std;

struct Password {
    char id[12];
    char pass[12];
    bool isChange;
}p[1001];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> p[i].id >> p[i].pass;
    }
    int num = n;//計算有多少個被修改
    for (int i = 0; i < n; i++) {
        //對密碼進行修改
        for (int j = 0; j < strlen(p[i].pass);j++) {
            if (p[i].pass[j] == '1') {
                p[i].pass[j] = '@';
                p[i].isChange = true;
            }
            else if (p[i].pass[j] == '0') {
                p[i].pass[j] = '%';
                p[i].isChange = true;
            }
            else if (p[i].pass[j] == 'l') {
                p[i].pass[j] = 'L';
                p[i].isChange = true;
            }
            else if (p[i].pass[j] == 'O') {
                p[i].pass[j] = 'o';
                p[i].isChange = true;
            }
        }
        if (p[i].isChange != true) {
            //沒有被修改的話,num--
            p[i].isChange = false;
            num--;
        }
    }
    
    if (num == 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;
        }
    }
    else {
        //有資料被修改
        cout << num << endl;
        for (int i = 0; i < n; i++) {
            //隻輸出被修改的内容
            if (p[i].isChange) {
                cout << p[i].id << " " << p[i].pass << endl;
            }
        }
    }
    system("pause");
    return 0;
}