1. 程式人生 > >PAT-ADVANCED1035——Password

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++解題報告: