1035 Password 之 字符串修改密码

1. 题目

在这里插入图片描述

2. 分析

题意:因为在设置密码时,0与O,l与1难以区分,所以要你根据现有的密码,把其中的0、1、l、O改成%、@、L、o,如果密码没改过根据账户的数量输出对应的语句。

3. 解题过程

  1. 这里提供两种思路:一是,根据给出的密码,逐个字符分析是否在修改的范围,若在则修改,不在则不修改。二是,判断每个字符是否在修改的范围,是则修改并记录,否则不记录。
  2. 请看两种代码:
#include<string>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int main()
{
	int n; scanf("%d", &n);
	int cnt = 0;
	vector<string> st1, st2;
	for(int i = 0; i < n; i++)
	{
		string a, b;
		cin >> a >> b;
		string c;
		for(int j = 0; j < b.length(); j++)
		{
			if(b[j] == '1')
			{
				c += "@";
			}
			else if (b[j] == '0')
			{
				c += "%";
			}
			else if(b[j] == 'l')
			{
				c += "L";
			}
			else if(b[j] == 'O')
			{
				c += "o";
			}
			else c += b.substr(j, 1);
		}
		if(b == c) continue;
		st1.push_back(a);
		st2.push_back(c);
		cnt++;
	}
	if(cnt != 0)
	{
		printf("%d\n", cnt);
		for(int i = 0; i < st1.size(); i++)
		{
			printf("%s %s\n", st1[i].c_str(), st2[i].c_str());
		}
	}
	else if(cnt == 0 && n != 1) printf("There are %d accounts and no account is modified", n);
	else if(cnt == 0 && n == 1) printf("There is 1 account and no account is modified");
	return 0;
}

第二种:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int n; scanf("%d", &n);
	vector<string> v;
	for(int i = 0; i < n; i++)
	{
		string name, s;
		cin >> name >> s;
		int len = s.length(), flag = 0;
		for(int j = 0; j < len; j++)
		{
			switch(s[j])
			{
				case '1':
					s[j] = '@';
					flag = 1;
					break;
				case '0':
					s[j] = '%';
					flag = 1;
					break;
				case 'l':
					s[j] = 'L';
					flag = 1;
					break;
				case 'O':
					s[j] = 'o';
					flag = 1;
					break;
			}
		}
		if(flag)
		{
			string temp = name + " " + s;
			v.push_back(temp);
		}
	}
	int cnt = v.size();
	if(cnt != 0)
	{
		printf("%d\n", cnt);
		for(int i = 0; i < cnt; i++)
		{
			cout << v[i] << endl;
		}
	}
	else if(n == 1) printf("There is 1 account and no account is modified");
	else printf("There are %d accounts and no account is modified", n);
	return 0;
 } 

后者相对来说会更好,因为判断的次数少一点。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值