當你試圖登錄某個系統卻忘了密碼時,系統一般只會允許你嘗試有限多次,當超出允許次數時,賬號就會被鎖死。本題就請你實現這個小功能。
輸入格式:
輸入在第一行給出一個密碼(長度不超過 20 的、不包含空格、Tab、回車的非空字符串)和一個正整數 N(≤ 10),分別是正確的密碼和系統允許嘗試的次數。隨後每行給出一個以回車結束的非空字符串,是用戶嘗試輸入的密碼。輸入保證至少有一次嘗試。當讀到一行只有單個 # 字符時,輸入結束,並且這一行不是用戶的輸入。
輸出格式:
對用戶的每個輸入,如果是正確的密碼且嘗試次數不超過 N,則在一行中輸出 Welcome in
,並結束程序;如果是錯誤的,則在一行中按格式輸出 Wrong password: 用戶輸入的錯誤密碼
;當錯誤嘗試達到 N 次時,再輸出一行 Account locked
,並結束程序。
輸入樣例 1:
Correct%pw 3
correct%pw
Correct@PW
whatisthepassword!
Correct%pw
#
輸出樣例 1:
Wrong password: correct%pw
Wrong password: Correct@PW
Wrong password: whatisthepassword!
Account locked
輸入樣例 2:
cool@gplt 3
coolman@gplt
coollady@gplt
cool@gplt
try again
#
輸出樣例 2:
Wrong password: coolman@gplt
Wrong password: coollady@gplt
Welcome in
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string temp, password;
int n, sum = 0;
cin >> password >> n;
getchar();
while (1) {
getline (cin, temp);
sum++;
if (temp == "#") {
break;
}
if (sum <= n && temp == password) {
printf ("Welcome in\n");
break;
}
if (sum <= n && temp != password) {
cout << "Wrong password: " << temp << endl;
}
if (sum == n) {
printf ("Account locked\n");
break;
}
}
return 0;
}