引入

前几天参加湖南多校的比赛,其中有这样一道题,需要使用高精度,同时需要排序,如果用c++实现的话,重载运算符很麻烦,于是直接学习了一发怎样用Java写大数,同时也算是学习Java基本常识了

题目

Description

Today the Intergalactic Council of Pebble Coins (ICPC) conducted an intergalactic auction of the Neutronium Chaos Pebble Coin (NCPC). This coin, which was forged in the Ancient Coin Machine (ACM), is rumored to be the key to ruling the universe.

Due to the extremely competitive nature of the auction, as well as the odd mechanics of the intergalactic currency used (far too advanced for mere mortals to understand), the auction was conducted with the following rules:

  1. only one participant was allowed to make a bid at a time,
  2. each participant was only allowed to make one bid, and
  3. a participant making a bid had to bid at least twice the amount of the highest bid at the time.

The first participant making a bid was allowed to make a bid of any positive amount.

After the auction there were a lot of sore losers – understandably, having just lost their chance at world domination. To make the losers feel a little better and prevent possible rioting, the ICPC has decided to hold a lottery for the participants. The winners of the lottery are determined as follows. The ICPC picks a random number \(s\). A group of participants is called winning if the sum of their bets from the auction is equal to \(s\). A participant wins the lottery and receives a prize – a shiny Pebble Coin – if they belong to any winning group of participants.

Given the names of the participants, the bets that they made, and the random number \(s\) chosen by the ICPC, help them determine which participants won the lottery.

Input

The first line of input contains two integers \(n\) and \(s\), where \(1 \le n \le 1\,000\) is the number of participants, and \(1 \le s < 10^{1\,000}\) is the random number chosen by the ICPC.

Then follow \(n\) lines describing the participants. Each line contains a string \(t\) and an integer \(b\), where \(t\) is the name of a participant, and \(1 \le b < 10^{1\,000}\) is the amount of his bet. The name of each participant is unique and consists of between \(1\) and \(20\) letters from the English alphabet.

Output

Output an integer \(k\) denoting the number of participants that won the lottery. Then output \(k\) lines containing the names of the participants that won the lottery, one per line, in any order.

Examples

Input

5 63
Vader 3
Voldemort 7
BorgQueen 20
Terminator 40
Megatron 101

Output

3
Terminator
BorgQueen
Vader

Input

4 1112
Blorg 10
Glorg 1000
Klorg 1
Zlorg 100

Output

0

题解

首先这个题说了一堆没用的废话,但有几点是必须要关注的,每一次竞标的价格一定是上次价格的两倍及以上,这样我们可以知道如果一个数在s之内,如果不选它,剩下的所有数加起来一定凑不够它,所以它必选,这样我们就可以排一遍序后直接按竞标价格从大到小枚举,小于当前要凑的数就选上,否则不选,问题就在于这个数很大,要用到高精度,于是就开始了学习java之旅

java输入输出

输入

要用到Scanner类,首先声明一个scanner变量,并用new运算符实例化Scanner,实例化Scanner时,需要传入System.in对象,Scanner通过传入的System.in获取用户输入,并对用户输入的字符进行处理,屏蔽了获取用户输入的复杂操作。

示例:

Scanner cin = new Scanner(System.in);

定义了一个名为cin的Scanner

输入不同类型数据的示例:

int n = cin.nextInt();//输入一个整数n
BigInteger sum = cin.nextBigInteger();//输入一个大整数sum
cin.next();//输入一个字符串,以空格为分隔
cin.nextLine();//输入一个字符串,以回车为分隔

输出

可以使用c中的printf,也可以使用新的println, print,区别是printf可以用来控制格式,println打印后换行,print不换行,均为System.out下的方法

示例:

System.out.println("不能加逗号,输出多个变量用加号连接" + "!");
System.out.print("。。。");
System.out.printf("%d", a);

Java定义变量

示例:

int n;//定义一个整数
BigInteger sum;//定义一个大整数
Vector<node> a = new Vector<node>();//定义一个Vector,类型为node,node为自己定义的类
Vector<String> ans = new Vector<String>();//定义一个类型为String的Vector
String[] name = new String[n + 1];//定义一个长度为n+1的字符串数组

Java实现结构体(Java中的类)

首先注意一点,静态类调用不了动态类的方法,如果在main类外面定义一个类,main中要调用这个类中的方法,要把这个类定义为static,就像下面这个

public static class node {
String name;
BigInteger bid;
node (String name, BigInteger bid) {
this.name = name;
this.bid = bid;
}
}

注意调用构造函数的时候要加new

Java中Vector的使用

添加一个元素:

for (int i = 1; i <= n; i++) {
a.add(new node(cin.next(), cin.nextBigInteger()));
}

对其排序,并自定义比较方法:

Collections.sort(a, new Comparator<node>() {
public int compare(node a, node b) {
return b.bid.compareTo(a.bid);
}
});

注意括号位置,这个排序是按照bid从大到小排序

遍历:

for (int i = 0; i < ans.size(); i++) {
System.out.println(ans.get(i));
}

使用get(i)获取在i处的值,如果是类型是类,可以再加.访问成员变量

Java大数操作

a = a.add(b);//加:a和b都是大数类型
a.subtract(b);//减
a.multiply(b);//乘
a.divide(b);//除:整除
a.compareTo(b);//比较大小,a小于b返回-1,等于返回0,大于返回1
a.mod(b);//求余
a.gcd(b);//求最大公约数
a.max(b);//求最大值
a.min(b);//求最小值
BigInteger.valuerOf();//将括号内数的转换为大整数

最后附上本题AC代码:

AC代码

import java.util.*;
import java.math.*; public class test {
public static class node {
String name;
BigInteger bid;
node (String name, BigInteger bid) {
this.name = name;
this.bid = bid;
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
BigInteger sum = cin.nextBigInteger(); Vector<node> a = new Vector<node>(); for (int i = 1; i <= n; i++) {
a.add(new node(cin.next(), cin.nextBigInteger()));
} Collections.sort(a, new Comparator<node>() {
public int compare(node a, node b) {
return b.bid.compareTo(a.bid);
}
}); Vector<String> ans = new Vector<String>(); for (int i = 0; i < n; i++) {
if (a.get(i).bid.compareTo(sum) <= 0) {
sum = sum.subtract(a.get(i).bid);
ans.add(a.get(i).name);
}
} if (sum.compareTo(BigInteger.valueOf(0)) != 0) ans.removeAllElements(); System.out.println(ans.size());
for (int i = 0; i < ans.size(); i++) {
System.out.println(ans.get(i));
}
} }

Java中大数的使用与Java入门(NCPC-Intergalactic Bidding)的更多相关文章

  1. 第87节:Java中的Bootstrap基础与SQL入门

    第87节:Java中的Bootstrap基础与SQL入门 前言复习 什么是JQ? : write less do more 写更少的代码,做更多的事 找出所有兄弟: $("div" ...

  2. java中大数类的学习

    java中提供了大数类BigInteger和BigDecimal分别表示大整数类和大浮点数类,这两个类都在java.math.*包中,因此每次必须在开头处引用该包. 一.BigInteger构造函数: ...

  3. .NET和JAVA中BYTE的区别以及JAVA中“DES/CBC/PKCS5PADDING” 加密解密在.NET中的实现

    场景:java 作为客户端调用已有的一个.net写的server的webservice,输入string,返回字节数组. 问题:返回的值不是自己想要的,跟.net客户端直接调用总是有差距 分析:平台不 ...

  4. Java中toArray的用法探究(java数组与list转换)

    转载原文地址: http://blog.csdn.net/guopengzhang/article/details/5497875 一.             Incident import jav ...

  5. Java 中的泛型详解-Java编程思想

    Java中的泛型参考了C++的模板,Java的界限是Java泛型的局限. 2.简单泛型 促成泛型出现最引人注目的一个原因就是为了创造容器类. 首先看一个只能持有单个对象的类,这个类可以明确指定其持有的 ...

  6. Java中String转换Double类型 Java小数点后留两位

    Java中String转换Double类型 double num1 = 0.0; String qq = "19.987"; num1 = Double.valueOf(qq.to ...

  7. JAVA中反射机制五(java.lang.reflect包)

    一.简介 java.lang.reflect包提供了用于获取类和对象的反射信息的类和接口.反射API允许对程序访问有关加载类的字段,方法和构造函数的信息进行编程访问.它允许在安全限制内使用反射的字段, ...

  8. JAVA中反射机制六(java.lang.reflect包)

    一.简介 java.lang.reflect包提供了用于获取类和对象的反射信息的类和接口.反射API允许对程序访问有关加载类的字段,方法和构造函数的信息进行编程访问.它允许在安全限制内使用反射的字段, ...

  9. java中类加载顺序(深入Java)

    未涉及虚拟机 0.<init>与<clinit>的区别 1.类的加载过程 2.类的使用方式 3.类的加载来源 4.重载之泛型参数不同可以吗 5.参考 引子 记得上次中秋一哥们写 ...

随机推荐

  1. 最短路问题:迪杰斯特拉算法(Dijsktra)

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  2. Knowledge Point 20180305 机器数转换与进制转换

    机器数(这里的机器数说的就是数值在计算机中的存储形式,相关可以了解数据在计算机中的表示)之间的转换往往是通过原码来实现的,下面我们结合进制来来一下: 进制也就是进位制,是人们规定的一种进位方法. 对于 ...

  3. Question 20171116 StringBuffer和StringBuilder的扩容机制

    StringBuffer和StringBuilder都是继承自AbstractStringBuilder,它们两个的区别在于buffer是线程安全的,builder是线程不安全的,前者安全效率低,后者 ...

  4. SQL0668N 不允许对表XX执行操作,原因码为 "3"

    DB2 Load导入数据失败之后,表被锁,提示,SQL0668N 不允许对表XX执行操作,原因码为 "3". 之前也遇到过,当时都是现查现用的,现在在博客记一下,以备后查. 解决方 ...

  5. 设置全局导航栏颜色,标题大小和UIBarButtonItem字体大小

    设置全局导航栏颜色,标题大小和UIBarButtonItem字体大小 在appdelegate里面设置 swift: UINavigationBar.appearance().barTintColor ...

  6. Mac系统配置php环境

    [写在前面——叨叨叨] -_-#急着配环境的同志们可以绕道.最近学校的实验室里接了一个小项目——考勤刷卡系统,利用RFID在硬件层获取学生卡的ID,通过wifi传输至服务器,进行考勤信息存储,手机端获 ...

  7. MySQL必会

    SQL语言对大小写不敏感,但一般使用大.1.创建数据库 CREATE DATABASE test; 2.授予权限 CRANT ALL ON test.* to user(s); 3.使用指定数据库 U ...

  8. Mysql 5.7 开启远程连接

    1 在控制台执行 mysql -uroot -p 系统提示输入数据库root用户的密码,输入完成后即进入mysql控制台 2 选择数据库 mysql -uroot -p use mysql; 开启远程 ...

  9. php 无限参数方法

    在很多项目开发中经常会用到共用方法但是参数不固定,每个参数都创建一遍阅读性不好,后期维护也麻烦,PHP有获取传入参数的方法,记录参考一下.这里有两个方法 <?php 方法一: #不指定参数个数方 ...

  10. linux的date常用命令

    1.显示现在时间 date 2.显示今天日期 date +"%F" date +"%Y-%m-%d" 3.现在时间转化为时间戳 date +%s 4.指定某日期 ...