Advertisement
Teammasik

laba7_OOP

Dec 12th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. try {
  7. File file = new File("input.txt");
  8. Scanner scanner = new Scanner(file);
  9. List<String> surnames = new ArrayList<>();
  10.  
  11. while (scanner.hasNextLine()) {
  12. String line = scanner.nextLine();
  13. String[] parts = line.split(" ");
  14. for (int i = 1; i < parts.length; i += 2) {
  15. String surname = parts[i];
  16. if (surname.length() > 0 && isVowel(surname.charAt(0))) {
  17. surnames.add(surname);
  18. }
  19. }
  20. }
  21. scanner.close();
  22.  
  23. if (surnames.isEmpty()) {
  24. System.out.println("Нет фамилий, начинающихся на гласную букву.");
  25. return;
  26. }
  27.  
  28. Collections.sort(surnames);
  29. String shortestSurname = surnames.get(0);
  30. for (String surname : surnames) {
  31. if (surname.length() < shortestSurname.length()) {
  32. shortestSurname = surname;
  33. }
  34. }
  35.  
  36. PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
  37. writer.println(shortestSurname);
  38. writer.close();
  39.  
  40. System.out.println("Самая короткая фамилия: " + shortestSurname);
  41. } catch (FileNotFoundException e) {
  42. System.out.println("Файл не найден.");
  43. } catch (IllegalArgumentException e) {
  44. System.out.println("Неверный формат данных");
  45. } catch (IOException e) {
  46. System.out.println("Ошибка записи в файл.");
  47. }
  48. }
  49.  
  50. private static boolean isVowel(char c) {
  51. c = Character.toLowerCase(c);
  52. return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y' ||
  53. c == 'у' || c == 'е' || c == 'ы' || c == 'а' || c == 'о' || c == 'э' ||
  54. c == 'я' || c == 'и' || c == 'ю';
  55. }
  56. }
  57.  
  58. -----------------------------------------------
  59. // po variantu
  60. import java.io.*;
  61. import java.util.*;
  62.  
  63. public class Var_work {
  64. public static void main(String[] args) {
  65. try {
  66. File file = new File("input2.txt");
  67. Scanner scanner = new Scanner(file);
  68. List<Integer> nums = new ArrayList<>();
  69. List<Integer> squares = new ArrayList<>();
  70.  
  71. while (scanner.hasNextLine()) {
  72. String line = scanner.nextLine();
  73. String[] parts = line.split(" ");
  74. for (int i = 1; i < parts.length; i += 1) {
  75. String num = parts[i];
  76. if (Integer.parseInt(num) % 2 == 1) {
  77. nums.add(Integer.parseInt(num));
  78. }
  79. }
  80. }
  81. scanner.close();
  82.  
  83. Integer max = Collections.max(nums);
  84. for (int i = 0; i*i <= max; i++) {
  85. squares.add(i * i);
  86. }
  87.  
  88. System.out.println("квадраты нечетных чисел:");
  89. for (int item : nums
  90. ) {
  91. if (squares.contains(item)) {
  92. System.out.println(item);
  93. }
  94. }
  95.  
  96. } catch (FileNotFoundException e) {
  97. System.out.println("Файл не найден.");
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement