大数的加法运算,杭电oj-1002

原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1002

【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

【AC代码】

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define max(a, b) a > b ? a:b
 4
 5 char a[1024];
 6 char b[1024];
 7 char c[1024];
 8 int i;
 9
10 void reverse(char *a)
11 {
12     int aa = strlen(a);
13     char t;
14     for(i=0; i<aa/2; i++)
15     {
16         t = a[i];
17         a[i] = a[aa-1-i];
18         a[aa-1-i] = t;
19     }
20 }
21 void add(char *a, char *b, char *c)
22 {
23         int cc = 0, aa = strlen(a), bb = strlen(b);
24         int len = max(aa, bb);
25            for(i=0; i<aa; i++)
26            {
27             a[i] = a[i]-‘0‘;
28         }
29            for(i=0; i<bb; i++)
30         {
31               b[i] = b[i]-‘0‘;
32         }
33         for(i=0; i<len; i++)
34         {
35             c[i] = (a[i]+b[i]+cc) % 10 + ‘0‘;
36             cc = (a[i]+b[i]+cc) / 10;
37         }
38            if(cc) c[i++] = cc + ‘0‘;
39           c[i] = ‘\0‘;
40 }
41 void print(char *c)
42 {
43     for(i = strlen(c)-1; i>=0; i--)
44         printf("%c", c[i]);
45         printf("\n");
46 }
47 main()
48 {
49     int n, j;
50     scanf("%d", &n);
51     for(j=1; j<=n; j++)
52     {
53         memset(a, 0, sizeof(a));
54         memset(b, 0, sizeof(b));
55         memset(c, 0, sizeof(c));
56         scanf("%s %s", a, b);
57         printf("Case %d:\n", j);
58         printf("%s + %s = ", a, b);
59         reverse(a), reverse(b);
60         add(a, b, c);
61         print(c);
62           if(j != n) printf("\n");
63     }
64
65 }
时间: 2024-08-09 16:40:18

大数的加法运算,杭电oj-1002的相关文章

杭电oj 1002

1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int nCases; 5 int m[1001], n[1001]; 6 char a[1001], b[1001]; 7 int main() 8 { 9 scanf("%d", &nCases); 10 for(int i = 1; i <= nCases; ++i) 11 { 12 memset(m,

杭电OJ 1002 大数相加

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 fol

杭电oj 1002 wrong answer(待改正)

/*#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ int n; int i,j,q;//计数 int al=0; int bl=0; int cl,dl; char turn; char a[1001]; char b[1001]; char c[1002]; int sum[1002];for(i=0;i<1001;i++){ sum[i]=a[i]=b[i]=c[i]='0';

大数A+B 【杭电-1002】 附题

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

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

异或^符号在C/C++中的使用 &amp; 杭电oj 2095

异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终结果. 特点:任何数和0异或都等于它本身;两个相同的数异或后的结果是0: 举例如下: int a = 4 =100(二进制) int b = 3 =011(二进制) int c = a^b = 111 = 7: 下面就^常用应用做个介绍: 1. 在一排数中找到独一无二的一个数 本例启发来自于杭电oj

杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=2 1.2.5 #include<stdio.h> /* 题意:找闰年. if((i%4==0 && i%100!=0) || i%400==0)count++; 3 2005 25 1855 12 2004 10000 2108 1904 43236 */ int main() { int t,y,n; int i,count=0; whil

『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧,用心在自己的研究上.晚上级会开完也就八点多了,开始打打题,今天在HDU杭电的ACM集训题看到一个奇葩的题,前来献上. 今日推荐: <全球风暴> 一部宇宙航空和地球气候片的良心佳作,后期特效建模都是特别杠杠的大片,不会让你失望的哟,我已经三刷了哈哈哈.这部片在爱奇艺有上线,有兴趣的朋友可以看看鸭.

杭电oj 1009 FatMouse&#39; Trade

Tips:本题采用贪心算法,类似于背包问题,关键在于读入数据之后,将数据按 J[i]/F[i] 从大到小排列即可. 1 /**本程序主要采用贪心算法思想,类似于背包问题*/ 2 #include<stdio.h> 3 #include<string.h> 4 int main() 5 { 6 int M,N; 7 while(scanf("%d %d",&M,&N)) 8 { 9 if(M == -1 && N == -1) 10

杭电OJ(HDU)-ACMSteps-Chapter Three-《FatMouse&amp;#39; Trade》《今年暑假不AC》《排名》《开门人和关门人》

http://acm.hdu.edu.cn/game/entry/problem/list.php?chapterid=1§ionid=3 1.3.1 FatMouse' Trade #include <algorithm> /* 题意:价值/代价的比值来排序,买比值大的. Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 */ #include<stdio.h>