GDUFE ACM-1002

题目:http://acm.gdufe.edu.cn/Problem/read/id/1002

A+B(Big Number Version)

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

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

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:

3
1 2
112233445566778899 998877665544332211
33333333333333333333333333 100000000000000000000

Sample Output:

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

Case 3:
33333333333333333333333333 + 100000000000000000000 = 33333433333333333333333333

思路:400位数啊,明显是unusigned long long int是不够大的,所以就要自己写一个。所以我就想,把最后一位数相加,然后把十位数加到前一位数上(如果没有十位数,那就是0),最后把整个数输出来,因为不知道具体有多少位数,所以两个数的和我是倒着储存的(能看懂我的意思吗==)

难度:感觉有一定的难度,想了很长时间,主要是写的时候觉得有难度,想出来不算很难吧。要把字符串转变成整数数组。

代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     int n;
 6     while(scanf("%d",&n)!=EOF)
 7     {
 8         char ai[400],bi[400];
 9         int i,j,d,e,k,q=0,a[400],b[400],c[400];
10         while(n--)
11         {
12             q++;
13            getchar();
14            scanf("%s",ai);
15            scanf("%s",bi);
16            d=strlen(ai);
17            e=strlen(bi);
18            for(i=0;i<d;i++)
19             a[i]=ai[i]-‘0‘;
20            for(j=0;j<e;j++)
21             b[j]=bi[j]-‘0‘;
22             k=0;
23             c[0]=a[d-1]+b[e-1];
24             if(d>1||e>1)
25            for(k=1,i=d-2,j=e-2;;i--,j--,k++)
26            {
27                if(i>=0&&j>=0)
28                 c[k]=c[k-1]/10+a[i]+b[j];
29               else if(i>=0&&j<0)
30                c[k]=c[k-1]/10+a[i];
31               else if(i<0&&j>=0)
32                 c[k]=c[k-1]/10+b[j];
33                else if(i<0&&j<0)
34                {if(c[k-1]>=10)
35                 c[k]=1;
36                 else k--;break;}
37            }
38            printf("Case %d:\n",q);
39            printf("%s + %s = ",ai,bi);
40            for(;k>=0;k--)
41            {c[k]=c[k]%10;
42             printf("%d",c[k]);
43             }
44             printf("\n");
45             if(n>0)
46                 printf("\n");
47         }
48     }
49     return 0;
50 }
时间: 2024-10-13 19:15:17

GDUFE ACM-1002的相关文章

[acm 1002] 浙大 Fire Net

已转战浙大 题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 浙大acm 1002 #include <iostream> #include <cstdlib> #include <cmath> #include <list> #define OPEN 1 #define CASTLE 2 #define BLOCK 3 #define WALL 4 // summary: 用贪心

acm 1002 算法设计

最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是问题描述: 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 fir

杭电acm 1002 大数模板(一)

从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的,但是经过自己的使用与调试也明白了其中的内涵. 首先定义大数的结构体: struct BigNum{ static const int BASE=100000000; static const int WIDTH=8; vector<int> s; BigNum(long long num=0){

C语言超大数据相加计算整理

在做ACM 1002题时,整理得到. #include<stdio.h>#include<string.h>#define MAX 1000void zero(char *s,int len){ int i; for(i=0;i<len;i++) s[i]-='0';}void back(char *s,int len){ int i; for(i=0;i<len;i++) s[i]+='0';}int main(){ char a[20][MAX],b[20][MAX

2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 681    Accepted Submission(s): 240 Problem Description This is a simple problem. The teacher gives Bob a list of prob

ACM学习历程—HDU5269 ZYB loves Xor I(位运算 &amp;&amp; dfs &amp;&amp; 排序)(BestCoder Round #44 1002题)

Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he wants to know the sum of all (lowbit(Ai xor Aj) (i,j∈[1,n]) We define that lowbit(x)=2^k,k is the smallest integer satisfied ((x and 2^k)>0)Specially,

ACM学习历程—BestCoder 2015百度之星资格赛1002 列变位法解密(vector容器)

Problem Description 列变位法是古典密码算法中变位加密的一种方法,具体过程如下 将明文字符分割成个数固定的分组(如5个一组,5即为密钥),按一组一行的次序整齐排列,最后不足一组不放置任何字符,完成后按列读取即成密文. 比如: 原文:123456789 密钥:4 变换后的矩阵: 1234 5678 9xxx (最后的几个x表示无任何字符,不是空格,不是制表符,就没有任何字符,下同) 密文:159263748 再比如: 原文:Hello, welcome to my dream w

2016 ACM/ICPC Asia Regional Qingdao Online 1002 Cure

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Given an integer n, we only want to know the sum of 1/k2 where k from 1 to n.   Input There are

华东交通大学2015年ACM“双基”程序设计竞赛1002

Problem B Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submission(s) : 698   Accepted Submission(s) : 342 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description 阿黄是一个小气的银行家,他有了一个银行,出版了4种

武汉科技大学ACM :1002: 零起点学算法38——求阶乘和

Problem Description 输入一个正整数n(n<=10),计算 S=1!+2!+3!+...+n! Input 输入一个正整数n(n<=10)(多组数据) Output 输出S(每组数据一行) Sample Input 2 Sample Output 3 #include<stdio.h> int main() { long int s,a; int i,n; while(scanf("%d",&n)!=EOF) { s=0; a=1; f