andy6804tw
2/20/2017 - 10:27 AM

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=15564 差集就跟交集相反,程式碼也類似

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

差集就跟交集相反,程式碼也類似

import java.util.*;  
public class Main {  
    public static void main(String[] args) {  
        Scanner scn = new Scanner(System.in);  
        String arr1[]=scn.nextLine().split(","),arr2[]=scn.nextLine().split(",");
        ArrayList<Integer> list=new ArrayList<Integer>();
        int j=0;
        for(int i=0;i<arr1.length;i++){
        	for(j=0;j<arr2.length;j++){
        		if(arr1[i].equals(arr2[j])){
        			break;
        		}
        	}
        	if(j==arr2.length)
        		list.add(Integer.parseInt(arr1[i]));
        }
        if(list.size()>0){
        	Collections.sort(list);
        	for(int i=0;i<list.size();i++){
        		if(i!=0)
        			System.out.print(" ");
        		System.out.print(list.get(i));
        	}
        	System.out.println();
        }else{
        	System.out.println("null");
        }
    }
/*題目:[C_ST84-易] 差集
    作者:1010
    時間:西元 2017 年 2 月 */
}