題意:二進制的高精度與低精度乘法
分析:通過17的二進制表示,我們發現,只有兩位1,所以可以用類似大整數加法的方法來處理。第幾位有1,就相當於把輸入的二進制右移幾位。存儲的時候,記得倒過來存。
Code:
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1005;
char s[maxn];
int a[maxn], b[maxn];
int ans[maxn];
int main() {
while(scanf("%s", s) != EOF) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int len = strlen(s);
for(int i = 0; i < len; i++)
a[i] = s[len-i-1]-'0';
for(int i = 4; i < len+4; i++)
b[i] = s[len+4-i-1]-'0';
int carry = 0;
for(int i = 0; i < len+4; i++) {
int tmp = a[i]+b[i]+carry;
ans[i] = tmp%2;
carry = tmp/2;
}
int cnt = len+4;
if(carry != 0) ans[cnt++] = 1;
for(int i = cnt-1; i >= 0; i--)
printf("%d", ans[i]);
printf("\n");
memset(s, 0, sizeof(s));
}
return 0;
}