问题:从data.bat文件中排序,找到数量最大的10个数字,以println的方式分别输出。
要求:
0.时间尽量短
1.类名为Test.java
2.无包名
3.无第三方引用
4.可直接javac编译和java运行
5.java命令行运行class,参数为data.bat文件路径,例如,java Test ./data.bat
6.jdk1.8
7.文件字符集,utf-8
8.不能有异常和警告
data.bat 地址:https://github.com/TabMM/drafts/tree/master/data.bat
废话不多说 直接上代码 (未经允许,不得转载)
方法一(本人写):
public class Test {
public static void main(String[] args) {
long start = System.currentTimeMillis();
readFileByLines();
System.err.print("使用时间" + (System.currentTimeMillis() - start) + "ms");
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines() {
int[] s = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
File file = new File("F:\IdeaProjects\snowflake\src\main\java\com\example\snowflake\data.dat");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
int temp = Integer.parseInt(tempString);
int t = temp > s[0] ? jh(0, temp, s) : (temp > s[1] ? jh(1, temp, s) : (temp > s[2] ? jh(2, temp, s) : (temp > s[3] ? jh(3, temp, s) : (temp > s[4] ? jh(4, temp, s) : (temp > s[5] ? jh(5, temp, s) : (temp > s[6] ? jh(6, temp, s) : (temp > s[7] ? jh(7, temp, s) : (temp > s[8] ? jh(8, temp, s) : (temp > s[9] ? jh(9, temp, s) : 0)))))))));
line++;
}
System.out.println(s[0] + " " + s[1] + " " + s[2] + " " + s[3] + " " + s[4] + " " + s[5] + " " + s[6] + " " + s[7] + " " + s[8] + " " + s[9]);
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
private static int jh(int index, int temp, int[] s) {
int a = index;
for (int i = 0; i < 9 - index; i++) {
stamp(i, s);
}
s[a] = temp;
return 0;
}
private static void stamp(int index, int[] s) {
s[9 - index] = s[8 - index];
}
}
方法二(@小和写):
public class Demo {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
TreeMap<Integer,Integer> treeMap = new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
BufferedReader br=new BufferedReader(new FileReader("F:\IdeaProjects\snowflake\src\main\java\com\example\snowflake\data.dat"));//读取文件
//输出
for(String s=br.readLine();s!=null;s=br.readLine()) {
treeMap.put(Integer.parseInt(s), Integer.parseInt(s));
if(treeMap.size() > 10){
treeMap.remove(treeMap.lastKey());
}
}
Iterator<Map.Entry<Integer,Integer>> it = treeMap.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Integer,Integer> entry = it.next();
System.out.println(entry.getKey() + " : " + entry.getValue());
}
System.err.print( "使用时间"+(System.currentTimeMillis() - start) +"ms");
}
}
方法三(@咿呀咿呀哟写):
public class Test {
public static int N = 10; // Top10
public static int arr[] = new int[N];
//数组长度
public static int len = arr.length;
//堆中元素的有效元素 heapSize<=len
public static int heapSize = len;
public static void main(String[] args){
long start = System.currentTimeMillis();
int _150M = 1024*1024*150;
File fin = new File("F:\IdeaProjects\snowflake\src\main\java\com\example\snowflake\data.dat");
FileChannel fcin = null;
try {
fcin = new RandomAccessFile(fin, "r").getChannel();
} catch (FileNotFoundException e) {
System.out.println("系统找不到指定文件");
return;
}
ByteBuffer rBuffer = ByteBuffer.allocate(_150M);
readFileByLine(_150M, fcin, rBuffer);
System.err.print( "使用时间"+(System.currentTimeMillis() - start) +"ms");
}
public static void readFileByLine(int bufSize, FileChannel fcin,
ByteBuffer rBuffer) {
String enterStr = " ";
try {
byte[] bs = new byte[bufSize];
String tempString = null;
while (fcin.read(rBuffer) != -1) {
int rSize = rBuffer.position();
rBuffer.rewind();
rBuffer.get(bs);
rBuffer.clear();
tempString = new String(bs, 0, rSize);
int fromIndex = 0;
int endIndex = 0;
int i = 0 ;
while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) {
String line = tempString.substring(fromIndex, endIndex);
fromIndex = endIndex + 1;
if (i<10){
arr[i]= Integer.valueOf(line);
i++;
continue;
}
//构建小顶堆
buildMinHeap();
if(Integer.valueOf(line) > arr[0]){
arr[0] = Integer.valueOf(line);
minHeap(0);
}
}
print();
}
} catch (IOException e) {
System.out.println("读取文件失败");
return;
}
}
/**
* 自底向上构建小堆
*/
public static void buildMinHeap(){
int size = len / 2;
for(int i = size;i>=0;i--){
minHeap(i);
}
}
/**
* i节点为根及子树是一个小堆
* @param i
*/
public static void minHeap(int i){
int l = left(i);
int r = right(i);
int index = i;
if(l<heapSize && arr[l]<arr[index]){
index = l;
}
if(r<heapSize && arr[r]<arr[index]){
index = r;
}
if(index != i){
int t = arr[index];
arr[index] = arr[i];
arr[i] = t;
//递归向下构建堆
minHeap(index);
}
}
/**
* 返回i节点的左孩子
* @param i
* @return
*/
public static int left(int i){
return 2*i;
}
/**
* 返回i节点的右孩子
* @param i
* @return
*/
public static int right(int i){
return 2*i+1;
}
/**
* 打印
*/
public static void print(){
for (int i = arr.length -1; i >=0 ; i--) {
System.out.println(arr[i]);
}
}
}