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

hdu_1003

时间:2014-12-29 08:52:24      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

 1 // hdu 1002
 2 // high precision 
 3 // Dec.28 2014
 4 
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <algorithm>
 8 
 9 #define MaxL 1111
10 
11 int T, a[MaxL], b[MaxL], s[MaxL], a_len, b_len, s_len;
12 char str_a[MaxL], str_b[MaxL];
13 
14 int main(int argc, char const *argv[])
15 {
16     scanf("%d",&T);
17     for(int case_num = 1; case_num <= T; ++case_num){
18         a_len = b_len = 0;
19         // get big number by character string as buffer
20         scanf("%s", str_a);
21         scanf("%s", str_b);
22         memset(a, 0, sizeof(a));
23         memset(b, 0, sizeof(b));
24         memset(s, 0, sizeof(s));
25         // read the number into int array in reverse order
26         for(int i = strlen(str_a)-1; i >= 0; --i)
27             a[++a_len] = str_a[i] - 0;
28         for (int i = strlen(str_b)-1; i >= 0; --i)
29             b[++b_len] = str_b[i] - 0;
30         // s_len is the longer one between a and b
31         s_len = std::max(a_len, b_len);
32         int carry = 0;
33         
34         for(int i = 1; i <= s_len; ++i){
35             int temp = a[i] + b[i] + carry;
36             carry = temp / 10 ;
37             s[i] = temp % 10;
38         }
39         if(carry > 0)
40             s[++s_len] = carry;
41         if(case_num > 1)
42             printf("\n");
43         printf("Case %d:\n%s + %s = ", case_num, str_a, str_b);
44         for(int i = s_len; i > 0; --i)
45             printf("%d", s[i]);
46         printf("\n");
47     }
48     return 0;
49 }

 

hdu_1003

标签:

原文地址:http://www.cnblogs.com/takeoffyoung/p/4190951.html

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