欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT A1035 Password

程序员文章站 2022-07-14 07:59:12
...

前言

传送门

正文

PAT A1035 Password
思路

详见代码注释,有关find()和replace()函数,也贴在代码下面,这两个函数以及和substr()在解字符串类型的题时,作用非常大。比如通过find()和substr()函数,我们就可以自己实现split()函数用于分割字符串,详见——>PAT B1009 说反话

参考题解

#include<iostream>
#include<string>
using namespace std;
/*
用一个二维字符串数组str存储输入的值,一个bool数组flag来表示对应的密码是否
被修改过 ,遍历密码字符串,将其中的'1'替换为'@','0'替换为'%',l替换为'L'
而'O'替换为o。若该字符串中有上述字符,则对应的flag设为true;这里需要
注意用变量定义数组长度的时候不能同时对数组进行初始化,需要在定义之后再初始化 
*/
int main(){
	int n,count=0;//count表示需要修改的数量 
	cin>>n;
	//注意用变量定义数组长度的时候不能同时对数组进行初始化,需要在定义之后再初始化 
	bool flag[n];
	for(int i=0;i<n;i++){
		flag[i]=false;
	}
	string str[n][2];
	for(int i=0;i<n;i++){
		for(int j=0;j<2;j++){//读入字符串 
			cin>>str[i][j];
		}
		while(str[i][1].find("1")!=string::npos){//循环查找是否有"1" 
			str[i][1]=str[i][1].replace(str[i][1].find("1"),1,"@");
			flag[i]=true;
		} 
		while(str[i][1].find("0")!=string::npos){
			str[i][1]=str[i][1].replace(str[i][1].find("0"),1,"%");
			flag[i]=true;
		}
		while(str[i][1].find("l")!=string::npos){
			str[i][1]=str[i][1].replace(str[i][1].find("l"),1,"L");
			flag[i]=true;
		}
		while(str[i][1].find("O")!=string::npos){
			str[i][1]=str[i][1].replace(str[i][1].find("O"),1,"o");
			flag[i]=true;
		}
	}
	for(int i=0;i<n;i++){//计算需要修改的密码数量 
		if(flag[i])count++; 
	} 
	if(count==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<<count<<endl;
		for(int i=0;i<n;i++){
			if(flag[i]==true)cout<<str[i][0]<<" "<<str[i][1]<<endl; 
		} 
	}
	return 0;
} 
string& replace (size_t pos, size_t len, const string& str) 
用str 替换指定字符串从起始位置pos开始长度为len 的字符
EG:

#include<iostream>
using namespace std;
int main(){
	string line="hello world, i love C++!";
	string s = line.replace(line.find("i"), 1, "haha");
	cout<<s<<endl;
	return 0;
}

PAT A1035 Password

PAT A1035 Password