HDU-1753: Ming A + B (a little more careful patient)

Problem Description

Saying that after a long month, Xiao Ming has grown a lot, so he changed a name called "Ming."
At this time he is not only do that within 100 addition that "Bob", and now he'll even positive addition to any fractional length.

Now, you give two positive decimal A and B, your task is to calculate the value representative of the Ming of A + B.

Entry

This topic contains multiple sets of test data, the processing to the end of the file.
Each set of test data is included in the length of a line which is not greater than two decimal positive and B. A 400

Export

Please output value A + B in a row inside, output a simplest form. See detailed requirements Sample Output.

SAMPLE INPUT

1.1 2.9
1.1111111111 2.3444323343
1 1.1

Sample Output

4
3.4555434454
2.1

This code really written spit, or need to own the idea of ​​a stroke stroked it, in fact, there are some code that extra space, you can optimize it. Not sure what went wrong, then, in fact, you can look for other samples to test.

AC Code

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
#define ll long long
using namespace std;
char a[505],b[505];//存储输入 
int a1[505],b1[505];
int sum[505];//存储最后结果 
int main()
{
	while(~scanf("%s %s",a,b))
	{
		memset(sum,0,sizeof(sum)); 
		int p,q,pd,qd;
		p=q=-1;
		pd=qd=0;//判断有没有小数点 
		int kk = -1;//用于位数的计数 
		int i=0;
		// 先将小数点后面的进行加法运算 
		while(a[i]!='.'&&a[i]) i++;
		if(a[i]=='.')
		{
			pd=1;
			for(i=i+1;a[i];i++) a1[++p]=a[i]-'0';
		}
		i=0;
		while(b[i]!='.'&&b[i]) i++;
		if(b[i]=='.')
		{
			qd=1;
			for(i=i+1;b[i];i++) b1[++q]=b[i]-'0';
		}
		//小数点后面的位数可能不一样,不一样的直接后面补零 
		if(p<q)
		{
			for(int i=p+1;i<=q;i++) a1[i]=0;
			p=q;
		}
		else if(p>q)
		{
			for(int i=q+1;i<=p;i++) b1[i]=0;
			q=p;
		}
		//小数点后面的相加 
		for(int i=p;i>=0;i--)
		{
			sum[++kk]+=a1[i]+b1[i];
			if(sum[kk]>9&&i!=0)
			{
				sum[kk+1]++;
				sum[kk]-=10;
			}
		}
		//存储小数点前面的数 
		p=q=-1;
		for(int i=0;a[i]!='.'&&a[i];i++) a1[++p]=a[i]-'0';
		for(int i=0;b[i]!='.'&&b[i];i++) b1[++q]=b[i]-'0';
		//小数点后面可能需要进位 
		if(sum[kk]>9)
		{
			sum[kk+1]++;
			sum[kk]-=10;
		}
		kk++;
		sum[kk+1]=sum[kk];
		sum[kk]=-1;//标志小数点的位置 
		
		if(kk==-1) kk=0;//可能没有小数点
		//小数点前面的数字相加 
		if(p==q)
		{
			
			for(int i=p;i>=0;i--)
			{
				sum[++kk]+=a1[i]+b1[i];
				if(sum[kk]>9)
				{
					sum[kk+1]++;
					sum[kk]-=10;
				}
			}
		}
		//前面的位数也可能不等
		else if(p>q)
		{
			while(p>=0&&q>=0)
			{
				sum[++kk]+=a1[p]+b1[q];
				if(sum[kk]>9)
				{
					sum[kk+1]++;
					sum[kk]-=10;
				}
				p--;
				q--;
			}
			 
			for(int i=p;i>=0;i--)
			{
				sum[++kk]+=a1[i];
				if(sum[kk]>9)
				{
					sum[kk+1]++;
					sum[kk]-=10;
				}
			}
		}
		else if(p<q)
		{
			while(p>=0&&q>=0)
			{
				sum[++kk]+=a1[p]+b1[q];
				if(sum[kk]>9)
				{
					sum[kk+1]++;
					sum[kk]-=10;
				}
				p--;
				q--;
			}
			
			for(int i=q;i>=0;i--)
			{
				sum[++kk]+=b1[i];
				if(sum[kk]>9)
				{
					sum[kk+1]++;
					sum[kk]-=10;
				}
			}
		}
		
		int j=0;
		while(sum[j]==0) j++;//去除后导零 
		if(sum[kk+1]) kk++;//最后相加的时候产生了进位,位数要加1 
		if(sum[kk]==0&&sum[kk-1]!=-1) kk--;//去除后导零 
		for(int i=kk;i>=j;i--)
		{
			if(sum[i]==-1&&i!=j) printf(".");
			else if(sum[i]!=-1) printf("%d",sum[i]);
		}
		printf("\n");
	}
	return 0;
}
Released six original articles · won praise 12 · views 968

Guess you like

Origin blog.csdn.net/qq_43550449/article/details/104077104