A + B Problem II

Problem 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
 
Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
 
 
分析:高精度大数的运算,直接套用刘汝佳老师的模板。注意坑点:每个case之间都要空一行,且最后一个case后不用空行。
 
 #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <istream>
#include <ostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
#define INF 100000
typedef long long ll;
const int maxn=;
struct BigInteger {
static const int BASE = ;
static const int WIDTH = ;
vector<int> s; BigInteger(long long num = ) { *this = num; }
BigInteger operator = (long long num) {
s.clear();
do {
s.push_back(num % BASE);
num /= BASE;
} while(num > );
return *this;
}
BigInteger operator = (const string& str) {
s.clear();
int x, len = (str.length() - ) / WIDTH + ;
for(int i = ; i < len; i++) {
int end = str.length() - i*WIDTH;
int start = max(, end - WIDTH);
sscanf(str.substr(start, end-start).c_str(), "%d", &x);
s.push_back(x);
}
return *this;
}
BigInteger operator + (const BigInteger& b) const {
BigInteger c;
c.s.clear();
for(int i = , g = ; ; i++) {
if(g == && i >= s.size() && i >= b.s.size()) break;
int x = g;
if(i < s.size()) x += s[i];
if(i < b.s.size()) x += b.s[i];
c.s.push_back(x % BASE);
g = x / BASE;
}
return c;
}
}; ostream& operator << (ostream &out, const BigInteger& x) {
out << x.s.back();
for(int i = x.s.size()-; i >= ; i--) {
char buf[];
sprintf(buf, "%08d", x.s[i]);
for(int j = ; j < strlen(buf); j++) out << buf[j];
}
return out;
} istream& operator >> (istream &in, BigInteger& x) {
string s;
if(!(in >> s)) return in;
x = s;
return in;
}
set<BigInteger> s; int main()
{
int t,times=,tmp;
BigInteger a,b;
scanf("%d",&t);
tmp=t;
while(t--){
cin>>a>>b;
BigInteger sum=a+b;
printf("Case %d:\n",++times);
cout<<a<<" + "<<b<<" = "<<sum<<endl;
if(times!=tmp) printf("\n");
}
return ;
}

HDU-1002(简单大数加法)的更多相关文章

  1. hdu 1002 Java 大数 加法

    http://acm.hdu.edu.cn/showproblem.php?pid=1002 PE   由于最后一个CASE不须要输出空行 import java.math.BigInteger; i ...

  2. hdu 5718(Oracle)大数加法

    曾经有一位国王,统治着一片未名之地.他膝下有三个女儿. 三个女儿中最年轻漂亮的当属Psyche.她的父亲不确定她未来的命运,于是他来到Delphi神庙求神谕. 神谕可以看作一个不含前导零的正整数n n ...

  3. hdu 1002 java 大数相加

    package Main; //import java.io.InputStream; import java.math.BigDecimal; import java.util.Scanner; p ...

  4. hdu 1002(大数)

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

  5. 大数加法~HDU 1002 A + B Problem II

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...

  6. 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...

  7. hdu 1002 A + B Problem II【大数加法】

    题目链接>>>>>> 题目大意:手动模拟大数加法,从而进行两个大数的加法运算 #include <stdio.h> #include <strin ...

  8. HDU 1041 Computer Transformation (简单大数)

    Computer Transformation http://acm.hdu.edu.cn/showproblem.php?pid=1041 Problem Description A sequenc ...

  9. HDU 1002 A - A + B Problem II (大数问题)

    原题代号:HDU 1002 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 原题描述: Problem Description I have a ...

随机推荐

  1. AHOI2009最小割

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1072  Solved: 446[Submit] ...

  2. RAC SCAN

    在介绍SCAN之前, 先简单的回顾一下ORACLE 关于IP 地址的命名用, 在9I RAC时, ORACLE没有自己的CLUSTERWARE, 主要依靠第三方的集群软件(如HACMP等), 客户端主 ...

  3. HDU 1695 GCD 莫比乌斯反演

    分析:简单的莫比乌斯反演 f[i]为k=i时的答案数 然后就很简单了 #include<iostream> #include<algorithm> #include<se ...

  4. HDOJ 2167 Pebbles (状态压缩dp)

    题意:给你一个n*n的矩阵,让你从矩阵中选择一些数是的他们的和最大,规则是:相邻的两个数不能同时取,位置为(i,j)的数与(i+1,j),(i-1,j),(i,j+1),(i,j-1),(i+1,j+ ...

  5. Datatable转换成List实体对象列表 几个实例

    一, /// <summary> /// 将Datatable转换为List集合 /// </summary> /// <typeparam name="T&q ...

  6. Curl之Post Json

    curl Post Json $ curl -i -X POST -H "'Content-type':'application/x-www-form-urlencoded', 'chars ...

  7. 【原】模式之-适配器Adapter模式

    适配器Adapter模式 适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 模式所涉及的角色有 ...

  8. 动态链接库DLL

    函数和数据被编译进一个二进制文件(通常扩展名为.LIB). 静态库: 在使用静态库的情况下,在编译链接可执行文件时,链接器从库中复制这些函数和数据并把它们和应用程序的其它模块组合起来创建最终的可执行文 ...

  9. HW3.7

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  10. VPS选购及辨别vps虚拟化技术

    现在国内外的VPS(Virtual Private Server)服务商非常多,每个服务商使用的VPS架构都不同.VPS属于虚拟化服务器,中文名:虚拟专用服务器. 常见的VPS虚拟化架构有多种:Ope ...