首页 > 其他 > 详细

高精度二进制加法

时间:2021-08-14 16:53:35      阅读:16      评论:0      收藏:0      [点我收藏+]
#include<vector>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;

const int N = 6e5 + 10;
int cnt = 1;
int e[N];
int main()
{
    string s;
    cin >> s;
    string t = s;
    s += "0000";
    int l1 = t.size(), l2 = s.size();
    int c = 0;
    string res = "";
    for (int i = t.size() - 1, j = s.size() - 1; j >= 0 || i >= 0; i --, j --)
    {
        if(j >= 0)
            c += s[j] - ‘0‘;
        if(i >= 0)
            c += t[i] - ‘0‘;
        res += c % 2 + ‘0‘;
        c /= 2;
    }
    if(c)
        res += ‘1‘;
    reverse(res.begin(), res.end());
    cout << res << endl;
}
//3 9 -5 8 -6 6 2 -9 -1 -4

高精度二进制加法

原文:https://www.cnblogs.com/jw-zhao/p/15140909.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!