hdoj 1002A + B Problem II



/*A + B Problem II

Input

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.

Output

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.

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*/

<span style="font-size:18px;">import java.util.Scanner;
import java.math.BigInteger;
public class Main{  //OJ上用Java A题时要注意到一点的是类名必须是Main ,要不然会编译错误。
    public static void main(String[]args){
        Scanner scanner=new Scanner(System.in);
        BigInteger a,b,c;
        int t;
        t=scanner.nextInt();
        for(int i=1;i<=t;i++){
            a=scanner.nextBigInteger();
            b=scanner.nextBigInteger();
            c=a.add(b);
            System.out.println("Case "+i+":");
            System.out.println(a+" + "+b+" = "+c);
            if(i<t)
                System.out.println();
        }
    }
}  </span>

<span style="font-size:18px;"># include <stdio.h>
# include <string.h>
# define MAX 1100

int main()
{
 int t;
 char Num1[MAX], Num2[MAX], Num3[MAX];//Num3[] 用于保存结果
 scanf("%d", &t);
 for(int Count = 0; Count < t; Count++)
 {
  if(Count) printf("\n");
  scanf("%s %s", Num1, Num2);
  //获取两个数字的位数
  int Len1 = strlen(Num1);
  int Len2 = strlen(Num2);
  int Len3 = 0;
  memset(Num3, '0', sizeof(Num3));
  //按位相加 先不管进位
  for(int i = Len1 - 1, j = Len2 - 1; i >= 0 && j >= 0; i--, j--)
  {
   Num3[Len3++] = Num1[i] + Num2[j] - '0';
   //如果有一个数组先为空 则把另一个数组里剩下的数字放入Num3[]
   if(i == 0)
    while(j--) Num3[Len3++] = Num2[j];
   else if(j == 0)
    while(i--) Num3[Len3++] = Num1[i];
  }
  //处理进位
  for(int i = 0; i < Len3; i++)
  {
   if(Num3[i] > '9')
   {
    Num3[i + 1] += (Num3[i] - '0') / 10;
    Num3[i] = (Num3[i] - '0') % 10 + '0';
   }
  }
  //格式输出
  for(int i = MAX - 1; i >= 0; i--)
  {
   if(Num3[i] != '0')
   {
    printf("Case %d:\n", Count + 1);
    printf("%s + %s = ", Num1, Num2);
    while(i >= 0) printf("%c",Num3[i--]);
    printf("\n");
   }
  }
 }
 return 0;
}
</span>

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-15 21:25:34

hdoj 1002A + B Problem II的相关文章

ACM--大数相加--HDOJ 1002--A + B Problem II

HDOJ题目地址:传送门 A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 315080    Accepted Submission(s): 61120 Problem Description I have a very simple problem for you. Given two integer

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]

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 【卡特兰数】

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6928    Accepted Submission(s): 3750 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stat

HDOJ 1023 Train Problem II (卡塔兰数)

Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway. Input The input contains se

杭电1002-A + B Problem II

#include<stdio.h>#include<string.h> int main(){    char str1[1001],str2[1001];    int t,i,maxlen,len1,len2,k,num=1;    scanf("%d",&t);    getchar();    while(t--)    {        int a[1001]={0},b[1001]={0},c[1001]={0};        scanf(

【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

HDOJ 1002 A + B Problem II (Big Numbers Addition)

题目链接在此?http://acm.hdu.edu.cn/showproblem.php?pid=1002 这题也比较简单,只需要开三个长度为1000的char数组来分别储存a.b.ans,再利用我们加法的算法,先向右对齐再相加.注意一下进位时的特殊情况就好了. 不过笔者的代码写好后提交上去,两次Presentation Error,然后才发现只是最后多输出一个空行的问题  =.= Orz /**  * HDOJ 1002 A + B Problem II  * Big Numbers Addi

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