Advertisement
Ligh7_of_H3av3n

03. Periodic Table

May 21st, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. package Uprajnenie;
  2.  
  3. import java.util.Scanner;
  4. import java.util.Set;
  5. import java.util.TreeSet;
  6.  
  7. public class PeriodicTable {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.         // Read the number of compounds
  13.         int n = scanner.nextInt();
  14.         scanner.nextLine(); // Consume the newline character
  15.  
  16.         Set<String> uniqueElements = new TreeSet<>();
  17.  
  18.         // Read each compound and add elements to the set
  19.         for (int i = 0; i < n; i++) {
  20.             String compound = scanner.nextLine();
  21.             String[] elements = compound.split("\\s+");
  22.             for (String element : elements) {
  23.                 uniqueElements.add(element);
  24.             }
  25.         }
  26.  
  27.         // Print the unique elements in ascending order
  28.         for (String element : uniqueElements) {
  29.             System.out.print(element + " ");
  30.         }
  31.  
  32.         scanner.close();
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement