2018校招編程題---密碼檢查

牛客網鏈接地址:https://www.nowcoder.com/practice/f2fbd8f61c564ca0b5feaa63ab42dae5?tpId=90&&tqId=30984&rp=9&ru=/activity/oj&qru=/ta/2018test/question-ranking

題目描述

小明同學最近開發了一個網站,在用戶註冊賬戶的時候,需要設置賬戶的密碼,爲了加強賬戶的安全性,小明對密碼強度有一定要求:

1. 密碼只能由大寫字母,小寫字母,數字構成;

2. 密碼不能以數字開頭;

3. 密碼中至少出現大寫字母,小寫字母和數字這三種字符類型中的兩種;

4. 密碼長度至少爲8

現在小明受到了n個密碼,他想請你寫程序判斷這些密碼中哪些是合適的,哪些是不合法的。

輸入描述:

輸入一個數n,接下來有n(n≤100)行,每行一個字符串,表示一個密碼,輸入保證字符串中只出現大寫字母,小寫字母和數字,字符串長度不超過100。

輸出描述:

輸入n行,如果密碼合法,輸出YES,不合法輸出NO

示例1

輸入

1
CdKfIfsiBgohWsydFYlMVRrGUpMALbmygeXdNpTmWkfyiZIKPtiflcgppuR

輸出

YES

實現代碼:

注意:count一個變量每一個位代表不同含義,避免了多次定義變量提高內存消耗

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#pragma warning(disable : 4996) 

int main()
{
    char str[500] = { 0 };
    char *p = NULL;
    unsigned char count = 0x00; //4.5.6.7位表示條件1,2,3,4,,0.1.2表示數字,小寫字母,大寫字母是否出現,
    int n = 0;
    scanf("%d",&n);
    getchar();
    while (n)
    {
        gets(str);
        p = str;

        if (*p > '0'&&*p < '9') //條件2
        {
            count |= 0x20;
            goto print;  //條件不滿足則直接跳轉輸出提高執行速度
        }
        if (strlen(p) < 8) //條件4
        {
            count |= 0x80;
            goto print;
        }
        while ('\0' == *p)  //遍歷整個字符串查看數字字母的是否出現
        {
            //數字或者大小寫字母出現相應位置1
            if (
                (*p > '0'&&*p < '9') || \
                (*p > 'a'&&*p < 'z') || \
                (*p > 'A'&&*p < 'Z')
                )
            {
                if (*p > '0'&&*p < '9')
                    count |= 0x01;
                if (*p > 'a'&&*p < 'z')
                    count |= 0x02;
                if (*p > 'A'&&*p < 'Z')
                    count |= 0x04;
            }
            else //出現其他非法字符則條件1置1
            {
                count |= 0x10;
                break ; //直接結束遍歷
            }
            p++;
        }
        if (
            ((count & 0x01 == 1) && (count & 0x02 == 0) && (count & 0x04 == 0)) ||
            ((count & 0x01 == 0) && (count & 0x02 == 1) && (count & 0x04 == 0)) ||
            ((count & 0x01 == 0) && (count & 0x02 == 0) && (count & 0x04 == 1))
            )
        {
            count |= 0x40;
        }
    print:{
        if (
        (count & 0xF0) == 0
        )
        printf("YES\n");
          else
              printf("NO\n");
            }
        n--;
        count = 0; //標誌變量清0
        memset(str, 0, sizeof(str)); //清空輸入字符數組
    }
    system("pause");
    return 0;
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章