zoukankan      html  css  js  c++  java
  • [PAT乙级题解]——试密码

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

    输入格式:

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

    很气了,坑了我那么久是因为如果输入aaa bbb的话,cin输入肯定是作为两个字符串处理了,所以改成了getchar()加getline(cin,str);这样输入字符串中间有个空格也会单做一个字符串来处理。。。

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <math.h>
    #include <set>
    using namespace std;
    
    //在VS里scanf必须用scanf_s这样的
    void no_67() {
        string pwd;
        int N;
        cin >> pwd >> N;
        string str;
        int cnt = 0;
        getchar();
        while (1) {
            getline(cin, str);
            if (str == "#") break;
            cnt++;
            if (cnt <= N && str == pwd) {
                cout << "Welcome in";
                break;
            }
            else if (cnt <= N && str != pwd) {
                cout << "Wrong password: " << str << endl;
                if (cnt == N) {
                    cout << "Account locked";
                    break;
                }
            }
        }
        
    }
    
    
    void no_67_1() {
        string pwd;
        int N;
        cin >> pwd >> N;
        string str;
        int cnt = 0;
        getchar();
        for(int i = 0;i<N;i++){
            getline(cin, str);
            if (str == "#") break;
            if (str == pwd) {
                cout << "Welcome in";
                break;
            }
            else if ( str != pwd) {
                cout << "Wrong password: " << str << endl;
                if (i == N-1) {
                    cout << "Account locked";
                    break;
                }
            }
        }
    
    }
    
    
    int main(int argc, char** argv) {
        no_67_1();
        return 0;
    }
     
  • 相关阅读:
    简单的使用rabbitmq的例子
    装饰者模式
    Zinterstore 命令
    策略模式
    简单工厂模式
    getFields()与getDeclaredFields()区别
    解决Jetty下EL版本冲突的问题
    网站ssl配置
    webservice 项目 配置wsdl
    js前端clone的要诀
  • 原文地址:https://www.cnblogs.com/MoisAbby/p/8571927.html
Copyright © 2011-2022 走看看