大數加法 模版

 c++ 代碼,寫的不錯,留下來當模版生氣

#include<iostream>
#include<string>
using namespace std;
string sum(string s1,string s2) 
{ 
     if(s1.length()<s2.length()) 
     { 
          string temp=s1; 
          s1=s2; 
          s2=temp; 
     } 
     for(int 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()
{  
	int n,count=1;
	string s1,s2;
	cin>>n;
	while(n--)
	{
	cin>>s1>>s2;
	cout<<"Case "<<count++<<':'<<endl;
	cout<<s1<<" + " <<s2<<" = "<<sum(s1,s2)<<endl;
	if(n>0) cout<<endl;
	}
	return 0;
}



 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章