(9、10)/48(5_16) 编程

1、另类加法

思路:肯定是用位运算,位运算A^B是不考虑进位的结果,(A&B) << 1是求得的进位
因此A^B + (A&B) << 1的结果就是和,当(A^B)或(A&B) << 1等于0 ,两项就变成了一项,不需要加法了

//另类加法
class a{
public:
	int addAB(int A, int B) {
		if (A == 0)return B;
		if (B == 0)return A;
		int a = 0, b = 0;
		a = A^B;
		b = (A&B) << 1;
		return addAB(a, b);
	}
};

2、走方格的方案数
从右下角的格子往左上角的格子递归:
到当前格子(n, m)的方法只有从左边(n - 1, m)与上边(n, m - 1),即到当前格子的总走法等于到(n - 1, m) + (n, m - 1)的走法;
递归出口:当前格子为上边界或者左边界时,只有一种走法。

int pathnum(int a, int b){
	if (a == 0 || b == 0){
		return 1;
	}
	return pathnum(a - 1, b) + pathnum(a, b - 1);
}
int main() {
	int n, m;
	while (cin >> n >> m) {
		cout << pathnum(n, m) << endl;
	}
}

3、井字棋
元素为1的代表是当前玩家的棋子,0表示没有棋子,-1代表是对方玩家的棋子,返回当前玩家是否胜出。
整个行数的值加起来等于列数,获胜
整个列数的值加起来等于行数,获胜
正斜board[row][row]
反斜board[row][col-i-i]

class Board {
public:
	bool checkWon(vector<vector<int> > board) {
		int row = board.size();
		int col = board[0].size();
		//每一行是否连续
		for (int i = 0; i<row; i++){
		int sum = 0;
			for (int j = 0; j<col; j++){
				sum += board[i][j];
			}
			if (sum == col){
				return true;
			}
		}
		//每一列是否连续
		for (int i = 0; i<col; i++){
			int sum = 0;
			for (int j = 0; j<row; j++){
				sum += board[j][i];
			}
			if (sum == row){
				return true;
			}
		}
		int sum = 0;
		//正斜是否连续
		for (int i = 0; i<row; i++){
			sum += board[i][i];
		}
		if (sum == row){
			return true;
		}
		//副斜是否连续
		sum = 0;
		for (int i = 0; i<row; i++){
			sum += board[i][col - i - 1];
		}
		if (sum == row){
			return true;
		}
		return false;
	}
};

4、密码等级强度

遍历一遍整个字符串,统计出字符、数字、大小写字母的个数,

使用if...else if...判断分数,

最后根据分数判断密码强度等级并输出。

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 > 4) {
		sum += 10;
	}
	else {
		sum += 5;
	}

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

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

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

	if (lower>0 && upper>0 && digit>0 && symbol>0){
		sum += 5;
	}
	else if (upper == charc || lower == charc){
		sum += 3;
	}
	else if (symbol == 0){
		sum += 2;
	}

	return sum;
}

int main(){
	string s;
	while (cin >> s) {
		int score = score_count(s);
		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;
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值