2013十大突破性科技
2013-12-17 18:41
351 查看
有如下一些数据:
1-2-3-4-5
2-3-2
2-4-4-2
1-6
保存在一个文本文档中(ANSI编码格式)。要求排序成如下结果:
1-6
1-2-3-4-5
2-4-4-2
2-3-2
即以每一行的最后一个元素进行排序,如果相同再以之前一个排序。都是降序。。
有兴趣的可以试试。
1-2-3-4-5
2-3-2
2-4-4-2
1-6
保存在一个文本文档中(ANSI编码格式)。要求排序成如下结果:
1-6
1-2-3-4-5
2-4-4-2
2-3-2
即以每一行的最后一个元素进行排序,如果相同再以之前一个排序。都是降序。。
有兴趣的可以试试。
package com.watersoft.file; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Sort { private int lineCount; private int numberCount; private String path; private int[][] matrix; /** * * * * @param args * contains the path of a text file which include the data being * sorted */ public static void main(String[] args) { if (args.length < 1) { System.err.println("Specific a file please!"); return; } System.out.println(">>> Information: sort start ..\n"); Sort s = new Sort(); s.setPath(args[0]); s.doSort(); s.display(); System.out.println("\n>>> Information: sort end .."); } /** * do sort: initialize, parse, then sort. */ public void doSort() { init(); parse(); for (int index = 0; index < this.lineCount - 1; index++) for (int innerIndex = 0; innerIndex < this.lineCount - 1; innerIndex++) sort(innerIndex, this.numberCount - 1); } /** * * sort the given data according to the given row index and column index * * * @param rowIndex * current row * * @param colIndex * current column */ private void sort(int rowIndex, int colIndex) { int aInt = this.matrix[rowIndex][colIndex]; int bInt = this.matrix[rowIndex + 1][colIndex]; int temp = 0; if (aInt < bInt) { /* * swap two rows */ for (int index = 0; index < this.numberCount; index++) { temp = this.matrix[rowIndex][index]; this.matrix[rowIndex][index] = this.matrix[rowIndex + 1][index]; this.matrix[rowIndex + 1][index] = temp; } } else if (aInt == bInt) { /* * compare previous elements of two rows */ colIndex--; sort(rowIndex, colIndex); } } /** * * read file to make sure the dimension of matrix * * * @return boolean value, true if successfully done, else false */ private boolean init() { try { FileReader fr; fr = new FileReader(this.path); BufferedReader br = new BufferedReader(fr); String tempString; int lc = 0; int nc = 0; while ((tempString = br.readLine()) != null) { lc++; nc = (nc < tempString.split("-").length) ? tempString .split("-").length : nc; } this.lineCount = lc; this.numberCount = nc; } catch (FileNotFoundException e) { System.err.println("Error: cannot find file:[" + this.path + "]"); return false; } catch (IOException e) { System.err.println("Error: cannot read file.."); return false; } return true; } private boolean parse() { /* * initialize matrix */ this.matrix = new int[this.lineCount][this.numberCount]; for (int index = 0; index < this.lineCount; index++) for (int innerIndex = 0; innerIndex < this.numberCount; innerIndex++) this.matrix[index][innerIndex] = Integer.MAX_VALUE; try { FileReader fr; fr = new FileReader(this.path); BufferedReader br = new BufferedReader(fr); String tempString; String[] tempArray; int lc = 0; int baseIndex = 0; while ((tempString = br.readLine()) != null) { tempArray = tempString.split("-"); baseIndex = this.numberCount - tempArray.length; for (int index = baseIndex; index < this.numberCount; index++) this.matrix[lc][index] = Integer.parseInt(tempArray[index - baseIndex]); lc++; } } catch (FileNotFoundException e) { System.err.println("Error: cannot find file:[" + this.path + "]"); return false; } catch (IOException e) { System.err.println("Error: cannot read file.."); return false; } return true; } /** * display sorted result */ public void display() { for (int index = 0; index < this.lineCount; index++) { for (int innerIndex = 0; innerIndex < this.numberCount; innerIndex++) { if (this.matrix[index][innerIndex] != Integer.MAX_VALUE) if (innerIndex + 1 != this.numberCount) System.out.print(this.matrix[index][innerIndex] + "-"); else System.out.print(this.matrix[index][innerIndex]); } System.out.println(); } } public void setPath(String path) { this.path = path; } }
相关文章推荐
- 2013十大突破性科技
- MIT技术评论评选2013十大突破性技术:深度学习居首
- 不管你从事哪个行业你都应该了解的:2017十大突破性科技
- MIT技术评论评选2013十大突破性技术:深度学习居首
- OWASP Top 10 – 2013, 最新十大安全隐患(ASP.NET解决方法)
- MIT评出全球十大突破性技术 阿里巴巴连摘两项
- 2013中国十大创新企业家
- 未来十大科技趋势
- 【互动出版网】2013双12全场科技类图书6.5折封顶
- 2013,互联网十大关键词
- 长知识了!2013最新十大约炮神器排行榜
- 盘点2013中国比特币十大事件
- 2008年美国十大科技新闻
- 十大“以身试法”的科技名流 盖茨上榜
- 你认识它们吗?2014十大科技流行词
- 2012大盘点之科技界十大超越
- 2012中国世界十大科技进展(转载)
- 2015十大最热门科技工作:软件开发工程师居首
- 2013机器学习十大问题
- 2017十大科技关键词 人工智能依然火热