大数的加法

     大树的加法,实则用数组去进行处理。将两个数的每位相加的结果先保存在整形数组中,然后逐位进行结果的转换。代码如下
#include "stdio.h"
#include<string.h>

int max(int x,int y)
{
	if (x>y)
		return x;
	else
		return y;
}
int main()
{
	int a[510]={0}, b[510]={0}, c[510]={0};
	int m, n, i, p, j;
	char str1[510], str2[510];
	while (scanf("%s %s",str1,str2) == 2)
	{
		m= strlen(str1);
		n= strlen(str2);
		p= max(m,n);
		for (i=0; i<p; i++)
		{
			a[m-1-i] = str1[i]- 48;
			b[n-1-i] = str2[i]- 48;
		}
		for (j=0; j<p; j++)
		{
			c[j] = a[j] + b[j];
		}
		for (i=0; i <p; i++)
		{
			c[i+1]= c[i]/10 +c[i+1];
			c[i]= c[i]%10;
		}
		if (c[p]==0)
		{
			for (i=p-1; i>=0; i--)
				printf("%d",c[i]);
		}
		else
		{
			for (i= p; i>=0; i--)
				printf("%d",c[i]);
		}
		printf("\n");
	}
	return 0;
}

转载于:https://www.cnblogs.com/Tovi/p/6194925.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值