當你試圖登錄某個系統卻忘了密碼時,系統一般只會允許你嘗試有限多次,當超出允許次數時,賬號就會被鎖死。本題就請你實現這個小功能。
輸入格式:
輸入在第一行給出一個密碼(長度不超過 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
//這道題的坑在于scanf輸出的str和單個字符的strcmp會出現不等的情況 //但是scanf輸入的字符串是可以和str進行比較的 //以后還是用c++中的string比較好用點 #include<cstdio> #include<cstring>using namespace std; const int maxn = 1000; char password[maxn],str[maxn];int main(){int n;scanf("%s %d",password,&n);//getchar();int cnt = 0;while(1){getchar();scanf("%[^\n]",str);if(str[0]=='#'&&str[1]=='\0') break;cnt++;if( cnt <= n && strcmp(str,password) == 0){printf("Welcome in\n");break;}else if( cnt <= n && strcmp(str,password) != 0){printf("Wrong password: %s\n",str);if(cnt == n){printf("Account locked\n");break;}}}return 0; }
//有問題的 #include<cstdio> #include<cstring>const int maxn = 30; char password[maxn],str[maxn];int main(){int n;scanf("%s %d",password,&n);//getchar();int cnt = 0;while(1){getchar();scanf("%s",str);cnt++;if(strcmp(str,"#") == 0) break;if( cnt <= n && strcmp(str,password) == 0){printf("Welcome in");break;}else if( cnt <= n && strcmp(str,password) != 0){printf("Wrong password: %s\n",str);}if(cnt == n){printf("Account locked\n");break;}}return 0; }
?