题目描述:

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

Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110

代码如下:

 #include<iostream>
#include<string>
using namespace std;
string ans;
void bintadd(string a,string b)
{
int alen = a.length() - ,blen = b.length() - ;//alen,blen分别存放a,b字符串的最大下标
int up = ,i = ,j,te;//up存放进位值,te存放余数 while(alen >= || blen >= )
{
if(alen >= && blen >= )//两个字符串都没有计算完
te = a[alen--] + b[blen--] + up -'' - '';//倒着计算字符串里的数,记得要加上进位值up
else
if(alen >= && blen < )//如果b字符串已经计算完
te = a[alen--] + up -'';
else
if(alen < && blen >= )//如果a字符串已经计算完
te = b[blen--] + up - '';
if(te > )
{
up = te / ;//得到进位值
te = te % ;//得到余数
}
else
up = ;
ans.push_back(te + '');//将结果放进结果字符串
}
if(up)
ans.push_back(up + '');//如果出现两个字符串一样长,而还有进位值
} int main()
{
string a,b;
int t,ca = ,i;
cin >> t;
for(ca = ;ca <= t;ca++)
{
cin >> a >> b;
bintadd(a,b);
if(ca != )
cout << endl;
cout << "Case" << " " << ca << ":" << endl;
cout << a << " + " << b << " = ";
for(i = ans.length() - ;i >= ;i--)//反向输出结果字符串
cout << ans[i];
cout << endl;
ans.erase();
}
}

代码分析:

上面的代码是我根据网上代码改正后的,我自己写的代码虽然答案正确,但没有上面的代码那么简洁,所以就用了网上的代码来分析,不过网上的这代码有错误,但竟然AC了。。。我改正后,也AC了。。

大数加法,主要就在于将数字存储为字符串,然后根据小学学的满十进一的原则,得出答案,最后反向输出。。。

参考地址:http://www.haogongju.net/art/1227474

A + B Problem II 大数加法的更多相关文章

  1. hdu1002 A + B Problem II[大数加法]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1002 题干 代码和解释 由题意这是一个涉及到大数的加法问题.去看了一眼大数加法的方法感觉头很大,然后突然发现Java可以流氓解决大数问题,毅 ...

  2. A + B Problem II(大数加法)

    一直格式错误,不想改了,没A #include <iostream> #include <stdio.h> #include <string.h> #include ...

  3. hdu1002 A + B Problem II(大数题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/ ...

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

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

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

  6. HDU 1002 A + B Problem II(高精度加法(C++/Java))

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

  7. hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏

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

  8. HDU1002 A + B Problem II 大数问题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java ...

  9. HDU 1023 Train Problem II 大数打表Catalan数

    一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n ...

随机推荐

  1. 补丁(patch)的制作与应用

    命令简介 用到的两个命令是diff和patch. diff diff可以比较两个东西,并可同时记录下二者的区别.制作补丁时的一般用法和常见选项为: diff [选项] 源文件(夹) 目的文件(夹) - ...

  2. 合作开发,导入MyEclipse项目报错问题

    因工作原因,同事将他的java项目交接给了我.和平时的交接一样.他把他最新的源代码,打成压缩包,发给我.我解压后,使用myeclipse开发工具,通过导入,将项目导入到我的开发工具中,这个时候有一个问 ...

  3. JAVA訪问URL

    JAVA訪问URL: package Test; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...

  4. CodeIgniter框架开发的统计程序源代码开放

    文章来源: PHP开发学习门户 自己初学php时,用CodeIgniter框架开发的后台统计程序源代码 程序部分页面如图: 具体配置及下载源代码:http://bbs.phpthinking.com/ ...

  5. CodeForces 452C Magic Trick (排列组合)

    #include <iostream> #include <cstdio> #include<cmath> #include<algorithm> us ...

  6. mysql 数据备份

    一.备份数据库并下载到本地[db_backup.php] php代码: <?php // 数据库参数(此处测试,直接给定,项目中使用配置文件) $cfg_dbname = 'blog'; $cf ...

  7. Git跨平台中文乱码临时解决方案

    Git 是一个非常优秀的分布式版本控制系统,最初为Linux Kernel版本管理进行量身定做.优点是,和其他版本控制系统相比,稳定,速度快,跨平台,易学易用,无需要花费成本.更多优点请点击阅读:ht ...

  8. C++基础知识梳理--C++的6个默认函数

    C++有六个默认函数:分别是 1.default构造函数; 2.默认拷贝构造函数; 3.默认析构函数; 4.赋值运算符; 5.取值运算符; 6.取值运算符const; // 这两个类的效果相同 cla ...

  9. Studious Student Problem Analysis

    (http://leetcode.com/2011/01/studious-student-problem-analysis.html) You've been given a list of wor ...

  10. HDOJ 3415 Max Sum of Max-K-sub-sequence(单调队列)

    因为是circle sequence,可以在序列最后+序列前n项(或前k项);利用前缀和思想,预处理出前i个数的和为sum[i],则i~j的和就为sum[j]-sum[i-1],对于每个j,取最小的s ...