A + B Problem II
之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历。学习算法有一段时间了,于是想从算法开始自己的博客生涯O(∩_∩)O~~
今天在网上看了一道大数相加(高精度)的题目,题目很简单,但是体现了算法编程的细心之处。
题目:A + B Problem II
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
/*注意哦这里是两个test之间会有一个空行*/
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
/*在最后一个test输出之后是不需要空行的*/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; int main()
{
int n;
cin>>n;
int t = ;
while(n--)
{
t++;
char a[];
char b[];
char c[] = {''}; //c数组用来存放两数相加之和,需要初始化一下
cin>>a; //cin会忽略回车、空格、tab键
cin>>b;
int k,sum;
k = strlen(a)>strlen(b)?strlen(a):strlen(b); //k 取两个长整数中较长的一个
a[k+]='\0'; // 对长整数之后的一位赋值'\0' 结束标志
sum = ; //累加器
for(int i = strlen(a)-,j=strlen(b)-;j>=||i>=;i--,j--,k--)
{
if(i>=) sum+=a[i]-'';
if(j>=) sum+=b[j]-'';
c[k] = sum% + '';
sum /= ;
}
if(sum!=)
c[] = sum + '';
else
strcpy(c,&c[]);
printf("Case %d:\n",t);
printf("%s + %s = %s\n",a,b,c);
if(n!=) //最后一个test 不需要输出空行
printf("\n");
}
return ;
}
A + B Problem II的更多相关文章
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- nyoj 623 A*B Problem II(矩阵)
A*B Problem II 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 ACM的C++同学有好多作业要做,最头痛莫过于线性代数了,因为每次做到矩阵相乘的时候,大 ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- nyoj 103 A + B problem II
点击打开链接 A+B Problem II 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. G ...
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- 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) ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- hdoj 1002 A + B Problem II
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdoj 1002 A + B Problem II【大数加法】
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 狂屌的Windows下的定时任务工具xStarter
xStarter是一款将某些常规计算机操作自动化进行为目的的程序. 它不能为你生成word文件,但是它可以周期性地为你备份文件以保持完整性. 程序的特点有:加强的任务计划工具;在系统事件上执行任务;用 ...
- css中important的用处
今天看代码时遇到一段不理解的地方. #note_content { line-height: 22px; border: #DEDEDE 1px solid; background: #FAFAFA; ...
- 安装xampp无法设置默认时间的坑
xampp无法设置默认时间,修改了时间还是无效 [Date] ; Defines the default timezone used by the date functions ; http://ph ...
- 【iOS】Quartz2D矩阵操作
前面画基本图形时,画四边形是由几条直线拼接成的,现在有更简便的方法. 一.关于矩阵操作 1.画一个四边形 通过设置两个端点(长和宽)来完成一个四边形的绘制. 代码: - (void)drawRect: ...
- Oracle 查询并删除重复记录的SQL语句
查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select ...
- RCA端子颜色(红、白、黄)
RCA端子(红白黄)的作用: 黄:视频 红:左声道 白:右声道 RCA为两口插头,红色代表左声道,白色为右声道,3.5(AUX口)同样为立体声接头,虽然它只有一个端口,同样也具有左右声道分开传输的功能 ...
- HTML Jquery
在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...
- HTTP路由
HTTP路由 HTTP路由(译者注:Play的路径映射机制)组件负责将HTTP请求交给对应的action(一个控制器Controller的公共静态方法)处理. 对于MVC框架来说,一个HTTP请求可以 ...
- 一个Chrome拓展——HttpPost
周末花了点时间做了一个chrome拓展,叫HttpPost,顾名思义是用来测试http的post请求. 先直接看效果 插件与拓展 在说这个做的过程前,先说明什么是Chrome插件.Chrome拓展 1 ...
- Android 程序目录及UI的简介
Android程序的目录结构 src:源码的组织管理目录. gen:自动生成的目录,会生成一些重要的文件,如R.java,该目录一般不需要我们编写. assets:该目录文件不会被编译成二进制编码,多 ...