NBUT-1228 J-Bored Three-God 前导0

Bored Three-God
时间限制: 1000 ms 内存限制: 65535 K
问题描述
The bored Three-God get another boring question.
This problem is ask you plus two big nubmer, please help him, when you solve this problem you can speak to Three-God,“Oh, Please give me a diffculte one, this one is too easy”.

输入
There are muti-case
For each case, there are two integers, n, m (0 < n, m < 10^10000).
输出
Calculate two integers’ sum
样例输入
1 1
2 3
10000 100000
样例输出
2
5
110000

这就是一道大数加法的模板题,但是要注意的是如果你输入含有前导0的话,输出的时候也一定要保留前导0,不能删去。
这道题的坑点就是在前导0上,因为题目中没有特别指出要保留,所以一直没想到WA在哪了,后来听说是要保留前导0的,崩溃。

AC代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string sum(string s1,string s2)
{
	if(s1.length()<s2.length())
	{
		string temp=s1;
		s1=s2;
		s2=temp;
	}
	int i,j;
	for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
	{
		s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));
		if(s1[i]-'0'>=10)
		{
			s1[i]=char((s1[i]-'0')%10+'0');
			if(i) s1[i-1]++;
			else s1='1'+s1;
		}
	}
	return s1;
}
int main()
{
    string s1,s2;
    while(cin>>s1>>s2)
    {
        cout<<sum(s1,s2)<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44137005/article/details/88957374