之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历。学习算法有一段时间了,于是想从算法开始自己的博客生涯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的更多相关文章

  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. nyoj 103 A + B problem II

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

  5. hdu 1023 Train Problem II

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

  6. 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) ...

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

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

  8. hdoj 1002 A + B Problem II

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

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

随机推荐

  1. WebApi传参总动员(五)

    上回说到涉及多个实体的传参,用常规的方法已经不能解决了.这回我们用终极大招搞定她. WebApi:注意要引用JSON.Net [HttpPost] public string GetData(stri ...

  2. C#语法糖

    首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1.  ...

  3. Android开发总是难以入门

    发现自己很难入门,是真的太难,还是自己主观拒绝.

  4. Windows下 C++ 实现匿名管道的读写操作

    由于刚弄C++没多久,部分还不熟练,最近又由于开发需求要求实现与其他程序进行通信,瞬间就感觉想到了匿名通信.于是自己查阅了一下资料,实现了一个可读可写的匿名管道: 源代码大部分都有注释: Pipe.h ...

  5. Linux基础学习系列(一)

    Linux是一种类似于UNIX的操作系统,由Linus Torvalds于1991年在minix操作系统的基础创建.Linux凭借其优良特性已经成为目前发展潜力最大的操作系统. Linux的版本有内核 ...

  6. xshell下载文件到本地/上传文件到服务器

    xshell很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz首先你的Ubuntu需要安装rz.sz(如果没有安装请执行以下命令,安装完的请跳过.其 ...

  7. 在Windows下编写并运行第一个ASP.NET 5 Preview Web API程序

    2015年07月21日在微软中国MSDN的官方微博上得知Visual Studio 2015正式版完美发布. 抱着尝鲜的心态下载了Visual Studio社区版本. 在这个首发的版本里面,我们可以看 ...

  8. [iOS] 使用xib作为应用程序入口 with IDE

    [iOS] 使用xib作为应用程序入口 with IDE 在「使用xib做为应用程序入口 with Code」这篇文章中,介绍了如何透过写Code的方式,来使用xib做为应用程序的入口.但其实在Xco ...

  9. 安装sql server managerment studio报错"The instance id is required but it is missing"

    问题描述: 今天在安装sql server managerment studio的时候提示报错"The instance id is required but it is missing&q ...

  10. Swift开发第三篇——Playground

    本篇分为两部分: 一.Playground的延时运行 二.Playground的可视化 一.Playground的延时运行 Playground 就是提供一个可以即时编译的类似 REPL 的环境,他为 ...