AcWing 1519. 密码

题目:

为了准备 PAT,系统不得不为用户生成随机密码。

但是有时一些数字和字母之间总是难以区分,比如 1(数字一)和 l(L 的小写),0(数字零)和 O(o 的大写)。

一种解决办法是将 1(数字一)替换为 @,将 0(数字零)替换为 %,将 l(L 的小写)替换为 L,将 O(o 的大写)替换为 o

现在,你的任务就是帮助系统检查这些用户的密码,并对难以区分的部分加以修改。

输入格式

第一行包含一个整数 N,表示用户数量。

接下来 N 行,每行包含一个用户名和一个密码,都是长度不超过 10 且不含空格的字符串。

输出格式

首先输出一个整数 M,表示已修改的用户密码数量。

接下来 M 行,每行输出一个用户名称和其修改后的密码。

用户的输出顺序和读入顺序必须相同。

如果没有用户的密码被修改,则输出 There are N accounts and no account is modified,其中 N 是用户总数。

如果 N=1,则应该输出 There is 1 account and no account is modified

数据范围

1≤N≤1000

我的解决:

#include <iostream>
using namespace std;

int main(){
    int time;
    cin>>time;
    string teamnum[1000],password[1000],answer[1000];
    for(int i=0;i<time;i++)
    {
        cin>>teamnum[i]>>password[i];
    }
    int temp=0;
    int time1=0;
    int dut=0;
    for(int m=0;m<time;m++)
    {
        string str=password[m];
        for(int n=0;n<int(str.size());n++)
        {
            if(str[n]=='1'||str[n]=='0'||str[n]=='l'||str[n]=='O')
            {
                    if(str[n]=='1')
                    {
                        str[n]='@';
                        temp=1;
                    }
                    if(str[n]=='0')
                    {
                        str[n]='%';
                        temp=1;
                    }
                    if(str[n]=='l')
                    {
                        str[n]='L';
                        temp=1;
                    }
                    if(str[n]=='O')
                    {
                        str[n]='o';
                        temp=1;
                    }
                dut=1;
            }
            
        }           
        if(dut==1)
        {
            answer[time1]=teamnum[m]+' '+str;
            time1++;
            dut=0;
        }

    }
    if(temp==0)
    {
        if(time==1)
        {
        cout<<"There is "<<time<<" account and no account is modified";
        }
        else{
        cout<<"There are "<<time<<" accounts and no account is modified";
        }
    }
    else
    {
        cout<<time1<<endl;
        for(int y=0;y<time1;y++)
        {
            cout<<answer[y]<<endl;
        }

    }
}

题解:

#include <iostream>

using namespace std;

const int N = 1010;

string name[N], pwd[N];

string change(string str)
{
    string res;

    for (auto c : str)
        if(c == '1')    res += '@';
        else if(c == '0')    res += '%';
        else if(c == 'l')    res += 'L';
        else if(c == 'O')    res += 'o';
        else res += c;

    return res;
}

int main()
{
    int n;
    cin >> n;

    int m = 0;
    for(int i = 0; i < n; i ++ )//从前往后读入每个同学的信息
    {
        string cur_name, cur_pwd;
        cin >> cur_name >> cur_pwd;//读入当前用户名和当前密码

        //判断是否修改密码(判断和修改可同时放在一个函数中)→利用函数修改掉密码中所有有歧义的字符
        string changed_pwd = change(cur_pwd);//求出更新后密码

        if(changed_pwd != cur_pwd)//如果密码被修改了
        {
            name[m] = cur_name;
            pwd[m] = changed_pwd;
            m++;
        }
    }

    //  !m  <=> m == 0
    if(!m)//如果密码没被修改过
    {
        if(n == 1) puts("There is 1 account and no account is modified");
        else printf("There are %d accounts and no account is modified\n", n);
    }
    else//密码被修改过,输出名字及密码
    {
        cout << m << endl;
        for (int i = 0; i < m; i ++ ) cout << name[i] << ' ' << pwd[i] << endl;
    }

    return 0;
}

作者:锦梨冲冲冲
链接:https://www.acwing.com/solution/content/10820/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值