PAT 甲级 1035 Password (20分)

原题链接1035 Password (20分)

题目大意:

1 -> @
0 -> %
l -> L
O -> o

让我们对输入的单词中,存在这些字母进行替换,如上。有这种字母的单词,就代表不合格的单词。

输出:如果更改了,就输出更改了的 username 和 password;如果没更改 就输出格式化的输出。

分析:

比较简单,直接模拟。重点是感受 string 在字符串处理中的灵活。

这里用到了 string 的 assign函数
name_out[j].assign(name);
代表在 name_out[ ] 数组的末尾拼接一个 string 类型的值。

满分代码:

#include <iostream>
#include <string.h>
#include <algorithm>
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;
const int MAXN = 1e3+10;
/*
	1s -- 1e7 ~ 1e8
	1 -> @
	0 -> %
	l -> L
	O -> o
*/ 
string change(string str) {
	string res;
	for(auto c: str) {
		// 直接拼接 string
		if(c == '1')	res += '@';
		else if(c == '0')    res += '%';
		else if(c == 'l')    res += 'L';
		else if(c == 'O')	 res += 'o';
		else    res += c; 
	}
	return res;
}
int main() {
	int n, ans = 0;
	string name_out[1010], pwd_out[1010];
	int j = 0;
	cin >> n;
	for(int i = 1; i <= n; i++) {
		string name, pwd;
		int flag = 0;
		cin >> name >> pwd;
		if(change(pwd) != pwd) {
			ans++;
			name_out[j].assign(name);
			pwd_out[j].assign(change(pwd));
			j++;
		}
	}
	if(!ans) { 
		// 1 account and no account is modified
		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);
		}
	} else {
		cout << ans << endl;
		for(int k = 0; k < ans; k++) {
			cout << name_out[k] << " " << pwd_out[k] << endl;
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值