Advertisement
ProgNeo

Untitled

Sep 18th, 2021 (edited)
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.63 KB | None | 0 0
  1. package com.company;
  2. import java.util.ArrayList;
  3. import java.util.Objects;
  4. import java.util.Scanner;
  5. import java.util.Arrays;
  6.  
  7. class Main
  8. {
  9.     private static Object[] InvertArray(Object[] array) {
  10.         for (int i = 0; i < array.length / 2; i++) {
  11.             Object temp = array[i];
  12.             array[i] = array[array.length - 1 - i];
  13.             array[array.length - 1 - i] = temp;
  14.         }
  15.         return array;
  16.     }
  17.  
  18.     public static void First() {
  19.         Scanner scanner = new Scanner(System.in);
  20.         boolean isDecrease;
  21.         boolean isSorted = true;
  22.         String[] string = new String[0];
  23.         String regex = "[+-]?([0-9]*[.])?[0-9]+";
  24.  
  25.         System.out.println("Задача 1");
  26.  
  27.         boolean isCorrect = false;
  28.         while (!isCorrect) {
  29.             isCorrect = true;
  30.  
  31.             System.out.print("Введите числа через пробел: ");
  32.             string = scanner.nextLine().split(" ");
  33.  
  34.             if (string.length < 2) {
  35.                 System.out.println("Введён один элемент: " + string[0]);
  36.                 return;
  37.             }
  38.  
  39.             for (String str : string) {
  40.                 if(!str.matches(regex)) {
  41.                     System.out.print("Строка содержит не цифры: ");
  42.                     isCorrect = false;
  43.                     break;
  44.                 }
  45.             }
  46.         }
  47.         Float[] array = new Float[string.length];
  48.  
  49.         for (int i = 0; i < string.length; i += 1) {
  50.             array[i] = Float.parseFloat(string[i]);
  51.         }
  52.  
  53.         int i = 1;
  54.         while (Objects.equals(array[i - 1], array[i])) {
  55.             if (i == string.length - 1) {
  56.                 System.out.println("Все числа равны");
  57.                 return;
  58.             }
  59.             i += 1;
  60.         }
  61.         isDecrease = array[i - 1] >= array[i];
  62.  
  63.         for (i = 1; i < array.length - 1; i += 1) {
  64.             boolean _isDecrease = array[i] >= array[i + 1];
  65.  
  66.             if (isDecrease != _isDecrease) {
  67.                 isSorted = false;
  68.                 break;
  69.             }
  70.         }
  71.         if (isSorted == false) {
  72.             System.out.println("Числа не отсортированы");
  73.         }
  74.         else {
  75.             System.out.printf("Числа отсортированы по %s \n", (isDecrease ? "убванию" : "возрастанию"));
  76.         }
  77.  
  78.         Arrays.sort(array);
  79.         if (isSorted){
  80.             if (isDecrease) {
  81.                 System.out.printf("Числа отсортированные по возрастанию: %s\n", Arrays.toString(array));
  82.             }
  83.             else {
  84.                 System.out.printf("Числа отсортированные по возрастанию: %s\n", Arrays.toString(InvertArray(array)));
  85.             }
  86.         }
  87.         else {
  88.             System.out.printf("Числа отсортированные по возрастанию: %s\n", Arrays.toString(array));
  89.             System.out.printf("Числа отсортированные по возрастанию: %s\n", Arrays.toString(InvertArray(array)));
  90.         }
  91.     }
  92.  
  93.     private static void Second() {
  94.         Scanner scanner = new Scanner(System.in);
  95.         int n;
  96.  
  97.         System.out.println("Задача 2");
  98.  
  99.         do {
  100.             System.out.print("Введите количество слов: ");
  101.             while (!scanner.hasNextInt()) {
  102.                 System.out.print("Неверный ввод. Введите число: ");
  103.                 scanner.next(); // this is important!
  104.             }
  105.             n = scanner.nextInt();
  106.         } while (n <= 0);
  107.  
  108.         String[] str = new String[n];
  109.  
  110.         for (int i = 0; i < n; i++) {
  111.             System.out.printf("str[%s]: ", i);
  112.             str[i] = new Scanner(System.in).next();
  113.         }
  114.  
  115.         for(int i=0; i<str.length-1; i++) {
  116.             for(int j=i+1;j<str.length;j++) {
  117.                 if(str[i].length()>=3 && str[j].length()>=3) {
  118.                     if(str[i].charAt(2)>str[j].charAt(2)) {
  119.                         String temp = str[i];
  120.                         str[i]=str[j];
  121.                         str[j]=temp;
  122.  
  123.                     }
  124.                 }
  125.                 else {
  126.                     var c1 = str[j].charAt(str[j].length() - 1);
  127.                     var c2 = str[i].charAt(str[i].length() - 1);
  128.                     if(str[i].length()<3 && str[j].length()<3) {
  129.                         if(c2 > c1) {
  130.                             String temp = str[i];
  131.                             str[i]=str[j];
  132.                             str[j]=temp;
  133.  
  134.                         }
  135.                     }
  136.                     else if(str[i].length()<3 && str[j].length()>=3) {
  137.                         if(c2 >str[j].charAt(2)) {
  138.                             String temp = str[i];
  139.                             str[i]=str[j];
  140.                             str[j]=temp;
  141.  
  142.                         }
  143.                     }
  144.                     else if(str[i].length()>=3 && str[j].length()<3) {
  145.                         if(str[i].charAt(2)> c1) {
  146.                             String temp = str[i];
  147.                             str[i]=str[j];
  148.                             str[j]=temp;
  149.  
  150.                         }
  151.                     }
  152.                 }
  153.             }
  154.         }
  155.         System.out.println("Отсортированный список слов по возрастанию:");
  156.         System.out.println(Arrays.toString(str));
  157.         System.out.println("Отсортированный список слов по убыванию:");
  158.         System.out.println(Arrays.toString(InvertArray(str)));
  159.     }
  160.  
  161.     public static void Third() {
  162.         System.out.println("Задача 3");
  163.         Scanner scanner = new Scanner(System.in);
  164.         System.out.print("Введите предложение: ");
  165.         String input = scanner.nextLine();
  166.  
  167.         input = input.replaceAll(",", " ");
  168.         input = input.replaceAll("\\.", " ");
  169.         input = input.replaceAll("  ", " ");
  170.  
  171.         String[] strings = input.split(" ");
  172.         ArrayList<String> palindromes = new ArrayList<>();
  173.  
  174.         for (String str : strings) {
  175.             StringBuffer rev = new StringBuffer(str).reverse();
  176.             String strRev = rev.toString();
  177.             if(str.equalsIgnoreCase(strRev)) {
  178.                 palindromes.add(str);
  179.             }
  180.         }
  181.  
  182.         System.out.printf("Список палиндромов: %s \n", Arrays.toString(palindromes.toArray(new String[0])));
  183.     }
  184.  
  185.     public static void main(String[] args) {
  186.         First();
  187.         Second();
  188.         Third();
  189.     }
  190. }
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement