PAT 乙级 1081.检查密码 C++/Java
本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .
,还必须既有字母也有数字。
输入格式:
输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。
输出格式:
对每个用户的密码,在一行中输出系统反馈信息,分以下5种:
- 如果密码合法,输出
Your password is wan mei.
; - 如果密码太短,不论合法与否,都输出
Your password is tai duan le.
; - 如果密码长度合法,但存在不合法字符,则输出
Your password is tai luan le.
; - 如果密码长度合法,但只有字母没有数字,则输出
Your password needs shu zi.
; - 如果密码长度合法,但只有数字没有字母,则输出
Your password needs zi mu.
。
输入样例:
5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6
输出样例:
Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.
C++实现:
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 //1081:检查密码 5 int main() { 6 int n; 7 cin >> n; 8 string pw; 9 getchar(); 10 for (int i = 0; i < n; i++) { 11 bool flagD = false, flagL = false, flagO = false; 12 getline(cin, pw); 13 if (pw.length() < 6) cout << "Your password is tai duan le." << endl; 14 else { 15 for (int i = 0; i < pw.length(); i++) { 16 if ((pw[i] >= 'a' && pw[i] <= 'z') || (pw[i] >= 'A' && pw[i] <= 'Z')) flagL = true; 17 else if (pw[i] >= '0' && pw[i] <= '9') flagD = true; 18 else if (pw[i] == '.') continue; 19 else flagO = true; 20 } 21 if (flagO) cout << "Your password is tai luan le." << endl; 22 else if (!flagD) cout << "Your password needs shu zi." << endl; 23 else if (!flagL) cout << "Your password needs zi mu." << endl; 24 else cout << "Your password is wan mei." << endl; 25 } 26 } 27 return 0; 28 }
Java实现:
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 5 public class Main { 6 public static void main(String[] args) throws IOException { 7 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 8 int n = Integer.parseInt(in.readLine()); 9 for (int i = 0; i < n; i++) { 10 String pas = in.readLine(); 11 if (pas.length() < 6) { 12 System.out.println("Your password is tai duan le."); 13 } else { 14 int dot = 0, chara = 0, num = 0; 15 for (int j = 0; j < pas.length(); j++) { 16 char a = pas.charAt(j); 17 if (a >= '0' && a <= '9') { 18 num++; 19 } 20 if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z')) { 21 chara++; 22 } 23 if (a == '.') { 24 dot++; 25 } 26 } 27 int sum = num + chara + dot; 28 if (sum < pas.length()) { 29 System.out.println("Your password is tai luan le."); 30 } else if (num == 0 && chara != 0) { 31 System.out.println("Your password needs shu zi."); 32 } else if (chara == 0 && num != 0) { 33 System.out.println("Your password needs zi mu."); 34 } else { 35 System.out.println("Your password is wan mei."); 36 } 37 } 38 } 39 } 40 }