描述
當(dāng)你試圖登錄某個(gè)系統(tǒng)卻忘了密碼時(shí),系統(tǒng)一般只會(huì)允許你嘗試有限多次盆繁,當(dāng)超出允許次數(shù)時(shí)拢驾,賬號(hào)就會(huì)被鎖死。本題就請(qǐng)你實(shí)現(xiàn)這個(gè)小功能改基。
輸入格式:
輸入在第一行給出一個(gè)密碼(長(zhǎng)度不超過(guò)20的、不包含空格咖为、Tab秕狰、回車的非空字符串)和一個(gè)正整數(shù)N(<= 10),分別是正確的密碼和系統(tǒng)允許嘗試的次數(shù)躁染。隨后每行給出一個(gè)以回車結(jié)束的非空字符串鸣哀,是用戶嘗試輸入的密碼。輸入保證至少有一次嘗試吞彤。當(dāng)讀到一行只有單個(gè)#字符時(shí)我衬,輸入結(jié)束,并且這一行不是用戶的輸入饰恕。
輸出格式:
對(duì)用戶的每個(gè)輸入挠羔,如果是正確的密碼且嘗試次數(shù)不超過(guò)N,則在一行中輸出“Welcome in”埋嵌,并結(jié)束程序破加;如果是錯(cuò)誤的,則在一行中按格式輸出“Wrong password: 用戶輸入的錯(cuò)誤密碼”雹嗦;當(dāng)錯(cuò)誤嘗試達(dá)到N次時(shí)范舀,再輸出一行“Account locked”合是,并結(jié)束程序。
輸入樣例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
C語(yǔ)言
#include <stdio.h>
#include <string.h>
#define STOP "#"
int main(void)
{
char pw[20];
char input[200];
int n;
scanf("%s %d", &pw, &n);
getchar(); // 吃掉 '\n'
int count = 1;
while (1){
gets(input); // 錯(cuò)誤的密碼可能有空格
if (strcmp(input, STOP) == 0){
break;
}
if (strcmp(pw, input) == 0){
printf("Welcome in\n");
break;
} else {
printf("Wrong password: %s\n", input);
}
if (count == n){
printf("Account locked\n");
break;
}
count++;
}
return 0;
}