PAT

1035 Password

2020-04-15  本文已影响0人  胖胖的熊管家

题目

在输密码的时候,会出现一些相似的字符。所以,要替换掉它。

替换字符 替换后
1 @
l L
0 %
O o

输出:
1.被修改密码的账户数量
2.被修改后的账户信息

如果没有账户密码被修改,输出”There are N accounts and no account is modified“;
如果只有一个账户,且账户密码没被修改,则输出”There is 1 account and no account is modified“。

Sample Input1
3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa
Sample Output1
2
Team000002 RLsp%dfa
Team000001 R@spodfa
Sample Input2
1
team110 abcdefg332
Sample Output2
There is 1 account and no account is modified
Sample Input3
2
team110 abcdefg222
team220 abcdefg333
Sample Output3
There are 2 accounts and no account is modified

解法

法一:C++
思路:

第一反应就是用数组来储存,然后遍历找出那些要替换的字符switch。然后想起来C++的数组没有找index的方法,所以就想再创一个数组存index。经过实践后,发现不用index数组,直接用vector。

源代码:
#include <iostream>
#include <cstdio>
#include <math.h>
#include<vector>
#include <string.h>
#include <sstream>

using namespace std;

int main() {
    int n;
    vector <string> accountarr;
    vector <string> passwordarr;
    string account,password;
    bool flag = false;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        flag = false;
        cin>>account>>password;
        for(int j=0;j<password.length();j++){
            switch (password[j]) {
                case '1':
                    password[j] = '@';
                    flag = true;
                    break;
                case 'l':
                    password[j] = 'L';
                    flag = true;
                    break;
                case '0':
                    password[j] = '%';
                    flag = true;
                    break;
                case 'O':
                    password[j] = 'o';
                    flag = true;
                    break;
            }
        }
        if(flag){
            accountarr.push_back(account);
            passwordarr.push_back(password);
        }
    }
    
//-------------按要求输出------------
    if(accountarr.size() == 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<<accountarr.size()<<endl;
        for(int i=0;i<accountarr.size();i++){
            cout<<accountarr[i]<<" "<<passwordarr[i]<<endl;
        }
    }
    return 0;
}

知识点+坑:
这次没什么特别的知识点,主要是vector的运用。
还有就是,在看了柳神的代码后才发现我的accountarr和passwordarr可以合成一个vector。Like this:
 if(flag) {
            string temp = name + " " + s;
            v.push_back(temp);
        }

摘自https://www.liuchuo.net/archives/2063

上一篇下一篇

猜你喜欢

热点阅读