/* 1.将IP的每一段都前面补两个0 2.将每一端都变成3位 //割成四段的IP 3.将其从大到小排列 4.将前面多余的0去掉 */ import java.util.*; import java.io.*; class RuXue1 { public static void main(String[] args) throws Exception { //System.out.println("Hello World!"); IpSort(); } public static void IpSort()throws Exception { BufferedReader br=new BufferedReader(new FileReader("ruxue1.txt")); String line=null; StringBuffer sb=new StringBuffer(); while ((line=br.readLine())!=null) { sb.append(line+" "); } String str=sb.toString(); //System.out.println(str); //"61.54.231.245 61.54.231.9 61.54.231.246 61.54.231.48 61.53.231.249"; //1.将IP的每一段都前面补两个0 str=str.replaceAll("(\\d+)","00$1"); //2.将每一端都变成3位 str=str.replaceAll("0*(\\d{3})","$1"); //割成四段的IP String[] s=str.split(" "); //3.将其从大到小排列 TreeSet<String> tree=new TreeSet<String>(); for (String st:s) { tree.add(st); } for (String st:tree) { st=st.replaceAll("0*(\\d+)","$1"); //打印字符串 //4.将前面多余的0去掉 System.out.println(st); } br.close(); } } |