A + B Problem II(大数加法)
http://acm.hdu.edu.cn/showproblem.php?pid=1002
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 261608 Accepted Submission(s): 50625
1 2
112233445566778899 998877665544332211
import java.math.BigInteger;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);//大数的输入,定义一个输入器
BigInteger a = null, b = null, c = null;//开始要赋值成空
a = BigInteger.valueOf();
b = BigInteger.valueOf();
int T;
T = cin.nextInt();//读入T;
// while(cin.hasNextBigInteger())//判断是否读到文件结尾相当于while(~scanf())
for(int cas = ; cas <= T; cas++)
{
a = cin.nextBigInteger();
b = cin.nextBigInteger(); // BigInteger zero = BigInteger.valueOf(0);//大数判断是不是等于0
// if(a.equals(BigInteger.valueOf(0))){System.out.println("haha");}
// if(a.equals(zero)) {System.out.println("hehe");}
c = a.add(b);
if(cas > ) System.out.println();//大数的换行输出
System.out.println("Case " + cas + ":");//大数的输出是用+号连接
System.out.println(a + " + " + b + " = "+c);
}
cin.close();//关闭读入器
} }
下面是大数string模拟的模板
//***********加法*********************
#include<algorithm>
string add(string s1,string s2)
{
string ans = "";
int i,j,x,y,k=;
for(i=s1.length()-,j=s2.length()-;i>= && j>= ;i--,j--)
{
x = s1[i] - '';
y = s2[j] - '';
ans += char((x+y+k)% + '');
k = (x+y+k)/;
}
while(i>=)
{
x=s1[i]-'';
ans += char ((x+k)% + '');
k = (x+k)/;
i--;
}
while(j>=)
{
y=s2[j]-'';
ans += char((y+k)% + '');
k = (y+k)/;
j--;
}
if(k>)
ans += '';
//ans.reverse();
reverse(ans.begin(),ans.end());
return ans;
}
//******************************* //************加法***************
string add(string s1,string s2)
{
string ans = "";
int i,j,x,y,k=;
for(i=s1.length()-,j=s2.length()-;i>= && j>= ;i--,j--)
{
x = s1[i] - '';
y = s2[j] - '';
ans = char((x+y+k)% + '') + ans;
k = (x+y+k)/;
}
while(i>=)
{
x=s1[i]-'';
ans = char ((x+k)% + '') + ans;//不如+=快,但是可以不用倒序
k = (x+k)/;
i--;
}
while(j>=)
{
y=s2[j]-'';
ans = char((y+k)% + '') + ans;
k = (y+k)/;
j--;
}
if(k>)
ans = '' + ans;
return ans;
}
//*********************加法**************************************
下面是完整的代码
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<algorithm>
using namespace std; string add(string s1,string s2)
{
string ans = "";
int i,j,x,y,k=;
for(i=s1.length()-,j=s2.length()-;i>= && j>= ;i--,j--)
{
x = s1[i] - '';
y = s2[j] - '';
ans += char((x+y+k)% + '');
k = (x+y+k)/;
}
while(i>=)
{
x=s1[i]-'';
ans += char ((x+k)% + '');
k = (x+k)/;
i--;
}
while(j>=)
{
y=s2[j]-'';
ans += char((y+k)% + '');
k = (y+k)/;
j--;
}
if(k>)
ans += '';
//ans.reverse();
reverse(ans.begin(),ans.end());
return ans;
}
int main()
{
string t , tt;
int T ,c = ;
cin>>T;
while(T--)
{
c++;
cin>>t>>tt;
string ans = add(t,tt);
if(c!=) cout<<endl;
cout<<"Case "<<c<<":"<<endl;
cout<<t<<" + "<<tt<<" = "<<ans<<endl;
}
return ;
}
A + B Problem II(大数加法)的更多相关文章
- A + B Problem II 大数加法
题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...
- hdu1002 A + B Problem II[大数加法]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu1002 题干 代码和解释 由题意这是一个涉及到大数的加法问题.去看了一眼大数加法的方法感觉头很大,然后突然发现Java可以流氓解决大数问题,毅 ...
- A + B Problem II(大数加法)
一直格式错误,不想改了,没A #include <iostream> #include <stdio.h> #include <string.h> #include ...
- hdu1002 A + B Problem II(大数题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/ ...
- 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) ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- 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) ...
- 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) ...
- HDU1002 A + B Problem II 大数问题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java ...
- HDU 1023 Train Problem II 大数打表Catalan数
一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n ...
随机推荐
- java二维码生成代码
QRCodeUtil.encode(text, "D:/004.jpg", "D:", true, "exp");// 这个方法的第一个参数 ...
- web调用客户端程序
背景 最近做一个集成需求,我们是B/S架构的,对方是C/S架构的,对方直接扔过来一个EXE连OCX都没有,让我们调用,也就是说,我们需要通过js程序去调用他们的客户端程序并传入多个参数,当时内心是崩溃 ...
- HHVM源码剖析
一.前言 hhvm源码中充满了很多C++11的新特性,并且使用了各种设计模式如工厂,模板方法等,利用智能指针包裹指针,让delete没有肆意的出现 模板,继承,explicit,纯虚函数的出现令代码中 ...
- LVM 详解
一.前言<http://blog.chinaunix.net/uid-186064-id-2823296.html> LVM是逻辑卷管理(Logic Volume Manage)的简称,它 ...
- Linux第八讲随笔 -tar / 系统启动流程
linux 第八讲1.tar 参考 作用:压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的. 语法:tar[必要参数][选择参数][文件] 参数:必要参数有如下: -A 新增压缩文件到 ...
- CPP--关于long的争议和思考
先普及一下VS开发Linux的知识点 VS2017的安装:https://www.cnblogs.com/dunitian/p/8051985.html 创建项目在这 第一次运行的时候会让输入服务器信 ...
- docker 保存 加载(导入 导出镜像
tensorflow 的docker镜像很大,pull一次由于墙经常失败.其实docker 可以将镜像导出再导入. 保存加载(tensorflow)镜像 1) 查看镜像 docker images 如 ...
- Java UDP实现聊天功能代码
我以前经常写的是基于TCP的网络编程,由于TCP建立连接鼻血要经过三次握手连接,服务器端需要阻塞式等待客户端的连接.而UDP则是可以直接向目的地址的目的端口上发送数据包,由于它只负责发送出去就好,不管 ...
- JavaScript ES6中export及export default的区别
相信很多人都使用过export.export default.import,然而它们到底有什么区别呢? 在JavaScript ES6中,export与export default均可用于导出常量.函 ...
- input 光标在 chrome下不兼容 解决方案
input 光标在 chrome下不兼容 解决方案 height: 52px; line-height: normal; line-height:52px\9 .list li input[type= ...