题目要求:
给定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;
}
发表回复