ZOJ3477&JAVA大数类
转:http://blog.csdn.net/sunkun2013/article/details/11822927
import java.util.*;
import java.math.BigInteger;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Switch {
public static void main(String[] args) {
// TODO Auto-generated method stub BigInteger a = new BigInteger("997945672345647898769");
BigInteger b = new BigInteger("59164562345721340329"); System.out.println("两大数运算结果为:"); BigInteger c = a.add(b);
BigInteger d = a.subtract(b);
BigInteger e = a.multiply(b);
BigInteger f = a.divide(b); // 若除数为0,程序会自动抛出异常
BigInteger g = a.remainder(b); System.out.println(a + " + " + b + " = " + c);
System.out.println(a + " - " + b + " = " + d);
System.out.println(a + " * " + b + " = " + e);
System.out.println(a + " / " + b + " = " + f);
System.out.println(a + " % " + b + " = " + g);
}
}
Doraemon's Number Game
Time Limit: 2 Seconds Memory Limit: 65536 KB
Doraemon and Nobita are playing a number game. First, Doraemon will give Nobita N positive numbers. Then Nobita can deal with these numbers for two rounds. Every time Nobita can delete i (2 ≤ i ≤ K) numbers from the number set. Assume that the numbers deleted is a[ 1 ], a[ 2 ] ... a[ i ]. There should be a new number X= ( a[ 1 ] * a[ 2 ] * ... * a[ i ] + 1 ) to be inserted back into the number set. The operation will be applied to the number set over and over until there remains only one number in the set. The number is the result of round. Assume two results A and B are produced after two rounds. Nobita can get |A - B| scores.
Now Nobita wants to get the highest score. Please help him.
Input
Input will contain no more than 10 cases. The first line of each case contains two positive integers N and K (1 ≤ N ≤ 100, 1 ≤ K ≤ 50). The following line contains Npositive integers no larger than 50, indicating the numbers given by Doraemon.
Output
For each case, you should output highest score in one line.
Sample Input
6 3
1 3 4 10 7 15
Sample Output
5563
Hint
For most cases, N ≤ 20
题意:在一个正数集合中,可以删去任意i(2<=i<=k)个数,加上这i个数的乘积+1的数,最后只剩下一个数。因为有多种情况每种情况对应一个数,问:在这些只剩下一个数的数中选取两个数绝对值之差最大(|A-B|)的即是答案。
思路:猜想到:在数集中每次删去最小的两个数加上一个数,这样最后剩下的一个数是最大的;同样的,在数集中每次删去最多个(即K)数再加上一个数,这样最后剩下的一个数是最小的。
难点:由于这里可能达到50个数相乘,所以考虑到用JAVA大数类。
import java.util.*;
import java.math.BigInteger;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
//int i;
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
while(in.hasNext())
{
int n=in.nextInt();
int k=in.nextInt();
if(n==1)
{
int e=in.nextInt();
System.out.println("0");
continue;
}
BigInteger a,b,one,temp;
PriorityQueue<BigInteger> minq=new PriorityQueue<BigInteger>();
PriorityQueue<BigInteger> maxq=new PriorityQueue<BigInteger>(1000,Collections.reverseOrder());
for(int i=0;i<n;i++)
{
a=in.nextBigInteger();
minq.add(a);
maxq.add(a);
}
one=BigInteger.ONE;
while(minq.size()>1)
{
a=minq.peek();
minq.remove(a);
b=minq.peek();
minq.remove(b);
a=a.multiply(b);
a=a.add(BigInteger.ONE);
minq.add(a);
}
one=minq.peek(); while(maxq.size()>k)
{
temp=BigInteger.ONE;
for(int i=0;i<k;i++)
{
a=maxq.peek();
maxq.remove(a);
temp=temp.multiply(a);
}
temp=temp.add(BigInteger.ONE);
maxq.add(temp);
}
temp=BigInteger.ONE;
while(!maxq.isEmpty())
{
a=maxq.peek();
maxq.remove(a);
temp=temp.multiply(a);
}
temp=temp.add(BigInteger.ONE);
//System.out.println(temp);
//System.out.println(one);
//System.out.println(temp);
System.out.println(one.subtract(temp));
}
} }
ZOJ3477&JAVA大数类的更多相关文章
- JAVA大数类
JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...
- JAVA大数类练手
今天突然看到了OJ上的大数类题目,由于学习了一点大数类的知识.果断水了6道题......都是非常基础的.就当的练手的吧. 学到的只是一些大数类的基本操作.以后多做点这样的题,争取熟练运用水大数题... ...
- Java大数类介绍
java能处理大数的类有两个高精度大整数BigInteger 和高精度浮点数BigDecimal,这两个类位于java.math包内,要使用它们必须在类前面引用该包:import java.math. ...
- HDU高精度总结(java大数类)
HDU1002 A + B Problem II [题意]大数相加 [链接]http://acm.hdu.edu.cn/showproblem.php?pid=1002 Sample Inpu ...
- JAVA大数类—基础操作(加减乘除、取模、四舍五入、设置保留位数)
当基础数据类型长度无法满足需求时可以使用大数类 构造方法接受字符串为参数 BigInteger bInt = new BigInteger("123123"); BigDecima ...
- JAVA - 大数类详解
写在前面 对于ACMer来说,java语言最大的优势就是BigInteger,Bigdecimal,String三个类. 这三个类分别是高精度整数,高精度浮点数和字符串,之所以说这个是它的优势是因为j ...
- Java 大数类BigInteger和BigDecimal的基本函数
在Java中有两个类BigInteger和BigDecimal分别表示不可变的任意精度的整数和不可变的有符号的任意精度的十进制数(浮点数).主要用于高精度计算中.这两个类使得java中的大数,高精度运 ...
- Java大数类BigDecimal及八种舍入模式的介绍
BigDecimal的引入 在利用Java编程语言开发银行.金融类等需要对数值进行高精度计算的软件时,我们经常使用BigDecimal和BigInteger这两个大数据类,而不是常见的int.long ...
- Java 大数类
划分结果存在数组.供应商下标0 在剩下的标记1 import java.math.BigInteger; import java.util.Scanner; public class Main { p ...
随机推荐
- Tomcat部署项目通过—IP地址:端口访问
如题所示,实现效果图如下: 设置如下: (1)修改${tomcat}/config/sever.xml文件虚拟内容目录: <Engine name="Catalina" de ...
- Java虚拟机体系结构深入研究总结
工作以来,代码越写越多,程序也越来越臃肿,效率越来越低,对于我这样一个追求完美的程序员来说,这是绝对不被允许的,于是除了不断优化程序结构外,内存优化和性能调优就成了我惯用的“伎俩”. 要对Java程序 ...
- errno的基本用法
error是一个包含在 perror()和strerrot()函数可以把errno的值转化为有意义的字符输出. #include <stdio.h> #include <stdlib ...
- JAVA中的正则表达式--待续
1.关于“\”,在JAVA中的正则表达式中的不同: 在其他语言中"\\"表示为:我想要在正则表达式中插入一个普通的反斜杠: 在Java中“\\”表示为:我想要插入一个正则表达式反斜 ...
- DM6437 dsp系列之启动过程全析(2)—AIS文件解析
本文均属自己阅读源码的点滴总结,转账请注明出处谢谢. 欢迎和大家交流.qq:1037701636 email: gzzaigcn2009@163.com,gzzaigcn2012@gmail.com ...
- linux TIME_WAIT过多的解决方法
查看TCP状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'查看SOCKET状态:cat /proc/n ...
- WEB服务器4--IIS中网站、Web应用程序和虚拟目录
网站.Web应用程序和虚拟目录 在IIS中可以创建网站.Web 应用程序和虚拟目录,以便与计算机网络上的用户共享信息. “网站”.“Web 应用程序”和“虚拟目录”这三个概念的关系如图 8‑1所示. ...
- 如何排版 微信公众号「代码块」之 MarkEditor
前段时间写过一篇文章 如何排版微信公众号「代码块」,讲的是如何使用浏览器插件 Markdown Here 来排版代码块.虽然用 Markdown Here 排版出来的样式还不错,但存在一个问题,就是代 ...
- 【jquery ,ajax,php】加载更多实例
jquery $(function() { //初始化 getData(0); var index = 1; $("#more").click(function() { getDa ...
- jquery.validate的使用
在页面上面引用 <script type="text/JavaScript" src="js/jQuery.js"></script> ...