【PAT_1035】Password

题目描述

为了准备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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值