鱼C论坛

 找回密码
 立即注册
查看: 1826|回复: 13

[吹水] 真正的A+B

[复制链接]
发表于 2023-7-28 22:16:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<bits/stdc++.h>
using namespace std;
char a[250],b[250];
int ia[250],ib[250],ic[250];
int main(){
        int jinwei = 0;
        int temp = 0,len;
        scanf("%s",a);
        scanf("%s",b);
        for (int i = strlen(a)-1;i>=0;i--){
                ia[temp] = a[i] - '0';
                temp ++;
        }
        temp = 0;
        for (int i = strlen(b)-1;i>=0;i--){
                ib[temp] = b[i] - '0';
                temp++;
        }
        if (strlen(a) > strlen(b)) {
                len = strlen(a);
        }
        else{
                len = strlen(b);
        }
        for (int i = 0;i<=len;i++){
                if (jinwei == 1){
                        ic[i] = ia[i] + ib[i] + 1;
                        jinwei = 0;
                }
                else{
                        ic[i] = ia[i] +ib[i];
                }
                if (ic[i] >=10){
                        jinwei = 1;
                        ic[i] -= 10;
                }
        }
        for (int i = len;i>=0;i--){
                if (ic[i] ==0&&i == len){        
                }
                else{
                        cout<<ic[i];
                }                
        }        
}
这才叫a+b
(高精度运算)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-7-28 22:30:06 | 显示全部楼层
不是要用线段树来做么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-28 22:33:13 | 显示全部楼层
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>

using cpp_int = boost::multiprecision::cpp_int;
using std::cin, std::cout, std::endl;

int main() {
    cpp_int a, b; cin >> a >> b;
    cout << a + b << endl;
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-28 22:37:08 | 显示全部楼层
#include <bits/stdc++.h>
#define ll long long
#define N 100000
using namespace std;
int sz[N], rev[N], tag[N], sum[N], ch[N][2], fa[N], val[N];
int n, m, rt, x;
void push_up(int x){
    sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
    sum[x] = sum[ch[x][1]] + sum[ch[x][0]] + val[x];
}
void push_down(int x){
    if(rev[x]){
        swap(ch[x][0], ch[x][1]);
        if(ch[x][1]) rev[ch[x][1]] ^= 1;
        if(ch[x][0]) rev[ch[x][0]] ^= 1;
        rev[x] = 0;
    }
    if(tag[x]){
        if(ch[x][1]) tag[ch[x][1]] += tag[x], sum[ch[x][1]] += tag[x];
        if(ch[x][0]) tag[ch[x][0]] += tag[x], sum[ch[x][0]] += tag[x];
        tag[x] = 0;
    }
}
void rotate(int x, int &k){
    int y = fa[x], z = fa[fa[x]];
    int kind = ch[y][1] == x;
    if(y == k) k = x;
    else ch[z][ch[z][1]==y] = x;
    fa[x] = z; fa[y] = x; fa[ch[x][!kind]] = y;
    ch[y][kind] = ch[x][!kind]; ch[x][!kind] = y;
    push_up(y); push_up(x);
}
void splay(int x, int &k){
    while(x != k){
        int y = fa[x], z = fa[fa[x]];
        if(y != k) if(ch[y][1] == x ^ ch[z][1] == y) rotate(x, k);
        else rotate(y, k);
        rotate(x, k);
    }
}
int kth(int x, int k){
    push_down(x);
    int r = sz[ch[x][0]]+1;
    if(k == r) return x;
    if(k < r) return kth(ch[x][0], k);
    else return kth(ch[x][1], k-r);
}
void split(int l, int r){
    int x = kth(rt, l), y = kth(rt, r+2);
    splay(x, rt); splay(y, ch[rt][1]);
}
void rever(int l, int r){
    split(l, r);
    rev[ch[ch[rt][1]][0]] ^= 1;
}
void add(int l, int r, int v){
    split(l, r);
    tag[ch[ch[rt][1]][0]] += v;
    val[ch[ch[rt][1]][0]] += v;
    push_up(ch[ch[rt][1]][0]);
}
int build(int l, int r, int f){
    if(l > r) return 0;
    if(l == r){
        fa[l] = f;
        sz[l] = 1;
        return l;
    }
    int mid = l + r >> 1;
    ch[mid][0] = build(l, mid-1, mid);
    ch[mid][1] = build(mid+1, r, mid);
    fa[mid] = f;
    push_up(mid);
    return mid;
}
int asksum(int l, int r){
    split(l, r);
    return sum[ch[ch[rt][1]][0]];
}
int main(){
    //总共两个数
    n = 2;
    rt = build(1, n+2, 0);//建树
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        add(i, i, x);//区间加
    }
    rever(1, n);//区间翻转
    printf("%d\n", asksum(1, n));//区间求和
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-28 22:40:11 | 显示全部楼层
《挑战A+B最长代码》
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-28 22:54:06 | 显示全部楼层
sfqxx 发表于 2023-7-28 22:40
《挑战A+B最长代码》

但是介个可以计算很大的数
超过了long long
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-28 22:54:40 | 显示全部楼层
sfqxx 发表于 2023-7-28 22:40
《挑战A+B最长代码》

泰裤辣
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-28 22:55:14 | 显示全部楼层
zhangjinxuan 发表于 2023-7-28 22:30
不是要用线段树来做么

用字符串也行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-28 23:01:09 | 显示全部楼层
香蕉那个不拿拿 发表于 2023-7-28 22:54
但是介个可以计算很大的数
超过了long long

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-29 10:03:47 | 显示全部楼层
我也来秀一下
#include <bits/stdc++.h>
using namespace std;

struct bint{
    int len;
    int t[5000];

    bint(){
        memset(t, 0, sizeof(t));
        len = 0;
    }

    bint(const string &s){
        memset(t, 0, sizeof(t));
        len = s.length();
        for (int i = 0; i < len; i++){
            t[i] = s[len - 1 - i] - '0';
        }
    }

    void print(){
        for (int i = 0; i < len; i++){
            cout << t[len - 1 - i];
        }
        cout << endl;
    }

    bint operator=(const bint &b){
        len = b.len;
        memcpy(t, b.t, len * sizeof(int));
        return *this;
    }

    bint operator+(const bint &b) const{
        bint c;
        c.len = max(len, b.len);
        int carry = 0;
        for (int i = 0; i < c.len; i++){
            int x = t[i] + b.t[i] + carry;
            c.t[i] = x % 10;
            carry = x / 10;
        }
        if (carry){
            c.t[c.len++] = carry;
        }
        return c;
    }

    bint operator*(const bint &b) const{
        bint c;
        for (int i = 0; i < len; i++){
            int carry = 0;
            for (int j = 0; j < b.len; j++){
                int x = t[i] * b.t[j] + c.t[i + j] + carry;
                c.t[i + j] = x % 10;
                carry = x / 10;
            }
            if (carry){
                c.t[i + b.len] += carry;
            }
        }
        c.len = len + b.len;
        while (c.len > 1 && c.t[c.len - 1] == 0){
            c.len--;
        }
        return c;
    }
};

int main(){
    string a, b;
    cin >> a >> b;
    bint c(a), d(b);
    bint ans_jia = c + d;
    ans_jia.print();

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-29 10:34:44 | 显示全部楼层
print(sum(map(int, input().split())))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-29 11:36:09 | 显示全部楼层

一点也不节省,逗号后面的空格,赶紧吃了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-29 11:42:10 | 显示全部楼层
zhangjinxuan 发表于 2023-7-29 11:36
一点也不节省,逗号后面的空格,赶紧吃了

print(sum(map(int,input().split())))
#___________
#|      o  /
#|        /
#|       /
#|      /    " "
#|      \
#|       \
#|        \
#|_________\
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-29 11:42:41 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 23:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表