思路是先比较a和b的大小是不是位数相同,然后位数小的加前导0直到位数相同为止,然后倒序相加记录进位的数就行了。

我的代码(思路借鉴别人)

#include <iostream>
#include <algorithm>
using namespace std;
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		string a,b;
		string aa,bb;
		cin>>a>>b;
		aa=a;bb=b;
		if(a.size()>b.size())
		{
			for(int i=0;a.size()!=b.size();i++)
			b='0'+b;	
		}
		else if(a.size()<b.size())
		{
			for(int i=0;a.size()!=b.size();i++)
			a='0'+a;	
		}	
		reverse(a.begin(),a.end());
		reverse(b.begin(),b.end());
		string c="";
		int jw=0;
		int num;
		for(int i=0;i<a.size();i++)
		{
			num=a[i]-'0'+b[i]-'0'+jw;
			jw=num/10;
			num%=10;
			c=char(num+'0')+c;
		}
		if(jw!=0)
		c=char(jw+'0')+c;
		cout<<"Case "<<i<<":"<<endl;
		cout<<aa<<" + "<<bb<<" = "<<c<<endl;
		if(i!=n)
		cout<<endl;
	} 
} 

相关文章:

  • 2021-06-25
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
猜你喜欢
  • 2021-09-17
  • 2021-11-17
  • 2022-12-23
  • 2021-06-09
  • 2021-12-18
  • 2021-10-12
  • 2021-08-15
相关资源
相似解决方案