其他分享
首页 > 其他分享> > 2022.5.6 AcWing每日一题

2022.5.6 AcWing每日一题

作者:互联网

签到题

字符串模拟二进制加法,因为 X17 = X16 + X1,而 16 倍相当于二进制左移四位,再和本身进行二进制相加即可。

#include <bits/stdc++.h>
using namespace std;

const int N = 1e3 + 10;

int a[N];
int b[N];
string str;

int main() {
	cin >> str;
	int len = str.size();
	for (int i = 0; i < len; i++) {
		a[i + 1] = str[i] - '0';
		b[i + 5] = str[i] - '0';
	}

	int tmp = 0;
	for (int i = len + 4; i >= 0; i--) {
		int sum = (a[i] + b[i] + tmp);
		a[i] = sum % 2;
		tmp = sum / 2;
	}

	for (int i = 0; i <= len + 4; i++) {
		if (i == 0 && a[i] == 0) {
			continue;
		}
		printf("%d", a[i]);
	}
	printf("\n");

	return 0;
}

标签:tmp,int,每日,len,二进制,str,2022.5,sum,AcWing
来源: https://www.cnblogs.com/superPG/p/16227264.html