#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
main()
{
char temp,n[MAX],m[MAX],result[MAX*2+1];
int lenn,lenm,i,now,x,high,low,place;
scanf("%s%s",n,m);
lenm=strlen(m);
lenn=strlen(n);
for (i=0;i<MAX*2+1;i++) result[i]='0';
/*将字符串倒置 */
for (i=0;i<lenn/2;i++){
temp=n[i];
n[i]=n[lenn-i-1];
n[lenn-i-1]=temp;
}
for (i=0;i<lenm/2;i++){
temp=m[i];
m[i]=m[lenm-i-1];
m[lenm-i-1]=temp;
}
for (i=0;i<(lenm>lenn?lenm:lenn);i++){
now=n[i]+m[i]-96;
place=i;
do{
high=(now+result[place]-48)/10;
low=(now+result[place]-48)%10;
result[place++]=(low+48);
now=high;}
while(now==1);
} result[place]='\0';
for (i=0;i<strlen(result)/2;i++){
temp=result[i];
result[i]=result[strlen(result)-i-1];
result[strlen(result)-i-1]=temp;
}
printf("%s\n\n",result);printf("%d",'.');
system("pause");
}
输入某 些数有错误
----------------解决方案--------------------------------------------------------
你的算法有问题
我帮你改了下,你看看区别
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main(void)
{
char temp,n[MAX+1],m[MAX+1],result[MAX+1+1];
int lenn,lenm,i,now,x,high,low,place, indexS, indexB;
int flag = 0;
memset(n, 0, MAX + 1);
memset(m, 0, MAX + 1);
printf("Please enter two integers, no more than 100 digits in each one:\n");
scanf("%s%s",n,m);
lenm=strlen(m);
lenn=strlen(n);
// for (i=0;i<MAX*2+1;i++) result[i]='0';
memset(result, 0, MAX+1+1); // initialize the result array
/*将字符串倒置 */
//printf("DEBUG: Before reversion, the first integer is %s\n", n);
for (i=0;i<lenn/2;i++)
{
temp=n[i];
n[i]=n[lenn-i-1];
n[lenn-i-1]=temp;
}
//printf("DEBUG: After reversion, the first integer is %s\n", n);
//printf("DEBUG: Before reversion, the second integer is %s\n", m);
for (i=0;i<lenm/2;i++)
{
temp=m[i];
m[i]=m[lenm-i-1];
m[lenm-i-1]=temp;
}
//printf("DEBUG: After reversion, the second integer is %s\n", m);
indexS = lenm < lenn ? lenm : lenn;
indexB = lenm > lenn ? lenm : lenn;
high = 0;
low = 0;
if (lenm > lenn)
flag = 1;
for (i=0;i<indexS;i++)
{
now=n[i]+m[i]-96;
now += high;
//printf("DEBUG: n[%d] = %d; m[%d] = %d; now = %d\n", i, n[i], i, m[i], now);
high = now / 10;
low = now % 10;
result[i] = low + 48;
// do
//{
// high=(now+result[place]-48)/10;
// low=(now+result[place]-48)%10;
// result[place++]=(low+48);
// now=high;
// }while(now==1);
}
for (; i < indexB; ++i)
{
now = high + (flag == 0) ? n[i] : m[i] -48;
//printf("DEBUG: n[%d] = %d; m[%d] = %d; now = %d\n", i, n[i], i, m[i], now);
high = now / 10;
low = now % 10;
result[i] = low + 48;
}
if (high != 0)
{
result[i] = high + 48;
//result[++i] = '\0';
}
//else
//result[i] = '\0';
// result[place]='\0';
/*
for (i=0;i<strlen(result)/2;i++)
{
temp=result[i];
result[i]=result[strlen(result)-i-1];
result[strlen(result)-i-1]=temp;
}
printf("%s\n\n",result);
*/
//printf("%d",'.');
printf("Result is :\n\t");
for (; i >= 0; --i)
putchar(result[i]);
putchar('\n');
return 0;
}
----------------解决方案--------------------------------------------------------
你的这个也不对啊
----------------解决方案--------------------------------------------------------
我测试过的用例结果都是对的,你给个不能正确获得结果的例子出来
----------------解决方案--------------------------------------------------------
MS没有这么难吧.做的就是数字数组的加法.
输入时将字符串替换成数字(倒过来替过来)然后就是从低往高加.
----------------解决方案--------------------------------------------------------
定义的变量还真多,要看程序,我要把每个变量先记明白!
----------------解决方案--------------------------------------------------------
[CODE]
#include<stdio.h>
typedef struct Big_Num{
int data[1000];
int length;
};
void Add(Big_Num &a,Big_Num &b)
{
int i,t=0;//进位
for(i=0;i<a.length&&i<b.length;i++)
{
int temp=a.data[i]+b.data[i]+t;
a.data[i]=temp%10;
t=temp/10;
}
while(i<a.length)
{
int temp=a.data[i]+t;
a.data[i]=temp%10;
t=temp/10;
i++;
}
while(i<b.length)
{
int temp=b.data[i]+t;
a.data[i]=temp%10;
t=temp/10;
i++;
}
a.length=i;
}
void Print(Big_Num a)
{
for(int i=a.length-1;i>=0;i--)
{
printf("%d",a.data[i]);
}
printf("\n");
}
int main()
{
Big_Num a={{1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3},17},b={{3,5,7,9,3,1,4,6,8,4,2,6,3,5,4,1,4,6},18};
Add(a,b);
Print(a);
return 0;
}
----------------解决方案--------------------------------------------------------
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
main()
{
char temp,n[MAX],m[MAX],result[MAX];
int lenn,lenm,i,now,x,high,low,place;
scanf("%s%s",n,m);
lenm=strlen(m);
lenn=strlen(n);
for (i=0;i<MAX;i++) result[i]='0';
/*将字符串倒置 */
for (i=0;i<lenn/2;i++){
temp=n[i];
n[i]=n[lenn-i-1];
n[lenn-i-1]=temp;
}
for (i=0;i<lenm/2;i++){
temp=m[i];
m[i]=m[lenm-i-1];
m[lenm-i-1]=temp;
}
for (i=lenn;i<MAX;i++) n[i]='0';
for (i=lenm;i<MAX;i++) m[i]='0';
i=0;
for (i=0;i<((lenn>lenm)?lenn:lenm);i++){
now=n[i]+m[i]-96;
place=i;
do{
high=(now+result[place]-48)/10;
low=(now+result[place]-48)%10;
result[place++]=(low+48);
now=high;}
while(now>0);
}
result[place]='\0';
for (i=0;i<strlen(result)/2;i++){
temp=result[i];
result[i]=result[strlen(result)-i-1];
result[strlen(result)-i-1]=temp;
}
printf("%s\n\n",result);
system("pause");
}
正确
----------------解决方案--------------------------------------------------------
result[MAX];
==> result[MAX + 1];
最高位可能会产生进位
----------------解决方案--------------------------------------------------------
已经改了
刚刚改的,谢谢了
----------------解决方案--------------------------------------------------------