【PAT乙級】1081 檢查密碼
阿新 • • 發佈:2018-11-03
本題要求你幫助某網站的使用者註冊模組寫一個密碼合法性檢查的小功能。該網站要求使用者設定的密碼必須由不少於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.
個人理解
水題略過
程式碼實現
#include <cstdio> #include <cstring> #include <string> #include <set> #include <map> #include <cmath> #include <algorithm> #include <iostream> #define ll long long #define ep 1e-5 #define INF 0x7FFFFFFF using namespace std; int main() { // 輸入 int n; cin >> n; getchar(); // 判斷n個密碼 while (n --) { bool tailuanle = false, have_num = false, have_word = false; string password; getline(cin, password); int len = int(password.length()); // 密碼太短 if (len < 6) { cout << "Your password is tai duan le." << endl; continue; } for (int i = 0; i < len; i ++) { char c = password[i]; // 密碼太亂 if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9') && c != '.') { tailuanle = true; break; } if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { have_word = true; } else if (c >= '0' && c <= '9') { have_num = true; } } // 輸出 if (tailuanle) { cout << "Your password is tai luan le." << endl; continue; } if (!have_num) { cout << "Your password needs shu zi." << endl; } if (!have_word) { cout << "Your password needs zi mu." << endl; } if (have_num && have_word) { cout << "Your password is wan mei." << endl; } } return 0; }
總結
學習不息,繼續加油