关于算法-数据结构:PAT甲级1035-Password

27次阅读

共计 1126 个字符,预计需要花费 3 分钟才能阅读完成。

题目要求:

给定 n 集体的用户名和明码,依照要求替换明码中的特定字符(1 (one) by @, 0 (zero) by %, l by L, and O by o), 如果不存在须要批改的明码,则须要依据单复数输入 There is(are) 1(n) account(s) and no account is modified

算法思路:

在输出明码的时候就遍历该明码的每一个字符,应用 flag 标记该明码是否须要批改, 如果批改过就应用 vector<string> modified 保留批改过的字符串 (username+password),其大小能够用来判断批改过的明码的多少,最初依照 modified.size() 是否为 0 来进行不同的输入即可。

提交后果:

AC 代码:
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>

using namespace std;

vector<string> modified;// 用于保留批改过的字符串(username+password),其大小能够用来判断批改过的明码的多少 

int main(){
    int n;// 明码总数
    cin>>n;
    string username,password;
    for(int i=0;i<n;++i){cin>>username>>password;// 批改明码中的 1 (one) by @, 0 (zero) by %, l by L, and O by o 
        bool flag = false;// 判断是否被批改 
        for(int j=0;j<password.size();++j){if(password[j]=='1'){password[j] = '@';
                flag = true;
            }
            if(password[j]=='0'){password[j] = '%';
                flag = true;
            }
            if(password[j]=='l'){password[j] = 'L';
                flag = true;
            }
            if(password[j]=='O'){password[j] = 'o';
                flag = true;
            }
        }
        if(flag){// 批改过了 
            modified.push_back(username+" "+password);
        }
    }
    if(modified.size()==0){if(n>1){cout<<"There are"<<n<<"accounts and no account is modified";}else{cout<<"There is 1 account and no account is modified";}
    }else{cout<<modified.size()<<endl;
        for(auto s:modified){cout<<s<<endl;}
    }
    return 0;
}

正文完
 0