andy6804tw
10/17/2017 - 12:04 PM

ITSA第58次月賽 Problem 2. 道路修補

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=38905

這題有兩種做法第一種最直覺建立長度10000的陣列把需要修補的道路依序塞入數值,另外找出裡面數值最大的數最後算修補的道路就從0~amx就好囉 第二種方法就是利用java中的set容器囉,他會自動地把重複數字砍掉最後算set的總長度就是答案囉!

import java.util.*;  
  
public class Main {  
  
    public static void main(String[] args) {  
        Scanner scn =new Scanner(System.in);  
        int  n=scn.nextInt();  
        while(n--!=0) {  
            int t=scn.nextInt(),max=0,tot=0;  
            Set<Integer> set = new HashSet<Integer>();  
            for(int i=0;i<t;i++) {  
                int x=scn.nextInt(),y=scn.nextInt();  
                for(int j=x+1;j<=y;j++) {  
                    set.add(j);  
                }  
            }  
            System.out.println(set.size());  
        }  
  
    }  
    /*題目:ITSA第58次月賽 Problem 2. 道路修補
    作者:1010
    時間:西元 2017 年10 月 */
}