天天看點

1081 檢查密碼 (15 分)(gets()函數自動丢掉末尾\n,不需要getchar())注意邏輯的先後順序考慮!continue用法:跳過後續一句程式。gets函數用法:包含等價于scanf("%s",str);好處:AC代碼:注意點:

本題要求你幫助某網站的使用者注冊子產品寫一個密碼合法性檢查的小功能。該網站要求使用者設定的密碼必須由不少于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.
           

注意邏輯的先後順序考慮!

continue用法:跳過後續一句程式。

gets函數用法:包含等價于scanf("%s",str);

好處:

  1. 比scanf("%s",str);更簡潔

  2. 允許輸入空格,而scanf()不允許

  3. 以\n回車結束輸入,但是相比scanf()不需要去getchar(),gets自動丢掉末尾\n

AC代碼:

#include<stdio.h>
#include<string.h>
int  main()
{
    int N;
    char a[81];
    scanf("%d", &N);
    getchar();//清空回車符
    while (N--)
    {
        gets(a);
        int len = strlen(a);
        if (len < 6)
        {
            printf("Your password is tai duan le.\n");
            continue;
        }
        else
        {
            int f1 = 0, f2 = 0, f3 = 1;

            for (int j = 0; j < len; j++)
            {
                if (a[j] >= '0' && a[j] <= '9')
                    f1 = 1;
                else if ((a[j] >= 'a' && a[j] <= 'z') || (a[j] >= 'A' && a[j] <= 'Z'))
                    f2 = 1;
                else if (a[j] == '.');
                else
                {
                    f3 = 0;
                    break;
                }
            }
            if (!f3)
            {
                printf("Your password is tai luan le.\n");
            }
            else if (!f1 && f2)
            {
                printf("Your password needs shu zi.\n");
            }
            else if (!f2 && f1)
            {
                printf("Your password needs zi mu.\n");
            }
            else
            {
                printf("Your password is wan mei.\n");
            }
        }
    }
    return 0;
}
           

注意點:

不超過 80 個字元的非空字元串   數組大小不能是a[80],而是81,看清楚題目!!

注意邏輯的先後順序考慮