Week 1 # A A + B Problem II

原题描述:

A - 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.

InputThe 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. 
OutputFor 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. Output a blank line between two test cases.

Sample Input

2
1 2
112233445566778899 998877665544332211

Sample Output

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110第二次看到这个题目了,第一次是在杭电acm上,那时侯想了用数组,不过没做出来。其实这个题目不算难,我直接定义了两个个字符型数组用来存储算数,定义一个整形数组存储值。先判断那个数组长度大.两个字符型数组从后面相加,存储到整形数组上,如果值大于10再向前进1(不可能大于20)一直到短的那个数组为0的时候停止相加。直接把长的赋值到上面就可以了,记得进位!用长的字符型数组长度判断整形数组的长度。然后就可以输出了。代码复杂,但是浅显易懂。AC代码:
 1 #include <iostream>
 2 #include <string.h>
 3 const int N=1000;
 4 using namespace std;
 5 int main()
 6 {
 7     int t,l,m,n,sum[N+1]={0};
 8     int g=1;
 9     char a[N],b[N];
10     cin>>t;
11     while(t--)
12     {
13         int i;
14         cin>>a>>b;
15         m=strlen(a);
16         n=strlen(b);
17         l=m>=n?m:n;
18         if(m<=N&&n<=N)
19         {
20         if(m>=n)
21         {
22             for(i=0;n>0;i++)
23             {
24                 sum[i]=sum[i]+(a[m-1]-‘0‘)+(b[n-1]-‘0‘);
25                 m--;
26                 n--;
27                 if(sum[i]>9)
28                 {
29                     sum[i]-=10;
30                     sum[i+1]++;
31                 }
32             }
33             for( ;m>0;i++,m--)
34             sum[i]=sum[i]+(a[m-1]-‘0‘);
35         }
36         else
37          {
38             for(i=0;m>0;i++)
39             {
40                 sum[i]=sum[i]+(a[m-1]-‘0‘)+(b[n-1]-‘0‘);
41                 m--;
42                 n--;
43                 if(sum[i]>9)
44                 {
45                     sum[i]-=10;
46                     sum[i+1]++;
47                 }
48             }
49             for( ;n>0;i++,n--)
50             sum[i]=sum[i]+(b[n-1]-‘0‘);
51         }
52         }
53         if(sum[i]==0)
54         i--;
55         cout<<"Case "<<g<<":"<<endl;
56         g++;
57         cout<<a<<" + "<<b<<" = ";
58          for( ;i>=0;i--)
59         cout<<sum[i];
60         for(i=0;i<=l;i++)
61         sum[i]=0;
62         if(t!=0)
63         cout<<endl<<endl;
64         else cout<<endl;
65     }
66     return 0;
67 }
时间: 2024-10-11 02:19:23

Week 1 # A 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],