美文网首页
【PAT_1035】Password

【PAT_1035】Password

作者: 6J | 来源:发表于2018-08-02 15:37 被阅读0次

    题目描述

    为了准备PAT,judge有时必须为用户生成随机密码。 问题是总是有一些令人困惑的密码,因为很难区分1(一)与l(L为小写),或0(零)与O(o为大写)。 一种解决方案是用1代替1(一),用0代替0(零),用L代替l,用o代替O. 现在,您的工作是编写一个程序来检查法官生成的帐户,并帮助juge修改令人困惑的密码。

    输入

    第一行:N(用户数<= 1000)
    随后N行:
    id(用户id<10个字符) password(原始密码<10字符)

    输出

    第一行t(t个需要修改的密码)
    之后t行:id(用户id) password(修正之后的密码)
    如果没有一个用户需要修改那么输出
    当N>1
    "There are N accounts and no account is modified"
    如果N==1则输出为
    "There are 1 account and no account is modified"

    解题思路

    本题实际上为从字符串中查找是否有‘1’,‘l','0','O',并将将这些字母相应的换成@,L,%,o;

    代码1

    #include<stdio.h>
    #include<iostream>
    #include<string>
    #include<map>
    #include<vector>
    using namespace std;
    int main() {
        map<char, char>m;
        m['1'] = '@';
        m['0'] = '%';
        m['l'] = 'L';
        m['O'] = 'o';
        int n;
        scanf("%d", &n);
        string id,str;
        int  t = 0;
        vector<pair<string, string> > res;
        for (int i = 0; i < n; i++) {
            cin>>id >> str;
            bool flag = false;
            for (int j = 0; j < str.size(); j++) {
                if (m.find(str[j])!=m.end()) {
                    flag = true;
                    str[j] = m[str[j]];
                }
            }
            if (flag) {
                res.push_back(pair<string, string> (id, str));
                t++;
            }
        }
        if (t==0){
          if(n>1)printf("There are %d accounts and no account is modified", n);
          else{
            printf("There is 1 account and no account is modified");
        }
        }else {
            cout << t << endl;
            for (int i = 0; i < t; i++) {
                cout << res[i].first << " " << res[i].second << endl;
            }
        }
        return 0;
    }
    
    

    代码2

    发现可以不用map存储,直接判断str[i]

    #include<stdio.h>
    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    int main() {
        int n;
        scanf("%d", &n);
        string id,str;
        int  t = 0;
        vector<pair<string, string> > res;
        for (int i = 0; i < n; i++) {
            cin>>id >> str;
            bool flag = false;
            for (int j = 0; j < str.size(); j++) {
                switch(str[j]){
                 case '0':
                      str[j]='%';
                      flag =true;
                      break;
                 case '1':
                      str[j]='@';
                      flag =true;
                      break;    
                 case 'l':
                      str[j]='L';
                      flag =true;
                      break;
                 case 'O':
                      str[j]='o';
                      flag =true;
                      break;
                default:
                      break;
                }
            }
            if (flag) {
                res.push_back(pair<string, string> (id, str));
                t++;
            }
        }
        if (t==0){
          if(n>1)printf("There are %d accounts and no account is modified", n);
          else{
            printf("There is 1 account and no account is modified");
        }
        }else {
            cout << t << endl;
            for (int i = 0; i < t; i++) {
                cout << res[i].first << " " << res[i].second << endl;
            }
        }
        return 0;
    }
    
    
    

    相关文章

      网友评论

          本文标题:【PAT_1035】Password

          本文链接:https://www.haomeiwen.com/subject/iifsvftx.html