本人使用高精度,只得40分

P1001 A+B Problem

keda2761 @ 2023-03-26 11:54:35


#include<bits/stdc++.h>
using namespace std;
char a[10001],b[10001];
int ina[10001],inb[10001],inc[10002],l;
int main(){
    cin>>a>>b;
    l=strlen(a)-1;//a的末尾下标 
    for(int i=0;i<strlen(a);i++){
        ina[l]=a[i]-'0';
        l--;//反转数字(其实是字符串)
    }
    l=strlen(b)-1;  
    for(int i=0;i<strlen(b);i++){
        inb[l]=b[i]-'0';
        l--;//反转数字(其实是字符串) 
    }
    int len=max(strlen(a),strlen(b));
    for(int i=0;i<len;i++){
        inc[i]=inc[i]+ina[i]+inb[i];//加上 
        if(inc[i]>9){//进位 
            inc[i+1]=inc[i+1]+inc[i]/10;//下一位加10 
            inc[i]=inc[i]%10;//舍去十位数 
        }
    }
    int k;
    for(int i=999;i>=0;i--){
        if(inc[i]!=0){
            k=i;
            break;
        }//寻找最大的位数 
    }
    for(int i=k;i>=0;i--){
        cout<<inc[i];
    }//输出次数 
    return 0;
}//不谢!!! 

by dingchentao0720 @ 2023-04-29 13:54:30

@zhouyilin,还用高精度?int 可以!!


by jianghl666_IKUN @ 2023-05-02 09:29:46

《a+b用高精度》人才,人才!


by Big_Brain @ 2023-05-04 09:06:01

这题有负数,要考虑

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
char a[1000005];
char b[1000005];
int fa, fb;
int la, lb;

void show(int ed) {
    if (fa)
        printf("-");
    for (int i = ed; i >= fa; --i)
        printf("%d", a[i]);
}
void add() {
    int i, j, t = 0, s, l;
    for (i = fa; i < la || i < lb; ++i) {
        s = a[i]  + b[i]   + t;
        t = s / 10;
        a[i] = s % 10;
    }
    a[i] = t ;
    l = t ? i : i - 1;
    show(l);
}
int cmp() {
    int la = strlen(a), lb = strlen(b);
    if (la - fa > lb - fb)
        return 1;
    else if (la - fa < lb - fb)
        return 0;
    else {
        int i ;
        for ( i = 0; i < la && a[i + fa] == b[i + fb]; ++i);
        return a[i + fa] > b[i + fb];
    }
}

void minu() {
    int i, j, c = 0, l = -1, s;
    for (i = 0; i + fa < la; ++i) {
        s = a[i + fa] - b[i + fb] - c >= 0 ? 0 : 1;
        a[i + fa] = (10 + a[i + fa] - b[i + fb] - c) % 10;
        l = a[i + fa] ? i + fa : l; 
        c = s;
    }
    if (l < 0)
        printf("0");
    else
        show(l);
}

int main() {
    scanf("%s%s", a, b);
    fa = ('-' == a[0]), fb = ('-' == b[0]);

    if ( !cmp())
        swap(a, b), swap(fa, fb);
    la = strlen(a), lb = strlen(b);
    reverse(a + fa, a + la), reverse(b + fb, b + lb);
    int i = fa, j = fb;
    while (i < la)
        a[i] -= '0', i ++;
    while (j < lb)
        b[j] -= '0', j ++;
    if (fa ^ fb)
        minu();
    else
        add();
    return 0;
}

by Big_Brain @ 2023-05-04 09:07:09

一个负数使用减法,没有或两个负数使用加法,就这样


by jianghaoxuan1 @ 2023-05-11 13:29:38

有手就行~

#include<bits/stdc++.h>
using namespace std;
int main(){
    //freopen("p1001.in","r",stdin);
    //freopen("p1001.out","w",stdout);
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

by 2022_37_yzyUUU @ 2023-07-11 09:06:35

数组开小了


by 2022_37_yzyUUU @ 2023-07-11 09:07:51

1数组小了 2有负数


by SBSSSSSBSB @ 2023-07-22 19:49:45

@zhouyilin ,me too


上一页 |