经典例题——大整数加法(计蒜客—T1098)

原题链接: https://nanti.jisuanke.com/t/T1098

在这里插入图片描述
测试样例

Sample Input
22222222222222222222
33333333333333333333
Sample Output
55555555555555555555

解题思路: 大数加法,没什么好说的,注意进位,Python一步解决。(人生苦短,我爱Python。)

PythonAC代码

s1=int(input())
s2=int(input())
print(s1+s2)

标准C++AC代码

/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

string s1,s2;
int result[maxn];
int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>s1>>s2){
		int len1=s1.size();
		int len2=s2.size();
		int pos=0;
		int temp1,temp2,res=0,ans;
		while(pos<len1||pos<len2){
			if(pos>=len1){
				temp1=0;
			}
			else{
				temp1=s1[len1-1-pos]-'0';
			}
			if(pos>=len2){
				temp2=0;
			}
			else{
				temp2=s2[len2-1-pos]-'0';
			}
			ans=res+temp1+temp2;
			if(ans>=10)
				res=1;
			else
				res=0;
			result[pos]=ans%10;
			pos++;
		}
		if(res==1){
			result[pos]=1;
			pos++;
		}
		bool flag=false;
		//判断第一个是不是0.
		per(i,pos-1,0){
			if(flag==false&&result[i]!=0)
				flag=true;
			if(flag)
				cout<<result[i];
		}
		if(!flag){
			//说明都是0,则输出0.
			cout<<0;
		}
		cout<<endl;
	}
	return 0;
}

posted @   unique_pursuit  阅读(84)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示