pat密码

思路:题目比较简单,先定义两个字符串数组来存储修改后的账号与密码,修改密码我是将字符串循环发现有要修改的字符修改,用count计数,当每次循环快结束时判断是否要存储,最后判断count是否为0和n是否为1来输出结果

#include<iostream>
#include <string>

using namespace std;
int main()
{
	int n;
	cin >> n;
	string id, pass_id;
	string new_id[1001], new_pass[1001];
	int count = 0;
	int j = 0;
	for(int x=0;x<n;x++)
	{
		cin >> id >> pass_id;
		string s= pass_id;
		int i = 0;
		int flag = 1;
		while (pass_id[i] != '\0')
		{
			if (pass_id[i] == '1')
				pass_id[i] = '@';
			else if (pass_id[i] == '0')
				pass_id[i] = '%';
			else if (pass_id[i] == 'l')
				pass_id[i] = 'L';
			else if (pass_id[i] == 'O')
				pass_id[i] = 'o';
			i++;
		}
		if (s != pass_id)
		{
			count++;
			new_id[j] = id;
			new_pass[j++] = pass_id;
		}
	}
	if (count)
	{
	    cout<<count<<endl;
		for (int i = 0; i < count; i++)
		{
			cout << new_id[i] << " " << new_pass[i] << endl;
		}
	}
	else if(n>1)
		cout << "There are " << n << " accounts and no account is modified";
	else
	{
	    cout<<"There is 1 account and no account is modified";
	}
	return 0;
}

更优解:

我觉得自己定义变量时总会不把能相同计数的变量减少,看了y总的代码觉得count和j可以当成同一个,并且可以定义一个函数来实现修改字符串的功能,在修改字符串时我用的是遍历字符串,看y总的是采取连接字符串来修改。(这里就没去重新写代码)

for(auto c:s)

res+=c

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值