加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数的加法

发布时间:2020-12-14 02:35:24 所属栏目:大数据 来源:网络整理
导读:? ? ?大树的加法,实则用数组去进行处理。将两个数的每位相加的结果先保存在整形数组中,然后逐位进行结果的转换。代码如下 #include "stdio.h"#includestring.hint max(int x,int y){if (xy)return x;elsereturn y;}int main(){int a[510]={0},b[510]={0},c
? ? ?大树的加法,实则用数组去进行处理。将两个数的每位相加的结果先保存在整形数组中,然后逐位进行结果的转换。代码如下
#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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读