desislava_topuzakova

03. Periodic Table

May 27th, 2021
1,382
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int countCompounds = Integer.parseInt(scanner.nextLine());
  7.         TreeSet<String> elements = new TreeSet<>();
  8.  
  9.         for (int compound = 1; compound <= countCompounds; compound++) {
  10.             String chemicalCompound = scanner.nextLine(); //"Mo O Ce"
  11.             //всеки хим. елемент -> аdd
  12.             String [] chemicalElements = chemicalCompound.split("\\s+"); //["Mo", "O", "Ce"]
  13.  
  14.             //начин 1
  15.             //elements.addAll(Arrays.asList(chemicalElements));
  16.  
  17.             //начин 2
  18.             Collections.addAll(elements, chemicalElements);
  19.         }
  20.  
  21.         for (String element : elements) {
  22.             System.out.print(element + " ");
  23.         }
  24.         //elements.forEach(el ->  System.out.print(element + " "));
  25.  
  26.  
  27.     }
  28. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment