杭州电子科技大学(HDU)ACM刷题----------大数相加之1002详解

#include 
#include 
using namespace std;

string BigNum(string str1,string str2){    //大数相加
	int i,j;
	if (str1.size()=0;i--,j--)      //str1.size()-1是因为str1从0开始计数
	{
		str1[i]=(str1[i]+(j>=0? str2[j]-'0':0));            //利用问号语句判断位数较短的str2的位数是否达到上限
		if (str1[i]-'0'>=10)
		{
				str1[i]=(((str1[i]-'0')%10)+'0');      
			if (i!=0)               //当i不为0时就执行str1[i-1]语句
			{
				str1[i-1]++;    
			}else{
				str1='1'+str1;  //最高位若还有进位就加上进位1,此处不能写作str1+='1',string类相加是直接把多个string语句按照写法顺序“嫁接”
			}
		}
	}
	return str1;
}

int main()
{
	int n;
	cin>>n;
	int flag=1;
	while (n--)
	{
		string result;
		string str1,str2;
		cin>>str1>>str2;
		result=BigNum(str1,str2);
		cout<<"Case "<

你可能感兴趣的:(算法与数据结构,数据结构与算法)