1067 test code (20 points)
When you try to log on to a system but forgot the password, the system usually only allow you to try a limited number of times when outside the permitted number of times, the account will be locked. This question will ask you this simple feature.
Input formats:
Enter a password is given (no longer than 20, no spaces, Tab, Enter, non-empty string), and a positive integer N (in the first row ≤ 10), respectively, and the system allows the correct password attempts frequency. Each subsequent row is given a non-empty string ends with a carriage return, the user attempts to enter a password. Input ensure that at least the first attempt. When only a single # character read one line, enter an end, and this line is not the user's input.
Output formats:
Each user's input, and if the correct password attempts does not exceed N, then the output line Welcome in
, and the program ends; if it is incorrect, the output in the format line Wrong password: 用户输入的错误密码
; when incorrect attempts reaches N times, then the output line Account locked
, and the program ends.
Sample Input 1:
Correct%pw 3
correct%pw
Correct@PW
whatisthepassword!
Correct%pw
#
Output Sample 1:
Wrong password: correct%pw
Wrong password: Correct@PW
Wrong password: whatisthepassword!
Account locked
Sample Input 2:
cool@gplt 3
coolman@gplt
coollady@gplt
cool@gplt
try again
#
Output Sample 2:
Wrong password: coolman@gplt
Wrong password: coollady@gplt
Welcome in
思路很简单:
要注意的地方1.用户会输入空格2.用户输入的密码位数不确定
首次通过代码:
1 #include<stdio.h> 2 #include<string.h> 3 4 int main(){ 5 char password[30]; 6 char input_password[300]; 7 int try_num; 8 scanf("%s %d",password,&try_num); 9 getchar(); 10 for(int i=0;i<try_num;i++){ 11 gets(input_password); 12 if(strcmp("#",input_password)==0) return 0; 13 else if(strcmp(password,input_password)==0) { 14 printf("Welcome in"); 15 return 0; 16 } 17 else { 18 printf("Wrong password: %s\n",input_password); 19 } 20 } 21 printf("Account locked"); 22 return 0; 23 }