HDU1084 – JAVA編程語言程序開發技術文章

[java] 
package D0709; 
 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Scanner; 
 
public class HDU1084 { 
 
    public static void main(String[] args) { 
        Scanner sc = new Scanner(System.in); 
 
        int n; 
        while (sc.hasNext()) { 
            n = sc.nextInt(); 
            if (n < 0) 
                break; 
            //集合保存所有學生的成績 
            ArrayList<Points> list = new ArrayList<Points>(); 
            int id=0;//為每一個學生添加一列id以方便得到本來輸入的時候的順序 
            for (int i = 0; i < n; i++) { 
                int pNum = sc.nextInt();//解決的題目數量 
                String str = sc.next(); 
                String[] ss = str.split("\\:"); 
                //所用時間 
                int time = Integer.parseInt(ss[0]) * 3600 + Integer.parseInt(ss[1]) * 60 + Integer.parseInt(ss[2]); 
                int score = 50 + pNum*10; 
                //添加一個學生到集合 
                list.add(new Points(++id,pNum,time,score)); 
            } 
            // 排序 
            Collections.sort(list); 
            //分數操作 
            for(int i = 0;i<list.size();){ 
                Points pts = list.get(i); 
                int count = 1; 
                for(int j = i+1;j<list.size();j++){ 
                    Points pts2 = list.get(j); 
                    if(pts.pNum == pts2.pNum){ 
                        count ++; 
                    }else break; 
                } 
                // 有多個學生做出的題數相同(大於0小於5) 
                if(count > 1&& list.get(i).pNum < 5 && list.get(i).pNum>0){ 
                    for(int k = 0;k<count/2;k++){ 
                        list.get(i+k).score+=5; 
                    } 
                } 
                //隻有一個學生做出這麼多(x)題(大於0小於5) 
                if (count == 1 && list.get(i).pNum < 5 && list.get(i).pNum>0) 
                    list.get(i).score += 5; 
                i += count;//繼續循環 
            } 
            int[]score = new int[n+1];// 用來按照輸入順序記錄學生的最後分數 
            for(int i = 0;i<list.size();i++){ 
                score[list.get(i).id] =list.get(i).score;  
            } 
            //輸出 
            for (int i = 1;i<=n;i++) { 
                System.out.println(score[i]); 
            } 
            System.out.println(); 
        } 
    } 

 
class Points implements Comparable<Points> { 
    public int id; 
    public int pNum; 
    public int time; 
    public int score; 
 
    public Points(int id,int pNum, int time, int score) { 
        this.id = id; 
        this.pNum = pNum; 
        this.time = time; 
        this.score = score; 
    } www.aiwalls.com
 
    @Override 
    public int compareTo(Points o) { 
        if (pNum > o.pNum) 
            return -1; 
        if (pNum == o.pNum && time < o.time) 
            return -1; 
        return 1; 
    } 
 

作者:lhfight

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *