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


井字棋

题目链接:

思路:

井字棋,是一种在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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有效的放假者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值