🌕🌗🌑🌑🌑

# 題目連結

  • 題目連結
  • Online Judge
  • uDebug

# 題目說明

Time limit: 3.000 seconds

# 題目

After graduating from the University of Notre Dame, you obtained a job at Top Shelf Software, Inc., as an entry-level computer engineer. On the first day, your manager sits down with you and tasks you with the following job: “We want to see how well you understand computer programming and the abstract science behind it. As an evaluation for all of our new hires, we require them to write a program to determine the relationship between pairs of sets. I’m quite sure that you’ll do well; my confidence is high. Here’s a list of requirements for what the program should do. Good luck.”

# Input

Your program should accept an even number of lines of text. Each pair of lines will represent two sets; the first line represents set A, the second line represents set B. Each line of text (set) will be a list of distinct integers.

# Output

After each pair of lines has been read in, the sets should be compared and one of the following responses should be output:

  • A is a proper subset of B
  • B is a proper subset of A
  • A equals B
  • A and B are disjoint
  • I'm confused!

# Sample Input

55 27
55 27
9 24 1995
9 24
1 2 3
1 2 3 4
1 2 3
4 5 6
1 2
2 3

# Sample Output

A equals B
B is a proper subset of A
A is a proper subset of B
A and B are disjoint
I'm confused!

# 解題技巧

利用 set 此資料結構,先將 setAsetB 利用另一個 set 將兩者存取在一起,進行判斷某個集合是否 equalsubsetdisjoinconfused

  • equal : setA.size() == set.size() && setB.size() == set.size()
  • subset : setA.size() == set.size() or setB.size() == set.size()
  • disjoin : setA.size() + setB.size() == set.size()
  • confused : 以上都不是

# Solution

Main.java
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String inputA = sc.nextLine();
            String inputB = sc.nextLine();
            Set<String> set = new HashSet<>();
            Set<String> setA = new HashSet<>();
            Set<String> setB = new HashSet<>();
            for(String temp : inputA.split(" ")){
                set.add(temp);
                setA.add(temp);
            }
            for(String temp : inputB.split(" ")){
                set.add(temp);
                setB.add(temp);
            }
            boolean isA = set.size() == setA.size();
            boolean isB = set.size() == setB.size();
            boolean isdisjoin = set.size() == setA.size() + setB.size();
            if(isA && isB){
                System.out.println("A equals B");
            }else if(isA){
                System.out.println("B is a proper subset of A");
            }else if(isB){
                System.out.println("A is a proper subset of B");
            }else if(isdisjoin){
                System.out.println("A and B are disjoint");
            }else{
                System.out.println("I'm confused!");
            }
        }
        sc.close();
    }
}
單字

** **
!! !!

片語 & 搭配詞

!! !!