两个超长数字相加,高手进来修改!
飞天小鱼 2005-10-16 12:51:35 两个超长数字相加,高手进来修改!例如:12345678910236544786665+78994564165746515374897=?
我自己写的程序有点问题,请修改,谢谢!
#include<iostream.h>
void add(int result[11], int buf1[10], int buf2[9])
{
int len =10; //strlen(buf1)<strlen(buf2) ? strlen(buf1): strlen(buf2);
result[9] = (buf1[9] + buf2[8])%10;
for(int i=len-1; i>=0; i--)
{
// result[i] = (buf1[i] + buf2[i-1] ) % 10 +'0';
// result[i-1] = (buf1[i-1] + buf2[i-2] ) / 10 +'0';
// result[i-1]+= (buf1[i] + buf2[i] ) % 10 +'0';
// return result[len];
int a= (buf1[i] + buf2[i-1])/10;
result[i-1] = (buf1[i-1] + buf2[i-2] + a)%10;
}
for(i=0;i<len;i++)
{
cout<<result[i];
}
// return result;
}
void main()
{
int a1[10]={1,2,3,4,5,6,7,8,9,2};
int a2[9]={1,2,3,4,5,6,7,8,9};
int a3[10];
add(a3,a1,a2);
}