之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历。学习算法有一段时间了,于是想从算法开始自己的博客生涯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. 【jQuery基础学习】06 jQuery表单验证插件-Validation

    jQuery的基础部分前面都讲完了,那么就看插件了. 关于jQuery表单验证插件-Validation validation特点: 内置验证规则:拥有必填.数字.E-Mail.URL和信用卡号码等1 ...

  2. Docker源码编译

    官方建议docker源码编译在docker容器内进行,因为官方提供的容器内已经继承了编译需要的环境,如果非要自己搭建编译环境也不是不可以,就是稍微有些繁琐.以下以1.8.2版本为例. 1.pull d ...

  3. 使用R的networkD3包画可交互的网络图

    d3network包code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs & ...

  4. ahjesus约束方法或属性的调用方

    class Program { static void Main(string[] args) { test(); run(); Console.ReadKey(); } static void ru ...

  5. c++之函数重载(函数匹配)

    Case void f(); void f(int); void f(int, int); void f(double, double = 3.14); 匹配原则: 1)其形参数量与本次调用提供的实参 ...

  6. 关于IE中通过http-equiv="X-UA-Compatible指定文件兼容性模式

    .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier ...

  7. AloneJs.albumBox() —— 相册对话框

    一.引用 <link href="https://cdn.suziyun.com/alonejs.min.css" rel="stylesheet" /& ...

  8. RHEL7用户管理

    本文介绍Linux的用户管理 用户管理 Linux 是一个可以实现多用户登陆的操作系统,不同用户可以同时登陆同一台主机,他们共享一些主机的资源,但他们也分别有自己的用户空间,用于存放各自的文件. 但实 ...

  9. SharePoint 禁用本地回环的两个方法

    有两种方法中,若要变通解决此问题,请根据您的具体情况使用下列方法之一. 方法 1: 指定主机名 (如果需要 NTLM 身份验证,请首选方法) 指定的主机名的映射到环回地址,并可以连接到 Web 站点在 ...

  10. SharePoint 2013 使用查阅项实现联动下拉框

    SharePoint列表使用中,经常会用到下拉框,而有些特殊的需求,会用到联动的下拉框,在SharePoint中默认没有这样的字段,所以如果实现,我们需要自己想办法. 这里,我们介绍如何使用JQuer ...