Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- try {
- File file = new File("input.txt");
- Scanner scanner = new Scanner(file);
- List<String> surnames = new ArrayList<>();
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- String[] parts = line.split(" ");
- for (int i = 1; i < parts.length; i += 2) {
- String surname = parts[i];
- if (surname.length() > 0 && isVowel(surname.charAt(0))) {
- surnames.add(surname);
- }
- }
- }
- scanner.close();
- if (surnames.isEmpty()) {
- System.out.println("Нет фамилий, начинающихся на гласную букву.");
- return;
- }
- Collections.sort(surnames);
- String shortestSurname = surnames.get(0);
- for (String surname : surnames) {
- if (surname.length() < shortestSurname.length()) {
- shortestSurname = surname;
- }
- }
- PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
- writer.println(shortestSurname);
- writer.close();
- System.out.println("Самая короткая фамилия: " + shortestSurname);
- } catch (FileNotFoundException e) {
- System.out.println("Файл не найден.");
- } catch (IllegalArgumentException e) {
- System.out.println("Неверный формат данных");
- } catch (IOException e) {
- System.out.println("Ошибка записи в файл.");
- }
- }
- private static boolean isVowel(char c) {
- c = Character.toLowerCase(c);
- return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y' ||
- c == 'у' || c == 'е' || c == 'ы' || c == 'а' || c == 'о' || c == 'э' ||
- c == 'я' || c == 'и' || c == 'ю';
- }
- }
- -----------------------------------------------
- // po variantu
- import java.io.*;
- import java.util.*;
- public class Var_work {
- public static void main(String[] args) {
- try {
- File file = new File("input2.txt");
- Scanner scanner = new Scanner(file);
- List<Integer> nums = new ArrayList<>();
- List<Integer> squares = new ArrayList<>();
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- String[] parts = line.split(" ");
- for (int i = 1; i < parts.length; i += 1) {
- String num = parts[i];
- if (Integer.parseInt(num) % 2 == 1) {
- nums.add(Integer.parseInt(num));
- }
- }
- }
- scanner.close();
- Integer max = Collections.max(nums);
- for (int i = 0; i*i <= max; i++) {
- squares.add(i * i);
- }
- System.out.println("квадраты нечетных чисел:");
- for (int item : nums
- ) {
- if (squares.contains(item)) {
- System.out.println(item);
- }
- }
- } catch (FileNotFoundException e) {
- System.out.println("Файл не найден.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement