PAT甲级 1035 Password (20 分) 题解

本文介绍了一种字符串处理算法,用于检查并替换密码中的特定字符(如'1'替换为'@','0'替换为'%'等),并统计需要修改的密码数量。通过遍历密码字符串,进行字符替换,最后比较原密码与修改后的密码是否相同来决定是否需要记录该修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述在这里插入图片描述在这里插入图片描述
\quad 字符串处理题,分别判断密码有没有需要修改的字符,按要求修改后统计修改的数量,然后就是按照要求输出即可。

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

string change(string s)
{
	string res = "";
	for (int i = 0; i < s.length(); ++i)
	{
		if(s[i]=='1') res += '@';
		else if(s[i]=='0') res += '%';
		else if(s[i]=='l') res += 'L';
		else if(s[i]=='O') res += 'o';
		else res += s[i];
	}
	return res;
}
int main(int argc, char const *argv[])
{
	int N, M;
	cin >> N;
	vector<pair<string, string> > res;
	for (int i = 0; i < N; ++i)
	{
		string name, password;
		cin >> name >> password;
		string s = change(password);
		if(s==password) continue;  // 没有修改
		res.push_back(make_pair(name, s));
	}
	M = res.size();
	if(M==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;
        return 0;
	}
	cout << M << endl;
	for (int i = 0; i < M; ++i)
	{
		cout << res[i].first << " " << res[i].second << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值