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 ...
随机推荐
- dp优化
入口 A(fzu 1894) 普通的单调队列,trick是进队判断的符号选取(>=wa , >ac). B(poj 2823) 没什么好说的 ,坑爹poj g++,tle ;c++,ac. ...
- linux group
groups 查看当前登录用户的组内成员 groups gliethttp 查看gliethttp用户所在的组,以及组内成员 whoami 查看当前登录用户名 /etc/group文件包含所有组 ...
- eclipse配置maven + 创建maven项目
登录|注册 努力+坚持,而且还很年轻 目录(?)[+] 在现实的企业中,以低成本.高效率.高质量的完成项目,不仅仅需要技术大牛,企业更加需要管理大牛,管理者只懂技术是远远不够的.当 ...
- 利用IIS7 解决URL访问限制问题
网站可以通过URl直接访问一些不希望被访问的东西, 比如一些图片,js,css等等. 为了解决这个问题看了好多文章,不过毕竟我是新手菜鸟级别的,没有具体的解决方法,真心不知道怎么弄. 今天在看IIS的 ...
- Highcharts属性
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Android--------Java接口回调
>###回调说明 回调说明: 1.class A,class B 2.class A 实现接口callback 3.class B拥有一个参数为c ...
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...
- codeforces 519E A and B and Lecture Rooms(LCA,倍增)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud E. A and B and Lecture Rooms A and B are ...
- (原)Eclipse的java中文件读写
1 在<uses-sdk…/>下面添加permission <uses-sdk android:minSdkVersion="16" android:target ...
- JavaScript中的原型继承原理
在JavaScript当中,对象A如果要继承对象B的属性和方法,那么只要将对象B放到对象A的原型链上即可.而某个对象的原型链,就是由该对象开始,通过__proto__属性连接起来的一串对象.__pro ...