1067 试密码 (20 分)

当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死。本题就请你实现这个小功能。

输入格式:

输入在第一行给出一个密码(长度不超过 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;
}
复制代码

 

posted @   王清河  阅读(160)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示