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 }

 

转载于:https://www.cnblogs.com/ruo786828164/p/5970860.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ACM-ICPC(国际大学生程序设计竞赛)是一项面向大学生的计算机编程竞赛,涉及算法和数据结构等领域。在比赛中,选手需要解决一系列编程问题,使用合适的算法和数据结构来实现正确和高效的解决方案。 对于整理ACM-ICPC模板,以下是一些建议: 1. 了解比赛要求:首先,你需要了解ACM-ICPC比赛的具体要求和规则。这包括了解比赛所涉及的算法和数据结构,以及题目的类型和难度等。 2. 收集资料:收集与ACM-ICPC相关的资料,包括经典算法和数据结构的实现代码、常见问题的解题思路等。可以参考教材、博客、论文等资源。 3. 整理模板:将收集到的资料整理成模板。可以按照算法和数据结构的分类进行整理,例如排序算法、图算法、字符串算法等。对每个模板,添加必要的注释和示例代码,以便理解和使用。 4. 测试代码:对每个模板编写测试代码,确保它们的正确性和可靠性。可以使用已知的测试用例或自行设计测试用例。 5. 更新与扩充:定期更新和扩充模板,以适应ACM-ICPC比赛中新出现的算法和数据结构。同时,根据自己的经验和理解,对模板进行优化和改进。 6. 练习和复习:在比赛之前,利用整理好的模板进行练习和复习。尝试解决一些经典问题,使用模板中的算法和数据结构进行实现,并进行优化。 希望这些建议对你整理ACM-ICPC模板有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值