hdu_1002_大数

A + B Problem II


Time
Limit: 2000/1000 MS (Java/Others)    Memory Limit:
65536/32768 K (Java/Others)
Total Submission(s):
201400    Accepted Submission(s):
38599

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

大数问题,总觉得自己写的麻烦了,不解释。。。Java 和 C ~~~~

Java 代码:


 1 import java.math.BigDecimal;
2 import java.util.*;
3
4 public class Main {
5 public static void main(String[] args) {
6 Scanner cin = new Scanner(System.in);
7 int n = cin.nextInt();
8 for (int i = 1; i <= n; ++i) {
9
10 BigDecimal a1, b1, c1;
11 a1 = cin.nextBigDecimal();
12 b1 = cin.nextBigDecimal();
13 c1 = a1.add(b1);
14 System.out.println("Case " + i + ":");
15 System.out.println(a1 + " + " + b1 + " = " + c1);
16 if (i < n)
17 System.out.println();
18 }
19 }
20 }

C代码:


 1 #include <cstdio>
2 #include <iostream>
3 #include <string.h>
4 using namespace std;
5
6 #define N 1000
7 int main()
8 {
9 int T, f;
10 int m, n, temp;
11 char A[N], B[N];
12 int sum[N+1], t[N];
13
14 cin>>T;
15 f=T;
16
17 while(T--)
18 {
19 scanf("%s",A);
20 scanf("%s",B);
21
22 m = strlen(A);
23 n = strlen(B);
24
25 if(m==n) {
26
27 for(int i = m-1; i>=0; i--) t[i]=A[i]+B[i]-‘0‘-‘0‘;
28
29 for(int i = m-1; i>=0; i--) {
30
31 temp = t[i]/10;
32 sum[i+1] = t[i]%10;
33 if(temp && i) t[i-1] += temp;
34 if(i==0) sum[0] = temp;
35 }
36
37 printf("Case %d:\n",f-T);
38 printf("%s + %s = ",A,B);
39
40 if(sum[0]) printf("%d", sum[0]);
41
42 for(int i = 1; i < m; i++) printf("%d", sum[i]);
43
44 printf("%d\n",sum[m]);
45 }
46 else if(m>n) {
47
48 for(int i = m-1; i>=0; i--) t[i]=A[i]-‘0‘;
49 for(int i = m-1; n>=m-i; i--) t[i]+=B[n-m+i]-‘0‘;
50
51 for(int i = m-1; i>=0; i--) {
52
53 temp = t[i]/10;
54 sum[i+1] = t[i]%10;
55 if(temp && i) t[i-1] += temp;
56 if(i==0) sum[0] = temp;
57 }
58
59 printf("Case %d:\n",f-T);
60 printf("%s + %s = ",A,B);
61
62 if(sum[0]) printf("%d", sum[0]);
63
64 for(int i = 1; i < m; i++) printf("%d", sum[i]);
65
66 printf("%d\n",sum[m]);
67
68 }
69 else {
70
71 for(int i = n-1; i>=0; i--) t[i]=B[i]-‘0‘;
72 for(int i = n-1; m>=n-i; i--) t[i]+=A[m-n+i]-‘0‘;
73
74 for(int i = n-1; i>=0; i--) {
75
76 temp = t[i]/10;
77 sum[i+1] = t[i]%10;
78 if(temp && i) t[i-1] += temp;
79 if(i==0) sum[0] = temp;
80 }
81
82 printf("Case %d:\n",f-T);
83 printf("%s + %s = ",A,B);
84
85 if(sum[0]) printf("%d", sum[0]);
86
87 for(int i = 1; i < n; i++) printf("%d", sum[i]);
88
89 printf("%d\n",sum[n]);
90
91 }
92 if(T) printf("\n");
93 }
94 return 0;
95 }

hdu_1002_大数,布布扣,bubuko.com

时间: 2024-11-06 06:23:41

hdu_1002_大数的相关文章

最短的计算大数乘法的c程序

#include <stdio.h> char s[99],t[99]; int m,n; void r(int i,int c) { int j=0,k=i; while(k)c+=s[j++]*t[k---1]; if(i)r(i-1,c/10); printf("%d",c%10); } void main() { gets(s);gets(t); while(s[n])s[n++]-=48; while(t[m])t[m++]-=48; r(m+n-1,0); }

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

nyoj 73 比大小 【java大数】

java大数. 代码: import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger a, b; BigInteger t = BigInteger.valueOf(0); a = cin.nextBigInteger(); b = cin.nextBi

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

各类大数模板

ps:转自http://blog.csdn.net/y990041769/article/details/20116995 大数加法模板(本人验证过,其他还没用,只是写在这) 1 string sum(string s1,string s2) 2 { 3 if(s1.length()<s2.length()) 4 { 5 string temp=s1; 6 s1=s2; 7 s2=temp; 8 } 9 int i,j; 10 for(i=s1.length()-1,j=s2.length()-

【51Nod】1005 大数加法

给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B Input示例 68932147586 468711654886 Output示例 537643802472 ==================================================================================================== 问题解法

HDU 1018 大数(求N!的位数/相加)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35382    Accepted Submission(s): 16888 Problem Description In many applications very large integers numbers are required. Some of these

java-两个大数相加

题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a="8888899999999888";  String b="88888888888888";  String str=new BigInteger(a).add(new BigInteger(b)).toString();  System.out.println(str);

大数乘法

1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 相关问题 大数加法 0 大数开平方 640 大数进制转换 320 大数除法 320 大数乘法 V2 80 代码: 1 #inclu