码迷,mamicode.com
首页 > 其他好文 > 详细

a+b

时间:2018-02-06 18:18:42      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:img   microsoft   main   col   target   思路   soft   image   post   

题目截图:

技术分享图片

 

思路:

  大整数加法,将 a,b 储存到数组中操作,具体看博文

 

代码如下:

 1 /*
 2     a+b
 3 */
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <math.h>
 8 #include <stdlib.h>
 9 #include <time.h>
10 #include <stdbool.h>
11 
12 int main() {
13     char stra[1001], strb[1001];                    // 用来输入 a,b 
14     int a[1001]={0}, b[1001]={0}, c[1002]={0};        // a+b=c 
15     int i, j=0, maxLen=0;                            // maxLen用来储存a,b较大的位数 
16     while(scanf("%s %s", stra, strb) != EOF) {
17         for(i=0; i<strlen(stra); ++i) {
18             a[i] = stra[strlen(stra)-1-i] - 0;    // 将 a 每一位分离并存储 
19         }
20         for(i=0; i<strlen(strb); ++i) {
21             b[i] = strb[strlen(strb)-1-i] - 0;    // 将 b 每一位分离并存储
22         }
23         // 求位数较大值 
24         maxLen = (strlen(stra) > strlen(strb)) ? strlen(stra) : strlen(strb);
25         for(i=0; i<maxLen; ++i) {                    // 大整数加法 
26             int temp = a[i] + b[i] + j;                // j 储存进位 
27             c[i] = temp%10;                            // 该位加法结果 
28             j = temp/10;                            // 新的进位 
29         }
30         if(j != 0) {                                // 还有进位 
31             c[i++] = j;
32         }
33         for(j=i-1; j>=0; --j) {                        // 按格式输出 
34             printf("%d", c[j]);
35         }
36         printf("\n");
37     }
38 
39     return 0;
40 }

 

a+b

标签:img   microsoft   main   col   target   思路   soft   image   post   

原文地址:https://www.cnblogs.com/coderJiebao/p/HustTest06.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!