pat 乙级 1067. 试密码(20)

1067. 试密码(20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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

输入格式:

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


第一 字符串做法:


#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <ctime>

using namespace std;
// 字符串的做法;
int main()
{
    int n;
    string pass,str;
    cin>>pass>>n;
    cin.get();
    while(n--)
    {
        getline(cin,str);
        if(str=="#") return 0;
        else if(str==pass)
        {
            cout<<"Welcome in"<<endl;return 0;
        }
        else
        {
            cout<<"Wrong password: "<<str<<endl;
        }
    }
    cout<<"Account locked"<<endl;
    return 0;
}


第二种 字符数组 做法:


#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <ctime>

using namespace std;

// 字符数组的做法 
int main() 
{
    char s[20],c[100005];
    int n,num=0;
    scanf("%s%d",s,&n);
    getchar(); // 吃掉换行 
    for (int i=0;;i++) {
      gets(c);  // 因为试的密码没说不允许空格 所以用gets
      if(num>=n) {
        cout<<"Account locked"<<endl;
        break;
      }

      if (strcmp(c,"#")==0)
      {
        break;
      }

      if (strcmp(s,c)!=0) {
        num++;
        cout<<"Wrong password: "<<c<<endl;
      }
      if (strcmp(s,c)==0) {
      cout<<"Welcome in"<<endl;
      break;
      }
    }
    return 0;
}

提交代码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值