PAT 解题报告 1035. Password (20)

1035. 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.

题目是说给定Password, 把Password里面的一些特定字母按照规则替换掉: replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. 如果有替换过就把那些替换过的都输出, 如果没有替换过, 那么输出 ”There are N accounts and no account is modified” , 注意当N是1的时候, account没有复数形式, 输出 ”There are 1 account and no account is modified” .

1035. Password 算法分析:

模拟题, 直接按照题目给出的规则做, 代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

int N;
string account;
string password;
vector<string> results;

bool gao(string &s) {
    bool flag = false;
    for(string::size_type ix=0; ix!=s.size(); ++ix) {
        if('1' == s[ix])
        { s[ix] = '@'; flag = true; }
        else if('0' == s[ix])
        { s[ix] = '%'; flag = true; }
        else if('l' == s[ix])
        { s[ix] = 'L'; flag = true; }
        else if('O' == s[ix])
        { s[ix] = 'o'; flag = true; }
        else
            ;
    }
    return flag;
}

int main(void) {
    cin>>N;
    for(int i=0; i<N; ++i) {
        cin>>account>>password;
        if(gao(password)) results.push_back(account+" "+password);
    }
    if(results.empty()) {
        if(1==N) 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<<results.size()<<endl;
        for(vector<string>::size_type ix=0; ix != results.size(); ++ix)
            cout<<results[ix]<<endl;
    }
    return 0;
}

1035. Password 注意点:

输入的Password如果都没有替换过, 那么输出 ”There are N accounts and no account is modified” , 注意当N是1的时候, account没有复数形式, 输出 ”There are 1 account and no account is modified” .

Written on December 28, 2013