算法练习(一)(HDU1002 A+B Problem Ⅱ 大数运算)

HDU1002 A+B Problem Ⅱ 

https://vjudge.net/contest/310259#problem/B

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
	char chr1[1010],chr2[1010];
	int t;
	int len1,len2,len;
	cin>>t;
    for(int s=1;s<=t;s++){
    	if(s!=1) cout<<endl;
		int i,j=0,k=0;
		scanf("%s %s",chr1,chr2);
		int a[1010]={0},b[1010]={0},c[1010]={0};
		len1=strlen(chr1);
		len2=strlen(chr2);
		for(i=len1-1,j=0;i>=0;i--) a[j++]=chr1[i]-'0';
		for(i=len2-1,k=0;i>=0;i--) b[k++]=chr2[i]-'0';
		if(len1>len2) len=len1;
		else len=len2;
		int m=0;
		for(i=0;i<len;i++)
		{
		   int temp=a[i]+b[i]+m;
		   c[i]=temp%10;
		   m=temp/10;
		}
		if(a[i-1]+b[i-1]>=10) c[i]=1;
		else {
			c[i]=0;
			i--;
		}
		printf("Case %d:\n",s);
		printf("%s + %s = ",chr1,chr2);
		for(;i>=0;i--) cout<<c[i];
		cout<<endl;
	}
	return 0;
 }  
posted @ 2019-12-21 12:05  yyer  阅读(176)  评论(0编辑  收藏  举报