问题 C: A+B Problem II

题目描述

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

A,B must be positive.

输入

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

输出

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation.

样例输入

2
1 2
112233445566778899 998877665544332211

样例输出

Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

 1 /*对于此题做法有两种:
 2  其一,使2字符串的中的字符数字减去‘0‘,逐个相加大于等于10的可以使本位减10,下一位自增1,后面的处理就非常简单了;
 3  其二,便是读入字符串后先让各个字符减‘0‘,一一对应存入整形数组中;之后再相加。
 4     对于2种方法大致是相同的,都要从后面向前加,逢十进位,以及数组初始化均要初始为0,一边方便运算。
 5  下面用法一,代码如下:*/
 6 #include<stdio.h>
 7 #include<string.h>
 8
 9 int main( ) {
10     int n, i,len1, len2, j, k, pi, t;
11     char str1[1010], str2[1010];
12     scanf("%d", &n);
13     for(i = 1; i <= n; i++) {
14         int a[1200] = {0};
15         int flag = 0;
16         scanf("%s%s", str1, str2);//以字符串形式读入
17         printf("Case %d:\n", i);
18         len1 = strlen(str1);
19         len2 = strlen(str2);
20         printf("%s + %s = ", str1, str2);
21         j = len1-1;
22         k = len2-1;
23         pi = 0;
24         while(j >= 0 && k >= 0) { //开始相加
25             if(a[pi] + (str1[j]-‘0‘) + (str2[j]-‘0‘) >= 10) { //相加后大于10的
26                 a[pi] = a[pi] + (str1[j] -‘0‘)+(str2[k] - ‘0‘)-10;
27                 a[pi+1]++;
28             }
29             else
30                 a[pi] = a[pi] + (str1[j] - ‘0‘)+(str2[k]-‘0‘);
31             pi++; k--; j--;
32         }
33         if(j>=0) {
34             for(t = j; t >= 0; t--) {
35                 a[pi] = a[pi]+(str1[t]-‘0‘);
36                 pi ++;
37             }
38         }
39         else if(k >= 0) {
40             for(t = k; t >= 0; t--) {
41                 a[pi] = a[pi] + str2[t]-‘0‘;
42                 pi++;
43             }
44         }
45         else if(a[pi]!=0)//对于位数相同2个数加后最高位大于10的
46             pi++;
47         for(t = pi - 1; t >= 0; t--) {
48             if(a[t]==0&&flag==0)
49                 continue;
50             else
51             {
52                 flag=1;
53                 printf("%d", a[t]);
54             }
55         }
56         if(i!=n)//对于2组之间加空行的情况
57             printf("\n");
58     }
59     return 0;
60 }

时间: 2025-01-06 04:05:28

问题 C: A+B Problem II的相关文章

杭电1023Train Problem II

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1023 题目: Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7675    Accepted Submission(s): 4131 Problem Description As we all know the

Train Problem II 卡特兰裸题(入门题)

Train Problem II  题目大意:给你一个数n,表示有n辆火车,编号从1到n,从远方驶过来,问你有多少种出站的可能. 解题思路:模拟栈的问题而已.  卡特兰问题. 1 import java.math.*; 2 import java.util.*; 3 import java.io.*; 4 5 public class Main 6 { 7 static int MS=101; 8 public static void main(String[] args) 9 { 10 Sca

hdoj 1002 A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 242959    Accepted Submission(s): 46863 Problem Description I have a very simple problem for you. Given two integers A and B, you

【HDOJ 1002】A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 238517    Accepted Submission(s): 45969 Problem Description I have a very simple problem for you. Given two integers A and B, you

HDU 1002-A + B Problem II(大数类)

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 215173    Accepted Submission(s): 41521 Problem Description I have a very simple problem for you. Given two integers A and B, yo

hdoj 1023 Train Problem II 【卡特兰】+【高精度】

题意:询问有多少种进站出站的顺序. 经典卡特兰.我对卡特兰目前的认识就是有n个1和n个-1,组成一个为2n的数列的方式有多少种.这就跟火车进站出站类似, 至于具体的卡特兰数的介绍,百度解释的很详细. 代码1(c语言): /* h(n) = h(n-1)*(4*n-2)/(n+1); */ #include <stdio.h> #include <string.h> #define M 110 int s[M][M] = {0}, b[M]; void init(){ s[1][0]

A + B Problem II(杭电1002)

/*A + B Problem II Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test case

HDU 1002 A + B Problem II(大整数相加)

A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input c

hdu1002 A + B Problem II(大数题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 230247    Accepted Submission(s): 44185 Problem Description I have a very sim

A + B Problem II(大数加法)

一直格式错误,不想改了,没A 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 using namespace std; 6 7 int main() 8 { 9 int T,K=0; 10 scanf("%d",&T); 11 char a[1002],b[1002]; 12 int ta[1002],