1. 程式人生 > >PAT-1081 檢查密碼

PAT-1081 檢查密碼

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>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{
  int n,num=0,cha=0,dot=0;
  string s;
  cin>>n;
  getchar();//這裡必須錄了那個回車符,否則不通過
  for(int i=0;i<n;i++)
  {
    num=0;
    cha=0;
    dot=0;
    getline(cin,s);//此處用cin會導致一個測試點不通過
    int l=s.length();
    if(l<6)
    {
      cout<<"Your password is tai duan le."<<endl;
    }
    else
    {
      for(int i=0;i<l;i++)
      {
          if(s[i]>='0'&&s[i]<='9')
          {
            num++;
          }
          if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))
          {
            cha++;
          }
          if(s[i]=='.')
          {
            dot++;
          }
      }
      int sum=num+cha+dot;
      if(sum<l)
      {
        cout<<"Your password is tai luan le."<<endl;
      }
      else if(num==0&&cha!=0)
      {
        cout<<"Your password needs shu zi."<<endl;
      }
      else if(cha==0&&num!=0)
      {
        cout<<"Your password needs zi mu."<<endl;
      }
      else
      {
        cout<<"Your password is wan mei."<<endl;
      }
    }
  }
  return 0;
}