原题描述:

A - A + B Problem II

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

InputThe 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. 
OutputFor 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
第二次看到这个题目了,第一次是在杭电acm上,那时侯想了用数组,不过没做出来。其实这个题目不算难,我直接定义了两个个字符型数组用来存储算数,定义一个整形数组存储值。先判断那个数组长度大.
两个字符型数组从后面相加,存储到整形数组上,如果值大于10再向前进1(不可能大于20)一直到短的那个数组为0的时候停止相加。直接把长的赋值到上面就可以了,记得进位!
用长的字符型数组长度判断整形数组的长度。然后就可以输出了。代码复杂,但是浅显易懂。
AC代码:
 1 #include <iostream>
 2 #include <string.h>
 3 const int N=1000;
 4 using namespace std;
 5 int main()
 6 {
 7     int t,l,m,n,sum[N+1]={0};
 8     int g=1;
 9     char a[N],b[N];
10     cin>>t;
11     while(t--)
12     {
13         int i;
14         cin>>a>>b;
15         m=strlen(a);
16         n=strlen(b);
17         l=m>=n?m:n;
18         if(m<=N&&n<=N)
19         {
20         if(m>=n)
21         {
22             for(i=0;n>0;i++)
23             {
24                 sum[i]=sum[i]+(a[m-1]-'0')+(b[n-1]-'0');
25                 m--;
26                 n--;
27                 if(sum[i]>9)
28                 {
29                     sum[i]-=10;
30                     sum[i+1]++;
31                 }
32             }
33             for( ;m>0;i++,m--)
34             sum[i]=sum[i]+(a[m-1]-'0');
35         }
36         else
37          {
38             for(i=0;m>0;i++)
39             {
40                 sum[i]=sum[i]+(a[m-1]-'0')+(b[n-1]-'0');
41                 m--;
42                 n--;
43                 if(sum[i]>9)
44                 {
45                     sum[i]-=10;
46                     sum[i+1]++;
47                 }
48             }
49             for( ;n>0;i++,n--)
50             sum[i]=sum[i]+(b[n-1]-'0');
51         }
52         }
53         if(sum[i]==0)
54         i--;
55         cout<<"Case "<<g<<":"<<endl;
56         g++;
57         cout<<a<<" + "<<b<<" = ";
58          for( ;i>=0;i--)
59         cout<<sum[i];
60         for(i=0;i<=l;i++)
61         sum[i]=0;
62         if(t!=0)
63         cout<<endl<<endl;
64         else cout<<endl;
65     }
66     return 0;
67 }

Week 1 # A A + B Problem II的更多相关文章

  1. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  2. nyoj 623 A*B Problem II(矩阵)

    A*B Problem II 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 ACM的C++同学有好多作业要做,最头痛莫过于线性代数了,因为每次做到矩阵相乘的时候,大 ...

  3. HDU 1002 A + B Problem II

    A + B Problem II   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted ...

  4. A + B Problem II

    之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历.学习算法有一段时间了,于是想从算法开始自己的博客生涯O(∩_∩)O~~ 今天在网上看了一道大数相加(高精度) ...

  5. nyoj 103 A + B problem II

    点击打开链接 A+B Problem II 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. G ...

  6. hdu 1023 Train Problem II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...

  7. HDU1002 -A + B Problem II(大数a+b)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  9. hdoj 1002 A + B Problem II

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. hdoj 1002 A + B Problem II【大数加法】

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. javaSE_07Java中类和对象-封装特性--练习

    1.编写封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩,一个学生类,我们关注姓名,年龄,学历等信息,要求年龄必须在19-40岁之间,默认为19,学历必须是大专,本科,研究生这几 ...

  2. 每篇半小时1天入门MongoDB——2.MongoDB环境变量配置和Shell操作

    上一篇:每篇半小时1天入门MongoDB——1.MongoDB介绍和安装 配置环境变量 Win10系统为例 右键单击“此电脑”——属性——高级系统设置——高级——环境变量,添加C:\Program F ...

  3. 加载jquery插件注意了

    1.尽量放在</body>之前,不要放在</head>标签之前,如果执意要放也要放在css之后,例如: <link href="style.css" ...

  4. JavaScript函数之递归

    递归 递归的本质就是使用函数自身来解决问题的思路. 递归的定义(摘): 程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用. 一个过程或函数在其定义或说明 ...

  5. IIS虚拟目录与UNC路径权限初探

    最近在一个项目中涉及到了虚拟目录与UNC路径的问题,总结出来分享给大家. 问题描述 某客户定制化项目(官网),有一个图片上传的功能.客户的Web机器有10台,通过F5负载均衡分摊请求. 假设这10台机 ...

  6. ecshop开发帮助

    http://www.ecshop.com/template_tutorial/ ECSHOP模板结构说明 http://help.ecshop.com/index.php ECSHOP帮助中心 ht ...

  7. 探索Windows命令行系列(2):命令行工具入门

    1.理论基础 1.1.命令行的前世今生 1.2.命令执行规则 1.3.使用命令历史 2.使用入门 2.1.启动和关闭命令行 2.2.执行简单的命令 2.3.命令行执行程序使用技巧 3.总结 1.理论基 ...

  8. js关闭当前窗口,window.close()方法只能是window.open打开的才能执行关闭

    js关闭当前窗口,window.close()方法只能是window.open打开的才能执行关闭. function closeWin() { //open(location, '_self').cl ...

  9. Entity Framework入门教程:SQLite数据源访问

    [环境安装] 可以通过NuGet直接搜索安装SQLite需要用到的组件 或者直接使用程序包管理器控制台 > Install-Package System.Data.SQLite 通过ADO.NE ...

  10. 引入CSS文件的方式,以及link与@import的区别

    一.引入css的方式 在HTML中引入css的方法主要有4种:行内式.内嵌式.链接式和导入式. 1.行内式 <div style="background:yellow;"&g ...