Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:这题目真是麻烦In Reply To:这题目真是麻烦 Posted by:luoguangyao at 2009-04-01 21:29:50 用java 处理 很简单的;不用10分钟就能写完。 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class Poj1520 { private static boolean isNumber(String a) { try { Integer.parseInt(a); return true; } catch (Exception e) { return false; } } /** * @param args */ public static void main(String[] args) { Scanner cin = new Scanner(System.in); while(true) { String s = cin.nextLine(); if (s.equals(".")) { break; } String[] temp = s.split("[ ,.]+"); List<String> list = new ArrayList<String>(); List<Integer> list2 = new ArrayList<Integer>(); for (int i = 0;i < temp.length; i++) { if (isNumber(temp[i])) { list2.add(Integer.parseInt(temp[i])); } else { list.add(temp[i]); } } int listi = 0; int list2i = 0; Collections.sort(list, new Comparator() { public int compare(Object arg0, Object arg1) { String s1 = (String) arg0; String s2 = (String) arg1; return s1.compareToIgnoreCase(s2); } }); Collections.sort(list2); /* for (String sss: list) { System.out.println(sss); } */ // for (Integer ss: list2) { // System.out.println(ss); /// } StringBuffer ret = new StringBuffer(); for (int i = 0; i < temp.length; i++) { if (isNumber(temp[i])) { ret.append(list2.get(list2i)); list2i++; } else { ret.append(list.get(listi)); listi++; } if (i != temp.length -1) { ret.append(", "); } } ret.append("."); System.out.println(ret.toString()); } } } > 不难但是恶心人 Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator