PAT-ADVANCED1035——Password

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED

原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805454989803520

题目描述:

PAT-ADVANCED1035——Password

题目翻译:

1035 密码

为了准备PAT,判题者有时必须为用户生成随机密码。问题是总是有一些令人困惑的密码,因为很难区分1(一)与l(小写L),或0(零)与O(大写o)。一种解决方案是用@代替1(一),用%代替0(零),用L代替l,用o代替O。现在,你的任务是编写一个程序来检查判题者生成的帐户,并帮助判题者修改令人困惑的密码。

输入格式:

每个输入文件包含一个测试用例。 每个测试用例包含一个正整数N(<= 1000),后面是N行用户。 每个用户都包含一个用户名和一个密码,两者都是不超过10个字符且没有空格的字符串。

输出格式:

对于每个测试用例,首先打印已修改的帐户数M,然后在以下M行中打印修改后的帐户信息,即用户名和相应的修改密码。 帐户必须以与读入时相同的顺序打印。如果没有修改帐户,则在一行中打印There are N accounts and no account is modified,其中N是帐户总数。但是,如果N为1,则必须打印There is 1 account and no account is modified。

输入样例1:

3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa

输出样例1:

2
Team000002 RLsp%dfa
Team000001 [email protected]

输入样例2:

1
team110 abcdefg332

输出样例2:

There is 1 account and no account is modified

输入样例3:

2
team110 abcdefg222
team220 abcdefg333

输出样例3:

There are 2 accounts and no account is modified

知识点:字符串

思路:用一个结构体来存储用户名和和密码信息

时间复杂度是O(N)。空间复杂度是O(n),其中n为需要修改密码的账户数量。

C++代码:

#include<iostream>
#include<cstring>
#include<vector>

using namespace std;

struct message{
	char user[11];
	char password[11];
};

bool needModified(char* password, int N);

int main(){
	int N;
	scanf("%d", &N);
	char user[11], password[11];
	vector<message> result;
	message tempMessage;
	for(int i = 0; i < N; i++){
		scanf("%s %s", user, password);
		if(needModified(password, strlen(password))){
			for(int i = 0; i < strlen(password); i++){
				if(password[i] == '1'){
					password[i] = '@';
				}else if(password[i] == '0'){
					password[i] = '%';
				}else if(password[i] == 'l'){
					password[i] = 'L';
				}else if(password[i] == 'O'){
					password[i] = 'o';
				}
			}
			strcpy(tempMessage.user, user);
			strcpy(tempMessage.password, password);
			result.push_back(tempMessage);
		}
	}
	if(result.size() == 0 && N == 1){
		printf("There is 1 account and no account is modified\n");
	}else if(result.size() == 0 && N > 1){
		printf("There are %d accounts and no account is modified\n", N);
	}else{
		printf("%d\n", result.size());
		for(int i = 0; i < result.size(); i++){
			printf("%s %s\n", result[i].user, result[i].password);
		}
	}
	return 0;
}

bool needModified(char* password, int N){
	for(int i = 0; i < N; i++){
		char temp = *(password + i);
		if(temp == '1' || temp == 'l' || temp == '0' || temp == 'O'){
			return true;
		}
	}
	return false;
}

C++解题报告:

PAT-ADVANCED1035——Password