每日一练10——井字棋&&密码强度等级

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

文章目录


井字棋

题目链接

思路

井字棋是一种在3*3格子上进行的连珠游戏三个相同就代表获胜。
井字棋有四种情况表示当前玩家获胜1代表当前玩家棋子

  1. 行全为1 即行的和为3
  2. 列全为1 列的和为3
  3. 主对角全为1 对角和为3
  4. 副对角全为1 对角和为3

如果扩展为N*N的话判断和是否等于N下面代码适用任何情况

代码

#include <type_traits>
class Board {
public:
    bool checkWon(vector<vector<int> > board) {
        // write code here
        //既然是三子棋那一定是正方形的
        int  length=board.size();
        
        //一列全是1
        int sum=0;
        for(int i=0;i<length;i++)
        {
            sum=0;
            for(int j=0;j<length;j++)
            {
                sum+=board[j][i];
            }
            if(sum==length)
            return true;
        }

        //一行全是1
        for(int i=0;i<length;i++)
        {
            sum=0;
            for(int j=0;j<length;j++)
            {
                sum+=board[i][j];
            }
            if(sum==length)
            return true;
        }

        //主对角线全是1
        sum=0;
        for(int i=0;i<length;i++)
        {
            sum+=board[i][i];

        }
        if(sum==length)
        return true;

        //副对角线全是1
        sum=0;
        for(int i=0;i<length;i++)
        {
            sum+=board[i][length-i-1];
        }
        if(sum==length)
        return true;

        return false;
    }
};

密码强度等级

题目链接

思路

这里对于不同种类的字符得分不同对于长度字母数字符号单独判断最后把所有的单项值根据题目要求相加输出对应的安全级别。
要点
1、对于题目中提到的不同条件都要一次性统计出来。
2、通过if else语句判断得分情况
最好是先从条件较多的 先判断
在这里插入图片描述
就像上面的几种条件
可以先从条件最多的五分的下手先看看是否满足5分的条件
然后不满足五分的再看看是否满足3分的条件其次是2分的
这样就可以选出来 符合最多的那一种奖励 同时if else语句也显得更加逻辑清晰。

代码

#include <iostream>
#include <string>
using namespace std;
int score_count(string& str) {
    int digit = 0, symbol = 0;
    int lower = 0, upper = 0, charc = 0;
    int size = 0, sum = 0;

    for (auto ch : str) {
        if (ch >= 'a' && ch <= 'z') {
            lower++;
            charc++;
        } else if (ch >= 'A' && ch <= 'Z') {
            upper++;
            charc++;
        } else if (ch >= '0' && ch <= '9') {
            digit++;
        } else if ((ch >= 0x21 && ch <= 0x2F) ||
                   (ch >= 0x3A && ch <= 0x40) ||
                   (ch >= 0x5B && ch <= 0x60) ||
                   (ch >= 0x7B && ch <= 0x7E)) {
            symbol++;
        }
    }
    size = str.size();
    if (size >= 8)
        sum += 25;
    else if (size >= 5)
        sum += 10;
    else
        sum += 5;

    if (lower > 0 && upper > 0)
        sum += 20;
    else if (lower == charc || upper == charc)
        sum += 10;

    if (digit == 1)
        sum += 10;
    else if (digit > 1)
        sum += 20;

    if (symbol == 1)
        sum += 10;
    else if (symbol > 1)
        sum += 25;

    if (lower > 0 && upper > 0 && digit > 0 && symbol > 0)
        sum += 5;
    else if ((lower > 0 || upper > 0) && digit > 0 && symbol > 0)
        sum += 3;
    else if ((lower > 0 || upper > 0) && digit > 0 && symbol == 0)
        sum += 2;
    return sum;
}
int main() {
    string str;
    while (cin >> str) {
        int score = score_count(str);
        if (score >= 90) {
            cout << "VERY_SECURE" << endl;
        } else if (score >= 80) {
            cout << "SECURE" << endl;
        } else if (score >= 70) {
            cout << "VERY_STRONG" << endl;
        } else if (score >= 60) {
            cout << "STRONG" << endl;
        } else if (score >= 50) {
            cout << "AVERAGE" << endl;
        } else if (score >= 25) {
            cout << "WEAK" << endl;
        } else {
            cout << "VERY_WEAK" << endl;
        }
    }
    return 0;
}

end

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6