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

hdoj 1002A + B Problem II

时间:2015-07-26 20:55:07      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:



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


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

hdoj 1002A + B Problem II

标签:

原文地址:http://blog.csdn.net/qjt19950610/article/details/47071187

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