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 ...
随机推荐
- epoll的使用实例
在网络编程中通常需要处理很多个连接,可以用select和poll来处理多个连接.但是select都受进程能打开的最大文件描述符个数的限制.并且select和poll效率会随着监听fd的数目增多而下降. ...
- 关于spring mybateis 定义resultType="java.util.HashMap"
关于spring mybateis 定义resultType="java.util.HashMap" List<HashMap<String, Object>& ...
- touchstart和touchend事件
touchstart和touchend事件 移动互联网是未来的发展趋势,现在国内很多互联网大佬都在争取移动这一块大饼,如微信及支付宝是目前比较成功的例子,当然还有各种APP和web运用. 由于公司的需 ...
- su 和 sudo 命令的区别-转载
link 一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要 ...
- javascript01
手敲代码01 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- Java 并发编程:volatile的使用及其原理
Java并发编程系列: Java 并发编程:核心理论 Java并发编程:Synchronized及其实现原理 Java并发编程:Synchronized底层优化(轻量级锁.偏向锁) Java 并发编程 ...
- Git操作流程,基本命令演示
任务列表: 有一个中央库Center,和三个工作站A,B,C. 初始化时,代码存放在中央库中,A,B,C三个工作站开始工作之前都要首先从中央库克隆一份代码到本地. 第一个任务:A和B合作修复一个缺陷, ...
- C# DataGridVie利用model特性动态加载列
今天闲来无事看到ORm的特性映射sql语句.我就想到datagridview也可以用这个来动态添加列.这样就不用每次都去界面上点开界面填列了. 代码简漏希望有人看到了能指点一二. 先定义好Datagr ...
- TypeScript VS JavaScript 深度对比
TypeScript 和 JavaScript 是目前项目开发中较为流行的两种脚本语言,我们已经熟知 TypeScript 是 JavaScript 的一个超集,但是 TypeScript 与 Jav ...
- 前端安全之XSS攻击及防御
xss攻击是什么 ? XSS攻击类似于SQL注入攻击,攻击之前,我们先找到一个存在XSS漏洞的网站,XSS漏洞分为两种,一种是DOM Based XSS漏洞,另一种是Stored XSS漏洞.理论上, ...