/
SofiMut
/
nickName
Обзор
Документация
Войти
/
SofiMut
/
nickName
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/Main.java
94 строки
3 KB
Софья Степанова
update src/main/java/Main.java
08 дек 2025, 06:33
08 дек 2025, 06:33
e38f21e
Код
Авторство
О чём код?
import java.util.Random; import java.util.concurrent.atomic.AtomicInteger; public class Main { public static AtomicInteger counter3 = new AtomicInteger(); public static AtomicInteger counter4 = new AtomicInteger(); public static AtomicInteger counter5 = new AtomicInteger(); public static void main(String[] args) throws InterruptedException { Random random = new Random(); String[] texts = new String[100_000]; for (int i = 0; i < texts.length; i++) { texts[i] = generateText("abc", 3 + random.nextInt(3)); //System.out.println(texts[i]); } Thread thread1 = new Thread(() -> { for (String text : texts) { if (isPalindrome(text) && !isSameChar(text)) { //System.out.println("Palindrom " + text); incrementCounter(text.length()); } } }); thread1.start(); Thread thread2 = new Thread(() -> { for (String text : texts) { if (isSameChar(text)) { // System.out.println("Îäèíàêîâûå " + text); incrementCounter(text.length()); } } }); thread2.start(); Thread thread3 = new Thread(() -> { for (String text : texts) { if (!isPalindrome(text) && isAscendingOrder(text)) { // System.out.println("Ïî âîçðàñòàíèþ:" + text); incrementCounter(text.length()); } } }); thread3.start(); thread1.join(); thread2.join(); thread3.join(); System.out.println("Сгенерировано красивых слов с длиной 3: " + counter3); System.out.println("Сгенерировано красивых слов с длиной 4: " + counter4); System.out.println("Сгенерировано красивых слов с длиной 5: " + counter5); } public static String generateText(String letters, int length) { Random random = new Random(); StringBuilder text = new StringBuilder(); for (int i = 0; i < length; i++) { text.append(letters.charAt(random.nextInt(letters.length()))); } return text.toString(); } public static boolean isPalindrome(String text) { return text.equals(new StringBuilder(text).reverse().toString()); } public static boolean isSameChar(String text) { for (int i = 1; i < text.length(); i++) { if (text.charAt(i) != text.charAt(i - 1)) return false; } return true; } public static boolean isAscendingOrder(String text) { for (int i = 1; i < text.length(); i++) { if (text.charAt(i) < text.charAt(i - 1)) return false; } return true; } public static void incrementCounter(int textLength) { if (textLength == 3) { counter3.getAndIncrement(); } else if (textLength == 4) { counter4.getAndIncrement(); } else { counter5.getAndIncrement(); } } }