1081 檢查密碼 (15 分)
- 2019 年 11 月 8 日
- 筆記
版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/shiliang97/article/details/99650827
1081 檢查密碼 (15 分)
本題要求你幫助某網站的用戶註冊模組寫一個密碼合法性檢查的小功能。該網站要求用戶設置的密碼必須由不少於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<iostream> using namespace std; int main(){ int n; cin>>n;getchar(); for(int i=0;i<n;i++){ int hasnum=0; int hasa=0; int no=0; string s; getline(cin,s); if(s.length()<6){ cout<<"Your password is tai duan le."<<endl; continue; }else{ for(int a=0;a<(s.length());a++){ if(s[a]>='a'&&s[a]<='z'){ hasa=1; }else if(s[a]>='A'&&s[a]<='Z'){ hasa=1; }else if(s[a]>='0'&&s[a]<='9'){ hasnum=1; }else if(s[a]=='.'){ } else{ no=1; //cout<<s; //cout<<s[a]<<endl; } } if(no){ printf("Your password is tai luan le.n"); }else if(hasnum==0){ printf("Your password needs shu zi.n"); }else if(hasa==0){ printf("Your password needs zi mu.n"); }else { printf("Your password is wan mei.n"); } } } return 0; }
再放柳神的
(還好了其實,跟我的本質差不多,但是人家排版比較好)
#include <iostream> #include <cctype> using namespace std; int main() { int n; cin >> n; getchar(); for (int i = 0; i < n; i++) { string s; getline(cin, s); if (s.length() >= 6) { int invalid = 0, hasAlpha = 0, hasNum = 0; for (int j = 0; j < s.length(); j++) { if (s[j] != '.' && !isalnum(s[j])) invalid = 1; else if (isalpha(s[j])) hasAlpha = 1; else if (isdigit(s[j])) hasNum = 1; } if (invalid == 1) cout << "Your password is tai luan le.n"; else if (hasNum == 0) cout << "Your password needs shu zi.n"; else if (hasAlpha == 0) cout << "Your password needs zi mu.n"; else cout << "Your password is wan mei.n"; } else cout << "Your password is tai duan le.n"; } return 0; }