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 ...
随机推荐
- 发几个速度快可以用的google IP,谷歌IP(转)
google搜索引擎打不开时的解决办法,谷歌(google)的IP是多少? google IP镜像. 这里搜集了几个经过测试可用的IP,用来在不能域名访问google的时候进行访问,实时更新! 前面几 ...
- pyqt最小化学习
# -*- coding: cp936 -*- #!/usr/bin/env python # -*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui ...
- UIKit和Core Graphics绘图(三)——绘制虚线,椭圆以及饼图
绘制虚线 虚线绘制主要调用CGContextSetLineDash函数. 这个函数有4个参数,除了一个是上下文外,phase为初始跳过几个点开始绘制,第三个参数为一个CGFloat数组,指定你绘制的样 ...
- 【转载】ADO.NET与ROM的比较(1):ADO.NET实现CRUD
[转载]ADO.NET与ROM的比较(1):ADO.NET实现CRUD 转自周公 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hiberna ...
- asp.net缓存(三)
Asp.net应用程序数据缓存 System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类.此命名空间包括 Cache 类,该类是一个字典,您可以在其中存储任意数据对象,如哈希表和 ...
- hdu 2504
Problem Description 有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b.若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c. Input 第一 ...
- CAN总线
1.CAN(controller area network) LAN(local area network)局域网 2.CAN等通信协议的开发,使多种LAN通过网关进行数据交换得以实现.如应用在汽车电 ...
- 多台服务之间共享Session
一.问题:为了满足足够大的应用,满足更多的客户,就需要架设N台Web服务器(N>=2),在多台Web服务器的情况下,就会涉及到一个问题:用户登陆一台服务器以后,如果在跨越到另一台服务器的时候能够 ...
- Lenovo Y430P安装Linux无线网卡
新买了一台Lenovo Y430P的笔记本,笔记本自带的无线网卡型号是BCM43142.安装了CentOS6.5的操作系统后,按照网上搜索到的网址http://zh-cn.broadcom.com/s ...
- css3的滤镜模糊的效果
最近在做一个css3的滤镜模糊的效果,但是我发现,有些浏览器一点效果都没有,这是浏览器兼容性导致的,怕今后会忘记所以就先写下来,也希望可以帮到需要的小伙伴. 代码如下: div{//设置半透明滤镜效果 ...