PTA(Basic Level) 1067:试密码 (C语言实现)
分析:
结束程序只有两种方法:
①输入的串为"#";②在尝试次数内输入正确的密码。
如果在尝试次数内不能输入正确的密码,账户锁住,但不结束程序,仍然可以输入密码,直到输入"#"为止。
#include<stdio.h>
#include<string.h>
int main(){
char password[50],str[50];
int n;
scanf("%s%d",password,&n);
getchar();
while(1)
{
gets(str);
if(strcmp(str,"#") == 0)//结束程序
break;
if(n>0 && strcmp(str,password) == 0)
{
printf("Welcome in\n");
break;
}
else if(n >0 && strcmp(str,password) != 0)
{
printf("Wrong password: %s\n",str);
if(n == 1)
printf("Account locked\n");
n--;
}
}
return 0;
}