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. codeigniter实现ajax分页

    <?php /** *417 add 主要是实现ajax分页 **/ class MY_Pagination extends CI_Pagination{ public function __c ...

  2. UIButton图片与文字位置调整

    1:左图右文 默认效果就行 2:左文右图 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn addTarget:se ...

  3. vpn分配多ip的配置

    1,创建vpn路由和远程访问 --配置并启用和路由远程访问--自定义配置--选择“vpn访问”,“nat和基本防火墙. 2,vpn配置ip路由选择(nat/基本防火墙)--新增接口(选择本地网卡)-- ...

  4. [Tommas] UNION 和 UNION ALL 的区别

    UNION指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看,UNION跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料.UNION的一个限制是两个 SQL 语句所产生的 ...

  5. bootstrap布局两列或者多列表单

    1, 代码如下: <div class="form-group"> <label for="starttime" class="co ...

  6. curl post请求

    libcurl发送post请求,包括httpheader参数 static size_t getCharCode(void *ptr, size_t size, size_t nmemb, void ...

  7. (转载)链表环中的入口点 编程之美 leecode 学习

    http://www.cnblogs.com/hiddenfox/p/3408931.html 说的很细 /** * Definition for singly-linked list. * clas ...

  8. NOIP2001 一元三次方程求解

    题一  一元三次方程求解(20分) 问题描述 有形如:ax3+bx2+cx+d=0  这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d  均为实数),并约定该方程存在三个不同实根(根的范 ...

  9. 微软 Build 2014开发者大会干货整理-1

    微软 Build 2014开发者大会第二天的主题演讲主要包含两部分:Microsoft Azure的发展状况,以及 .NET和生态系统的发展介绍.第二天的重点整理也由此分为上下两部分.您可以在Chan ...

  10. nyoj 678 最小K个数之和

    最小K个数之和 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 输入n个整数,输出其中最小的K个数之和.例如输入4,5,1,1,6,2,7,3,3这9个数字,当k=4 ...