PATAdvanceLevel1035
题目链接:1035 Password (20 分)
题目描述如下:
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
1035 Password (20 分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N (≤1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.
Output Specification:
For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified where N is the total number of accounts. However, if N is one, you must print There is 1 account and no account is modified instead.
Sample Input 1:
3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa
Sample Output 1:
2
Team000002 RLsp%dfa
Team000001 R@spodfa
Sample Input 2:
1
team110 abcdefg332
Sample Output 2:
There is 1 account and no account is modified
Sample Input 3:
2
team110 abcdefg222
team220 abcdefg333
Sample Output 3:
There are 2 accounts and no account is modified
题目大意:
(仅翻译了题目的大致意思并未完全翻译)
题目的大致意思是在英文字符串中数字‘1’和数字 ‘0’ 容易被误认为小写的英文字母 ‘l’和大写的英文字母 ‘O’。希望你能用 ‘@’、 ‘%’、 ‘L’和小写的 ‘o’替换密码中的数字 ‘1’、数字 ‘0’、小写的 ‘l’和大写的 ‘O’。
输入
第一行输入一个正整数N(小于1000),然后有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代替(这是英语中的语法,不过个人感觉很坑)。
解题思路:
这是PAT中最简单的一类题,直接检测输入中的密码是否有需要替换的字符,如果有就进行替换并做好标记替换了多少。由于这道题很简单我就选用的是python语言进行解题以便学习python语言(本人还不太会python)。由于题目很简单,代码中的变量命名就很随意没有按照命名规则命名,请见谅。
代码如下:
1 | n = eval(input())#input函数输入的字符变量,可以用eval函数使其变为整型 |
总结
本题的解题思路很简单,本人做这道题主要是学习python语法
这道题目学习到的知识如下:
- 用range函数实现给定次数的for循环
虽然不知道怎么做是否正确,不过python中的for循环与其他语言不同,只能用这个方法代替 - 字符串处理函数
python有许多字符串处理函数,本题用到的有字符串分割函数和字符串替换函数,这样我们就可以不用自己写替换函数了
这道题学到的经验:
一定要仔细读题,单复数输出的错误我找了很久才发现。
python语言真的很强,不过本人能力太弱,这道题并没有把python的优点发挥出来,本题的代码还有很多地方可以优化和精简。
这题本人还写了C/C++代码,以下是用C/C++写的代码:
1 |
|
两种代码提交测试后发现了些许不同
下图为python代码提交后的结果

下图为C++代码提交后的结果

从测试结果可以看出python代码所需的运行时长比C++代码的运行时长要长一些,所需内存也要大一些。不过后面有一道题让我惊讶的是同样的逻辑python代码未运行超时而C++代码却运行超时了。具体情况会在后续文章中说明。
谢谢阅读,希望对你有所收获。