HDU1002   A + B Problem II

【题意】大数相加

【链接】http://acm.hdu.edu.cn/showproblem.php?pid=1002

Sample Input
2
1 2
112233445566778899 998877665544332211
 
Sample Output
Case 1:
1 + 2 = 3 Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

代码:

import java.io.*;
import java.util.*;
import java.math.BigInteger;//声明BigInteger大数类
import java.lang.*;
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int t,i=1;
t = cin.nextInt();
int tot = 0;
BigInteger a,b,c; //BigInteger类型
while (i<=t)
{
a=cin.nextBigInteger();
b=cin.nextBigInteger();
c=a.add(b);
System.out.println("Case "+i+":");
System.out.println(a+" + "+b+" = "+c);
if(i<t) System.out.println("");
i++;
}
}
}

HDU1042 N!

【题意】大数阶乘

【链接】

pid=1042" style="color:rgb(202,0,0); text-decoration:none">http://acm.hdu.edu.cn/showproblem.php?pid=1042

Sample Input
1
2
3
 
Sample Output
1
2
6

代码:

import java.io.*;
import java.util.*;
import java.math.BigInteger;//声明BigInteger大数类
import java.lang.*;
public class FF
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
int n=cin.nextInt();
BigInteger ans=BigInteger.ONE;
for(int i=1; i<=n; ++i)
{
ans=ans.multiply(BigInteger.valueOf(i));
}
System.out.println(ans);
System.gc(); //调用垃圾回收机制
}
}
}

HDU 1047 Integer Inquiry

【题意】多个大数相加

【链接】http://acm.hdu.edu.cn/showproblem.php?pid=1047

Sample Input
1 123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
 
Sample Output
370370367037037036703703703670

注意下格式

代码:

import java.io.*;
import java.util.*;
import java.math.BigInteger;//声明BigInteger大数类
import java.lang.*;
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int n=cin.nextInt();
while(n-->0)
{
BigInteger a,b,c;
b=BigInteger.ZERO;
while(cin.hasNextBigInteger())
{
c=BigInteger.ZERO;
c=cin.nextBigInteger();
if(!c.equals(BigInteger.valueOf(0)))
b=b.add(c);
else
{
System.out.println(b);
if(n!=0)
System.out.println("");
break;
}
}
System.gc();
}
}
}

HDU 1715  大菲波数

【题意】RT

【链接】http://acm.hdu.edu.cn/showproblem.php?pid=1715

Sample Input
5
1
2
3
4
5
 
Sample Output
1
1
2
3
5

代码:

import java.io.*;
import java.util.*;
import java.math.BigInteger;//声明BigInteger大数类
import java.lang.*;
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int n=cin.nextInt();
BigInteger fac[]= new BigInteger[1001];
fac[0]=BigInteger.ZERO;//初始赋值
fac[1]=BigInteger.ONE;
for(int i=2; i<=1000; ++i) fac[i]=fac[i-1].add(fac[i-2]);
while(n-->0)
{
int a;
a=cin.nextInt();
System.out.println(fac[a]);
}
//System.gc();
}
}

HDU 1063  Exponentiation

【题意】高精度幂

【链接】http://acm.hdu.edu.cn/showproblem.php?pid=1063

Sample Input
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12
 
Sample Output
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

最简形式是去掉后面的 0,以及小于 1 的小数的小数点前的 0

实现高精度幂java方法:

(1)调用pow函数

(2)for循环

代码:

import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;//声明BigInteger大数类
import java.lang.*;
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
BigDecimal ans=cin.nextBigDecimal();
int n=cin.nextInt();
String res=ans.pow(n).stripTrailingZeros().toPlainString();//整数去掉小数点和后面的0
       if(res.startsWith("0"))//去掉前导0
{
res=res.substring(1);
}
System.out.println(res);
/* BigDecimal a=BigDecimal.ONE;
int n=cin.nextInt();
for(int i=1; i<=n; ++i)
a=a.multiply(ans);
String res=a.stripTrailingZeros().toPlainString();
if(res.startsWith("0"))
{
res=res.substring(1);
}
System.out.println(res);
*/
}
}
}

HDU 1316   How Many Fibs?

【题意】区间fibonacci

【链接】http://acm.hdu.edu.cn/showproblem.php?pid=1316

Sample Input
10 100
1234567890 9876543210
0 0
 
Sample Output
5
4

代码:

import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;//声明BigInteger大数类
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
BigInteger a,b;
int ans,i;
BigInteger fac[]=new BigInteger[1005];
BigInteger zero=BigInteger.ZERO;
fac[1]=BigInteger.valueOf(1);
fac[2]=BigInteger.valueOf(2);
for(i=3; i<1005; ++i) fac[i]=fac[i-1].add(fac[i-2]);
while(cin.hasNextBigInteger())
{
a=cin.nextBigInteger();
b=cin.nextBigInteger();
if(a.compareTo(zero)==0&&b.compareTo(zero)==0) break;
for(ans=0,i=1; i<1005; ++i)
{
if(a.compareTo(fac[i])<=0&&b.compareTo(fac[i])>=0) ans++;
if(b.compareTo(fac[i])<0) break;
}
System.out.println(ans);
}
}
}

HDU 1753   大明A+B (高精度)

【题意】高精度小数相加

【链接】http://acm.hdu.edu.cn/showproblem.php?

pid=1753

Sample Input
1.1 2.9
1.1111111111 2.3444323343
1 1.1
 
Sample Output
4
3.4555434454
2.1

代码:

import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;//声明BigInteger大数类
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
BigDecimal a,b,c;
while(cin.hasNextBigDecimal())
{
a=cin.nextBigDecimal();
b=cin.nextBigDecimal();
c=a.add(b);
String res=c.stripTrailingZeros().toPlainString();
System.out.println(res);
}
}
}

HDU高精度总结(java大数类)的更多相关文章

  1. JAVA大数类

    JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...

  2. ZOJ3477&JAVA大数类

    转:http://blog.csdn.net/sunkun2013/article/details/11822927 import java.util.*; import java.math.BigI ...

  3. JAVA大数类练手

    今天突然看到了OJ上的大数类题目,由于学习了一点大数类的知识.果断水了6道题......都是非常基础的.就当的练手的吧. 学到的只是一些大数类的基本操作.以后多做点这样的题,争取熟练运用水大数题... ...

  4. Java大数类介绍

    java能处理大数的类有两个高精度大整数BigInteger 和高精度浮点数BigDecimal,这两个类位于java.math包内,要使用它们必须在类前面引用该包:import java.math. ...

  5. JAVA大数类—基础操作(加减乘除、取模、四舍五入、设置保留位数)

    当基础数据类型长度无法满足需求时可以使用大数类 构造方法接受字符串为参数 BigInteger bInt = new BigInteger("123123"); BigDecima ...

  6. hdu 1042 N! java大数及判断文件末尾

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submi ...

  7. HDU 1212 Big Number(C++ 大数取模)(java 大数类运用)

    Big Number 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 ——每天在线,欢迎留言谈论. 题目大意: 给你两个数 n1,n2.其中n1 ...

  8. 多校第六场 HDU 4927 JAVA大数类+模拟

    HDU 4927 −ai,直到序列长度为1.输出最后的数. 思路:这题实在是太晕了,比赛的时候搞了四个小时,从T到WA,唉--对算组合还是不太了解啊.如今对组合算比較什么了-- import java ...

  9. JAVA - 大数类详解

    写在前面 对于ACMer来说,java语言最大的优势就是BigInteger,Bigdecimal,String三个类. 这三个类分别是高精度整数,高精度浮点数和字符串,之所以说这个是它的优势是因为j ...

随机推荐

  1. 杂谈PID控制算法——最终篇:C语言实现51单片机中的PID算法

    真遗憾,第二篇章没能够发表到首页上去.趁热打铁.把最终篇——代码篇给发上来. 代码的设计思想请移步前两篇文章 //pid.h #ifndef __PID__ #define __PID__ /*PID ...

  2. 通俗解释遗传算法及其Matlab实现

    早上再看一个APP推荐的文章,发现的. (1)初识遗传算法 遗传算法,模拟达尔文进化论的自然选择和遗传学机理的生物进化过程的计算模型,一种选择不断选择优良个体的算法.谈到遗传,想想自然界动物遗传是怎么 ...

  3. [CSS]滚动条样式设置

    概述 最近项目中需要,将一个页面嵌入在一个webbrower中,这个webrower是定高的,在页面内容超过webbrower高度时,需要以滚动条的形式展现,当时也考虑了使用webbrower的滚动条 ...

  4. VR开发者必看:4大最为值得关注的内容平台【转】

            时间 2016-01-19 14:12:57 原文  http://www.sfw.cn/xinwen/478369.html 主题 虚拟现实 Oculus 对很多有意涉及VR行业的内 ...

  5. Kafka 简单实验一(安装Kafka)

    Apache Kafka - 安装步骤 步骤1 - Java安装 希望您现在已经在您的计算机上安装了Java,因此您只需使用以下命令进行验证. $ java -version 如果您的计算机上成功安装 ...

  6. tomcat环境部署

    环境说明 系统版本     CentOS 7.2 x86_64 软件版本     jdk-8u171 tomcat-8.0.27 1.tomcat介绍及软件包准备 Tomcat是Apache软件基金会 ...

  7. WCF报错

    1."没有终结点在侦听可以接受消息的 http://localhost:8084/Service1.svc.这通常是由于不正确的地址或者 SOAP 操作导致的.如果存在此情况,请参见 Inn ...

  8. LoadRunner中如何验证下载的文件大小、统计下载时间、度量下载速度

    LoadRunner中的web_get_in_property函数可用于返回上一个HTTP请求的相关信息,包括HTTP请求返回码.下载大小.下载时间等: The web_get_int_propert ...

  9. C语言字符串操作总结大全(超具体)

    1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat(p, p1, n) 附加指定长度 ...

  10. LeetCode题目:Spiral Matrix II

    原题地址:https://leetcode.com/problems/spiral-matrix-ii/ class Solution { public: vector<vector<in ...