PTA 乙级 1067试密码 Java代码
PAT 乙级 1067 试密码
当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死。本题就请你实现这个小功能。
题目要求
输入格式:
输入在第一行给出一个密码(长度不超过 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
输入样例 2:
cool@gplt 3
coolman@gplt
coollady@gplt
cool@gplt
try again
#
输出样例 2:
Wrong password: coolman@gplt
Wrong password: coollady@gplt
Welcome in
解题代码
Java代码
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main03 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] input = bf.readLine().split(" "); String pwd = input[0]; int n = Integer.parseInt(input[1]); int count = 0; String str; do { str = bf.readLine(); if (count == n) { System.out.println("Account locked"); break; } else { if ("#".equals(str)) { break; } if (!pwd.equals(str)) { System.out.println("Wrong password: " + str); } else { System.out.println("Welcome in"); break; } count++; } } while (!"#".equals(str)); } }
整理之后的代码:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] input = bf.readLine().split(" "); String pwd = input[0]; int n = Integer.parseInt(input[1]); int count = 0; String str; while (true) { str = bf.readLine(); if (count == n) { System.out.println("Account locked"); break; } if ("#".equals(str)) { break; } if (pwd.equals(str)) { System.out.println("Welcome in"); break; } else { System.out.println("Wrong password: " + str); } count++; } } }
解题思路:
在解题过程中需要考虑两种特殊情况
第一种是直接#结束,不输入密码的情况,输出就结束程序;
第二种是输入次数用完之后,使用#输入,会输出"Account locked",而不是结束程序。
第一种输入:
123456 3
#
第一种输出:
第二种输入:
123456 3
111111
222222
333333
#
第二种输出:
Wrong password: 111111
Wrong password: 222222
Wrong password: 333333
Account locked
一开始出错的原因是测试用例2和测试用例4不能通过
测试用例4应该是第二种情况
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main03 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] input = bf.readLine().split(" "); String pwd = input[0]; int n = Integer.parseInt(input[1]); int count = 0; String str; do { str = bf.readLine(); if ("#".equals(str)) { break; } if (count == n) { System.out.println("Account locked"); break; } else { if (!pwd.equals(str)) { System.out.println("Wrong password: " + str); } else { System.out.println("Welcome in"); break; } count++; } } while (!"#".equals(str)); } }
这个程序会出现测试用例4不能通过

在IDEAJ中的运行情况:

以下代码会出现测试用例2不能通过的情况,就是如果在没有超过错误次数时,使用#结束输入的情况,同时也给出了在IDEAJ中的运行情况
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main03 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] input = bf.readLine().split(" "); String pwd = input[0]; int n = Integer.parseInt(input[1]); int count = 0; String str; do { str = bf.readLine(); /*if ("#".equals(str)) { break; }*/ if (count == n) { System.out.println("Account locked"); break; } else { if (!pwd.equals(str)) { System.out.println("Wrong password: " + str); } else { System.out.println("Welcome in"); break; } count++; } } while (!"#".equals(str)); } }

在IDEAJ中的运行情况:

分类:
PTA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通