zoukankan      html  css  js  c++  java
  • 计数算法-对200万考生的成绩就行排序

    import java.io.*;
    import java.text.DecimalFormat;
    
    
    /**
     * @author: small sunshine
     * @Description:
     * @date: 2021/6/20 10:04 上午
     */
    public class ScoreDescSort {
    
        private static long length = 200 * 10000;
    
        private static int[] score = new int[200 * 10000];
    
        private static int[] scoreCopy = new int[750 * 100 + 1];
    
        private static File file = new File("/Desktop/score.txt");
    
        private static File fileCopy = new File("/Desktop/score_copy.txt");
    
        /**
         * 对200万考生的分数(保留两位小数)倒序排列
         *
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
            long start = System.currentTimeMillis();
            createFile(length);
            createArray();
            flushArray();
            printResult();
            long end = System.currentTimeMillis();
            System.out.println("操作总用时:" + (end - start) + "ms");
        }
    
        /**
         * 1、创建分数文件
         *
         * @param length
         * @throws IOException
         */
        public static void createFile(long length) throws IOException {
    
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            for (int i = 0; i < length; i++) {
                if (i != 0) {
                    bufferedWriter.newLine();
                }
                bufferedWriter.write(doubleFormat(Math.random() * 750));
            }
            bufferedWriter.flush();
            outputStreamWriter.flush();
            bufferedWriter.close();
            outputStreamWriter.close();
        }
    
        /**
         * 从文件中读分数并写入到数组中
         *
         * @throws IOException
         */
        public static void createArray() throws IOException {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String scores = null;
            int index = 0;
            while ((scores = bufferedReader.readLine()) != null) {
                score[index++] = (int) (Double.valueOf(scores) * 100);
            }
            bufferedReader.close();
            inputStreamReader.close();
        }
    
        /**
         * 计数法处理分数
         */
        public static void flushArray() {
            for (int i:  score) {
                scoreCopy[i]++;
            }
        }
    
        /**
         * 排好序并打印出来
         *
         * @throws IOException
         */
        public static void printResult() throws IOException{
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(fileCopy), "UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            for (int j = scoreCopy.length - 1; j >= 0; j--) {
                if (scoreCopy[j] > 0) {
                    for (int z = 0; z < scoreCopy[j]; z++) {
                        bufferedWriter.write(doubleFormat((j / 100D)) + "");
                        bufferedWriter.newLine();
                    }
                }
            }
            bufferedWriter.flush();
            outputStreamWriter.flush();
            bufferedWriter.close();
            outputStreamWriter.close();
    
        }
    
    
    
        /**
         * 随机分数-格式化:#.##
         *
         * @param in
         * @return
         */
        public static String doubleFormat(double in) {
            DecimalFormat decimalFormat =  new DecimalFormat();
            decimalFormat.applyPattern("0.00");
            return decimalFormat.format(in);
        }
    }
    
    缘于生活,而归于工作。本人所书,而意于分享。 如有转载,请注明出处! --活出自己范儿
  • 相关阅读:
    ASP.NET的一些概念
    破解金盘gdlisxp系统
    对1,3,4,6,7,8组合显示成“1,3-4,6-8”方法
    C#关于Sort排序问题
    ISBN和标准编码关系以及概念
    sql批量修改插入数据
    C#winform导出数据到Excel的类
    C#winform初试报表
    JS解析XML文件和XML字符串
    java中String,StringBuffer,StringBuilder之间的区别
  • 原文地址:https://www.cnblogs.com/Small-sunshine/p/14926398.html
Copyright © 2011-2022 走看看