跳到正文

休闲·娱乐

堆在屎山里的P1001

Ocean2约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年5月18日
更新
2026年5月18日
篇幅
约 7 分钟

版本

  1. v1当前

事先声明,作者蒟蒻,刚学完屎山(线段树)不久,跑来拿核弹炸只蚊子,不喜莫喷


P1001 A+B Problem

题目描述

输入两个整数 a,ba,b,输出它们的和。

注意:

  1. Pascal 使用 integer 会爆掉哦!
  2. 有负数哦!
  3. C/C++ 的 main 函数必须是 int 类型。程序正常结束时的返回值必须是 0。这不仅对洛谷其他题目有效,而且也是 NOIP/CSP/NOI 比赛的要求!

好吧,同志们,我们就从这一题开始,向着大牛的路进发。

任何一个伟大的思想,都有一个微不足道的开始。

输入格式

输入两个以空格分隔的整数 a,ba,b。

输出格式

输出一个整数,表示 a+ba+b。

输入输出样例 #1

输入 #1

代码
20 30

输出 #1

代码
50

说明/提示

数据范围

对于所有测试数据,−109≤a,b≤109-{10}^9 \le a,b \le {10}^9。


题目看完了,来开始做题:

A+B\color{red} A+B Problem\color{red} Problem

C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int a,b;
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

A+B\color{orange} A+B Problem\color{orange} Problem

C++
#include <bits/stdc++.h>
using namespace std;
int add(int a,int b)
{
    while(b!=0)
    {
        unsigned int carry=(unsigned int)(a&b)<<1;
        a^=b;
        b=carry;
    }
    return a;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int a,b;
    cin>>a>>b;
    cout<<add(a, b);
    return 0;
}

A+B\color{yellow} A+B Problem\color{yellow} Problem

C++
#include <bits/stdc++.h>
using namespace std;
string removeLeadingZeros(string s)
{
    int i=0;
    while(i<s.length()-1&&s[i]== '0') i++;
    return s.substr(i);
}
bool isGreaterOrEqual(string a,string b)
{
    if (a.length()!=b.length()) return a.length()>b.length();
    return a>=b;
}
string addStrings(string a,string b)
{
    a=removeLeadingZeros(a);
    b=removeLeadingZeros(b);
    if (a.length()<b.length()) swap(a, b);
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    string result="";
    int carry=0;
    for(int i=0;i<a.length();i++)
    {
        int digitA=a[i]-'0';
        int digitB=(i<b.length())?(b[i]-'0'):0;
        int sum=digitA+digitB+carry;
        carry=sum/10;
        result.push_back((sum%10)+'0');
    }
    if(carry) result.push_back(carry+'0');
    reverse(result.begin(),result.end());
    return result;
}
string subtractStrings(string a,string b)
{
    a=removeLeadingZeros(a);
    b=removeLeadingZeros(b);
    reverse(a.begin(),a.end());
    reverse(b.begin(),b.end());
    string result="";
    int borrow=0;
    for(int i=0;i<a.length();i++)
    {
        int digitA=a[i]-'0'-borrow;
        int digitB=(i<b.length())?(b[i]-'0'):0;
        if (digitA<digitB)
        {
            digitA+=10;
            borrow=1;
        }
        else
        {
            borrow=0;
        }
        int diff=digitA-digitB;
        result.push_back(diff+'0');
    }
    while(result.length()>1&&result.back()=='0') result.pop_back();
    reverse(result.begin(),result.end());
    return result;
}
string highPrecisionAdd(string a,string b)
{
    bool aNeg=(a[0]=='-');
    bool bNeg=(b[0]=='-');
    if(aNeg) a=a.substr(1);
    if(bNeg) b=b.substr(1);
    if(aNeg==bNeg)
    {
        string result=addStrings(a,b);
        if(aNeg) result="-"+result;
        return result;
    }
    if(isGreaterOrEqual(a,b))
    {
        string result=subtractStrings(a,b);
        if(aNeg) result="-"+result;
        return result;
    }
    else
    {
        string result=subtractStrings(b,a);
        if(bNeg) result="-"+result;
        return result;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    string a,b;
    cin>>a>>b;
    cout<<highPrecisionAdd(a,b);
    return 0;
}
原文