求助

P1001 A+B Problem

HFBZ_MIT_MC @ 2024-08-02 12:12:11

#include<bits/stdc++.h>
using namespace std;
string s1,s2,s3;
int l1,l2;
int a,b,c,j;
char ch;
int main()
{
    cin>>s1>>s2;
    l1=s1.size();l2=s2.size();
    if(l1<l2) {swap(s1,s2);swap(l1,l2);}
    for(int i=1;i<=l1-l2;i++) s2='0'+s2;
    j=0;
    for(int i=l1-1;i>=0;i--)
    {
        a=s1[i]-'0';
        b=s2[i]-'0';
        c=a+b+j;
        if(c>=10) {c=c-10;j=1;}
        else      j=0;
        ch=char(c+'0');
        s3=ch+s3;
    }
    if(j==1) s3='1'+s3;
    cout<<s3;
    return 0;
}

by yangrenruiYRR @ 2024-08-02 12:13:11


#include<bits/stdc++.h>
using namesoace std;
int main(){
  int a,b;
  cin>>a>>b;
  cout<<a+b;
  return 0;
}

by luoyiwen @ 2024-08-02 14:52:32

@wangyihan100 至于吗


by ___LuXun___ @ 2024-08-02 19:35:48

求助!(50)

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

const int N = 1e3+10;

string s1,s2;
int a[N],b[N],c[N];
int main(){
    cin>>s1>>s2;
    int lena=s1.size(),lenb=s2.size();
    int lenmax=max(lena,lenb);
    for(int i=s1.size()-1,j=1;i>=0;i--){
        a[j++]=(s1[i]-'0');
    }
    for(int i=s2.size()-1,j=1;i>=0;i--){
        b[j++]=(s2[i]-'0');
    }
    for(int i=1;i<=lenmax;i++){
        c[i]=(a[i]+b[i]) +c[i];
        c[i+1]=c[i]/10;
        c[i]=c[i]%10;
    }
    lenmax++;
    while(c[lenmax]==0&&lenmax>1) lenmax--;
    for(int i=lenmax;i>=1;i--) cout<<c[i];
    return 0;
}

by Ly_yang @ 2024-08-05 17:38:43

@LuXun ......至于吗```

include<iostream>

using namespace std; int main() { int a,b; cin>>a>>b; cout<<a+b<<endl;

return 0;

}


by ___LuXun___ @ 2024-08-05 20:01:48

@Ly_yang 大佬,求调教。这个不能有负数,所以才50


by Ly_yang @ 2024-08-05 20:13:09

@LuXun 你用什么语言?


by Ly_yang @ 2024-08-05 20:14:01

@LuXun c++?


by ___LuXun___ @ 2024-08-05 20:29:48

@Ly_yang YES


by Ly_yang @ 2024-08-05 20:44:19

@LuXun 等一下(2min)


by yanghb666 @ 2024-08-06 15:24:16

给你高精代码,求关注(有点过分)

// C
#ifdef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,n) for(register int i = a; i <= n; i++)
#define ROF(i,a,n) for(register int i = a; i >= n; i--)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define INF 0x3f3f3f3f
#define INFS -0x3f3f3f3f
#define reg register
#define ll long long
#define LL long long
#define ull unsigned long long
#define ULL unsigned long long
#define ld long double
#define eps 1e-9
#define PI acos(-1)
#define mem(a,b) memset(a, b, sizeof(a))
#define memx(a) memset(a, INF, sizeof(a))
#define memy(a) memset(a, INFS, sizeof(a))
#define N 100005
#define M 1005
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mod 1000000007
#define mod2 998244353
#define mod3 1000000009
#define MAXX1 LONG_LONG_MAX
#define MINN1 LONG_LONG_MIN
#define MAXX INT_MAX
#define MINN INT_MIN
#define debug(x) cerr<<#x<<"="<<x<<endl
#define debug2(x,y) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define debug3(x,y,z) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define debug4(w,x,y,z) cerr<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define debug5(w,x,y,z,a) cerr<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#a<<"="<<a<<endl
#define debug6(w,x,y,z,a,b) cerr<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#a<<"="<<a<<" "<<#b<<"="<<b<<endl
#define debug7(w,x,y,z,a,b,c) cerr<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<endl
#define debug8(w,x,y,z,a,b,c,d) cerr<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "#d<<"="<<d<<endl
#define debug9(w,x,y,z,a,b,c,d,e) cerr<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "#d<<"="<<d<<" "<<#e<<"="<<e<<endl
#define debug10(w,x,y,z,a,b,c,d,e,f) cerr<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "#d<<"="<<d<<" "<<#e<<"="<<e<<" "<<#f<<"="<<f<<endl
#define debug11(w,x,y,z,a,b,c,d,e,f,g) cerr<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "#d<<"="<<d<<" "<<#e<<"="<<e<<" "<<#f<<"="<<f<<" "<<#g<<"="<<g<<endl
#define debug12(w,x,y,z,a,b,c,d,e,f,g,h) cerr<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "#d<<"="<<d<<" "<<#e<<"="<<e<<" "<<#f<<"="<<f<<" "<<#g<<"="<<g<<" "<<#h<<"="<<h<<endl
#define IOS2 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define check(x) cout<<#x<<"="<<x<<endl
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define input(x) cin>>x
#define output(x) cout<<x<<endl
#define input2(x,y) cin>>x>>y
#define output2(x,y) cout<<x<<" "<<y<<endl
#define input3(x,y,z) cin>>x>>y>>z
#define output3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define input4(x,y,z,a) cin>>x>>y>>z>>a
#define output4(x,y,z,a) cout<<x<<" "<<y<<" "<<z<<" "<<a<<endl
#define input5(x,y,z,a,b) cin>>x>>y>>z>>a>>b
#define output5(x,y,z,a,b) cout<<x<<" "<<y<<" "<<z<<" "<<a<<" "<<b<<endl
#define input6(x,y,z,a,b,c) cin>>x>>y>>z>>a>>b>>c
#define output6(x,y,z,a,b,c) cout<<x<<" "<<y<<" "<<z<<" "<<a<<" "<<b<<" "<<c<<endl
#define input7(x,y,z,a,b,c,d) cin>>x>>y>>z>>a>>b>>c>>d
#define output7(x,y,z,a,b,c,d) cout<<x<<" "<<y<<" "<<z<<" "<<a<<" "<<b<<" "<<c<<" "<<d<<endl
#define input8(x,y,z,a,b,c,d,e) cin>>x>>y>>z>>a>>b>>c>>d>>e
#define output8(x,y,z,a,b,c,d,e) cout<<x<<" "<<y<<" "<<z<<" "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl
#define input9(x,y,z,a,b,c,d,e,f) cin>>x>>y>>z>>a>>b>>c>>d>>e>>f
#define output9(x,y,z,a,b,c,d,e,f) cout<<x<<" "<<y<<" "<<z<<" "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<endl
#define input10(x,y,z,a,b,c,d,e,f,g) cin>>x>>y>>z>>a>>b>>c>>d>>e>>f>>g
#define output10(x,y,z,a,b,c,d,e,f,g) cout<<x<<" "<<y<<" "<<z<<" "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" "<<g<<endl
#define input11(x,y,z,a,b,c,d,e,f,g,h) cin>>x>>y>>z>>a>>b>>c>>d>>e>>f>>g>>h
#define output11(x,y,z,a,b,c,d,e,f,g,h) cout<<x<<" "<<y<<" "<<z<<" "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" "<<g<<" "<<h<<endl
#define input12(x,y,z,a,b,c,d,e,f,g,h,i) cin>>x>>y>>z>>a>>b>>c>>d>>e>>f>>g>>h>>i
#define output12(x,y,z,a,b,c,d,e,f,g,h,i) cout<<x<<" "<<y<<" "<<z<<" "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" "<<g<<" "<<h<<" "<<i<<endl
#define endl cout << endl
using namespace std;
int qpow(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
const int MAXN = 1e5;
const int siz = 8;
const int MOD = 1e8;
char ch1[MAXN], ch2[MAXN];
bool f1, f2, f; // 0为非负,1为负
int n;
int a[MAXN >> 2], b[MAXN >> 2], s[MAXN >> 2];
int cp[MAXN >> 2], lt[MAXN >> 2], wsd[MAXN >> 2];
void write(int num[]);                // 输出高精度数
void clear(int num[]);                // 数组重置
void ry(int num[]);                   //>>右移一位
void ly(int num[]);                   //<<左移一位
void cpy(int num1[], int num2[]); // num1=num2
int cmp(int num1[], int num2[]);  // compare num1 and num2
void pls(int a[], int b[]);
void sub(int a[], int b[]);
void mul(int a[], int b[]);
void div(int a[], int b[]);
void write(int num[])
{
    if (f)
        putchar('-'), f = 0;
    printf("%lld", num[num[0]]);
    for (int i = num[0] - 1; i; --i)
        printf("%08lld", num[i]);
    puts("");
}

void clear(int num[]) {
    for (int i = num[0]; i; --i)
        num[i] = 0;
    num[0] = 1;
}
void ry(int num[]) {
    for (int i = num[0]; i; --i) {
        if ((num[i] & 1) && i > 1)
            num[i - 1] += MOD;
        num[i] >>= 1;
    }
    if (!num[num[0]] && num[0] > 1)
        --num[0];
}
void ly(int num[]) {
    ++num[0];
    for (int i = 1; i <= num[0]; ++i)
    {
        num[i] <<= 1;
        if (num[i - 1] >= MOD)
            num[i - 1] -= MOD, ++num[i];
    }
    if (!num[num[0]] && num[0] > 1)
        --num[0];
    return;
}
void cpy(int num1[], int num2[])
{
    for (int i = num1[0]; i > num2[0]; --i)
        num1[i] = 0;
    for (int i = 0; i <= num2[0]; ++i)
        num1[i] = num2[i];
}
int cmp(int num1[], int num2[]) {
    if (num1[0] > num2[0])
        return 1;
    if (num1[0] < num2[0])
        return -1;
    for (int i = num1[0]; i; --i)
    {
        if (num1[i] > num2[i])
            return 1;
        if (num1[i] < num2[i])
            return -1;
    }
    return 0;
}
void init() {
    scanf("%s%s", ch1, ch2);
    if (ch1[0] == '-')
        ch1[0] = '0', f1 = 1;
    if (ch2[0] == '-')
        ch2[0] = '0', f2 = 1;
    int l1 = strlen(ch1), l2 = strlen(ch2);
    for (int i = l1 - 1; i >= 0; i -= siz)
    {
        int pw = 1;
        ++a[0];
        for (int j = i; j > i - siz && j >= 0; --j)
        {
            a[a[0]] += (ch1[j] ^ 48) * pw;
            pw = (pw << 3) + (pw << 1);
        }
    }
    for (int i = l2 - 1; i >= 0; i -= siz)
    {
        int pw = 1;
        ++b[0];
        for (int j = i; j > i - siz && j >= 0; --j)
        {
            b[b[0]] += (ch2[j] ^ 48) * pw;
            pw = (pw << 3) + (pw << 1);
        }
    }
    return;
}
void pls(int a[], int b[]) {
    if (f1 ^ f2) {
        if (f1)
            f1 ^= 1, sub(b, a), f1 ^= 1;
        if (f2)
            f2 ^= 1, sub(a, b), f2 ^= 1;
        return;
    }
    if (f1 & f2) {
        f1 = f2 = 0, f ^= 1, pls(a, b);
        f1 = f2 = 1;
        return;
    }
    clear(s);
    s[0] = max(a[0], b[0]) + 1;
    for (int i = 1; i <= s[0]; ++i)
    {
        s[i] += a[i] + b[i];
        if (s[i] >= MOD)
            s[i] -= MOD, ++s[i + 1];
    }
    if (!s[s[0]] && s[0] > 1)
        --s[0];
    return;
}
void sub(int a[], int b[]) {
    if (f1 ^ f2)
    {
        if (f1)
            f1 ^= 1, f ^= 1, pls(a, b);
        if (f2)
            f2 ^= 1, pls(a, b);
        return;
    }
    if (f1 & f2) {
        f1 = f2 = 0, sub(b, a);
        f1 = f2 = 1;
        return;
    }
    if (cmp(a, b) == -1)
        swap(a, b), f ^= 1;
    clear(s);
    s[0] = max(a[0], b[0]);
    for (int i = 1; i <= s[0]; ++i) {
        s[i] += a[i] - b[i];
        if (s[i] < 0)
            s[i] += MOD, --s[i + 1];
    }
    while (!s[s[0]] && s[0] > 1)
        --s[0];
    return;
}

void mul(int a[], int b[]) {
    if (f1 ^ f2)
        f ^= 1;
    clear(s);
    s[0] = a[0] + b[0];
    for (int i = 1; i <= a[0]; ++i)
    {
        for (int j = 1; j <= b[0]; ++j)
        {
            s[i + j - 1] += a[i] * b[j];
            if (s[i + j - 1] >= MOD)
                s[i + j] += s[i + j - 1] / MOD, s[i + j - 1] %= MOD;
        }
    }
    if (!s[s[0]] && s[0] > 1)
        --s[0];
    return;
}
void div(int a[], int b[]) {
    if (f1 ^ f2)
        f ^= 1;
    clear(cp), cp[1] = 1;
    clear(lt);
    while (cmp(a, b) != -1)
        ly(b), ly(cp);
    while (cp[0] > 1 || cp[1])
    {
        if (cmp(a, b) != -1)
        {
            sub(a, b), cpy(a, s);
            pls(lt, cp), cpy(lt, s);
        }
        ry(b), ry(cp);
    }
    cpy(s, lt), cpy(lt, a);
    return;
}
signed main() {
    IOS;
    init();
    clear(s);
    pls(a, b);
    write(s);
    return 0;
}

|