之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历。学习算法有一段时间了,于是想从算法开始自己的博客生涯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. [翻译]:SQL死锁-阻塞

    一般情况下死锁不是一步到位的,它必须满足特定的条件,然后形成资源的循环依赖才会产生死锁,死锁之前一定会出现阻塞,由阻塞升级才有可能出现死锁,所以我们有必要了解系统中都有哪些已经被阻塞的锁. 我在解决共 ...

  2. web安全——系统(Linux)

    简介 最小(少)原则,是安全的重要原则.最小的权限,最小的用户,最少的服务,最少的进程,是最安全的. 系统安全包括:文件系统保护.用户管理安全.进程的保护以及日志的管理. 场景 确保服务最少,每个都是 ...

  3. 我为什么要做富文本编辑器【wangEditor5个月总结】

    请访问wangEditor官网:www.wangEditor.com ----------------------------------------------------------------- ...

  4. swift学习笔记之-继承

    //继承 import UIKit /* 继承(Inheritance): 1.一个类可以继承(inherit)另一个类的方法(methods).属性(properties)和其它特性.当一个类继承其 ...

  5. elasticseach multi-field的实际用途

    下面是multi-field的介绍: multi_field 多域类型允许你对同一个值以映射的方式定义成多个基本类型 core_types . 这个非常有用,比如,如果你定义一个 string 类型的 ...

  6. 浅谈ES6中的Proxy

    Proxy是一个很有趣的对象,它能够修改某些操作的默认行为,等同于在语言层面做出修改,属于一种‘元编程’,即对编程语言进行编程. Proxy其实很好理解,就是在目标对象之前架设一层拦截,外界的访问都得 ...

  7. ABAP 搜索帮助

    当选择屏幕上的一个字段所参考的数据元素没有建立搜索帮助时,可以手工建立一个: 1.在se11创建一个搜索帮助ZAUTEST,需要输入: (1)描述: (2)选择方法:即搜索帮助显示字段所在的透明表: ...

  8. ArcGIS中定义图框样式

    ArcGIS系统中的样式可能不能满足实际生产需要,为了实现快速制图,可自定义一些样式,以便重复利用. 安装字符 因为样式中定义了自定义的符号,这些符号都打包到字体中,所以在使用样式之前,必须安装字体文 ...

  9. 在Android Studio中用Gradle添加Robolectric

    我们用Robolectric测试的话需要在gradle中进行配置,国内的详细教程太过简易,而且很多是低版本下的配置方案.所以经过本人的仔细摸索,找到了现在高版本中的配置方案,主要还是参考了官网的配置教 ...

  10. UIButton 内部介绍

    **     设置内容距btn边框距离     **/    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);        /** 将lab ...