Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:这题目真是麻烦

Posted by fishing_test at 2009-04-18 20:55:24 on Problem 1520
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator