如何添加多位数? [英] How to add multi-digit number?

查看:93
本文介绍了如何添加多位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加:

12334568757678978709709880988987687576546346543432432531253153213515 + 987328746329887632987216389721634879231648971236498723146238644348364893274



  void  add( char  * a, char  * b, char  * c)
{

/ / 在此处编写代码
}

int _tmain( int argc,_TCHAR * argv [])
{
char a [ 1024 ];
char b [ 1024 ];
char c [ 2048 ];
printf( 输入第一个值);
scanf_s( %s,a);
scanf_s( %s,b);


add(a,b,c);

printf( c is%s,c);
return 0 ;
}

解决方案

首先要做的事情:c不需要是a和b的两倍,它只是需要是一个更大的字符,因为两个整数之和总是小于或等于最大值的两倍:

  9  +  9  =  18  
99 + 99 = 198
999 + 999 = 1998





其次,这并不困难,只是看起来就是这样!

首先想一想如何手动完成:

1)你把它们写下来,所以每个的最低位数都在同一个柱子中

 12334568757678978709709880988987687576546346543432432531253153213515 
+ 98732874 6329887632987216389721634879231648971236498723146238644348364893274



2)然后,您将添加最低有效数字,并使用结果模10作为结果数字,任何多余数字都是进位:

 12334568757678978709709880988987687576546346543432432531253153213515 
+ 987328746329887632987216389721634879231648971236498723146238644348364893274
___________________________________________________________________________
... 9
___________________________________________________________________________
Carry:0

3)然后你会为下一个数字对重复这个,包括每次进位,直到你用完数字。



所以试试:从字符串的右端并添加数字对。

这并不复杂 - 但它是你的功课,所以我给你无码!

how to add:
12334568757678978709709880988987687576546346543432432531253153213515 + 987328746329887632987216389721634879231648971236498723146238644348364893274

void add(char *a, char *b, char *c)
{

    // Write code here
}
 
int _tmain(int argc, _TCHAR* argv[])
{
    char a[1024];
    char b[1024];
    char c[2048];
    printf("enter First values");
    scanf_s("%s",a);
    scanf_s("%s",b);
   
 
    add(a,b,c);
 
    printf("c is %s",c);
    return 0;
}

解决方案

First things first: c doesn't need to be twice the size of a and b, it only needs to be one character larger as the sum of two integers is always less than or equal to double the largest value:

  9 +   9 =   18
 99 +  99 =  198
999 + 999 = 1998



Second, this isn't difficult, it just seems that way!
Start by thinking how you would do it manually:
1) Your write them down so the least significant digit of each was in the same columne

         12334568757678978709709880988987687576546346543432432531253153213515
+ 987328746329887632987216389721634879231648971236498723146238644348364893274


2) Then, you would add the least significant digits, and use the result modulo 10 as the result digit, with any excess being the carry:

         12334568757678978709709880988987687576546346543432432531253153213515
+ 987328746329887632987216389721634879231648971236498723146238644348364893274
  ___________________________________________________________________________
 ...                                                                        9
  ___________________________________________________________________________
Carry:                                                                     0 

3) You would then repeat this for the next digit pair, including the carry each time, until you ran out of digits.

So try that: start from the right end of the strings and add digit pairs.
It's not complex - but it's your homework, so I'll give you no code!


这篇关于如何添加多位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆