Vanilla_Fury

laba_4_2_java_v3

Feb 13th, 2021
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. // на самом деле довольно очевидно, что ответы совпадают с последовательностью Фиббоначи, так что в рекурсии нет необходимости
  2.  
  3. package com.company;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class Main {
  11.  
  12.     private static Scanner scConsole;
  13.  
  14.     private static int bruteForce(short n, short CellIsNowOn) {
  15.         int answer = 0;
  16.         if (CellIsNowOn < n) {
  17.             answer += bruteForce(n, (short) (CellIsNowOn + 1));
  18.             if (CellIsNowOn < n - 1) {
  19.                 answer += bruteForce(n, (short) (CellIsNowOn + 2));
  20.             }
  21.         } else
  22.             answer = 1;
  23.         return answer;
  24.     }
  25.  
  26.     private static short choose(String sQuestion, String[] Options) { // не больше 9-ти вариантов ответа
  27.         short nChoice;
  28.         String answer;
  29.         StringBuilder optionsDigits = new StringBuilder();
  30.  
  31.         System.out.println(sQuestion + "\n\tВарианты: ");
  32.         for (int i = 0; i < Options.length; i++) {
  33.             System.out.println("\t" + (i + 1) + " - " + Options[i]);
  34.             optionsDigits.append(i + 1);
  35.         }
  36.  
  37.         answer = getAnythingFromConsole("", "^[" + optionsDigits + "]$", "Нужно ввести " +
  38.                 "цифру (одну из предложенных).");
  39.         nChoice = (short) (Short.parseShort(answer) - 1);
  40.  
  41.         return nChoice;
  42.     }
  43.  
  44.     private static String getAnythingFromConsole(String Question, String regEx, String clarification) {
  45.         String sInput;
  46.         String sOutput = "";
  47.  
  48.         if (!Question.equals("")) {
  49.             System.out.println(Question);
  50.         }
  51.  
  52.         while (sOutput.equals("")) {
  53.             sInput = scConsole.nextLine();
  54.             sInput = sInput.trim();
  55.             sOutput = findRegEx(sInput, regEx, "")[0];
  56.             if (sOutput.equals("")) {
  57.                 System.out.println("Данные введены неверно. " + clarification + " Повторите попытку:");
  58.             }
  59.         }
  60.  
  61.         return sOutput;
  62.     }
  63.  
  64.     private static String[] findRegEx(String sInput, String regEx, String outputIfNothingFound) {
  65.         ArrayList<String> arrStringOutput = new ArrayList<>();
  66.         Pattern pattern = Pattern.compile(regEx);
  67.         Matcher matcher = pattern.matcher(sInput);
  68.  
  69.         if (matcher.find()) {
  70.             do {
  71.                 arrStringOutput.add(matcher.group());
  72.             } while (matcher.find());
  73.         } else
  74.             arrStringOutput.add(outputIfNothingFound);
  75.  
  76.         return arrStringOutput.toArray(new String[0]);
  77.     }
  78.  
  79.     public static void mainPartOfProgram() {
  80.         short answerOnQuestion;
  81.         int answer;
  82.  
  83.         do {
  84.             int n = Integer.parseInt(getAnythingFromConsole("Введите число N:",
  85.                     "^[0]*([1-3]\\d|40|[3-9])$", "Надо ввести число от 3 до 40."));
  86.  
  87.             answer = bruteForce((short) n, (short) 1);
  88.             System.out.println("Ответ: " + answer + "\n");
  89.             answerOnQuestion = choose("Хотите ввести другое число N?", new String[]{"Да.",
  90.                     "Нет, закрыть программу."});
  91.         } while (answerOnQuestion == 0);
  92.     }
  93.  
  94.     public static void main(String[] args) {
  95.         scConsole = new Scanner(System.in);
  96.  
  97.         System.out.println("Задание: Имеется полоска клетчатой бумаги шириной в одну клетку и длиной в n клеток.\n" +
  98.                 "На первой клетке установлена шашка. Одним ходом шашку можно передвигать на одну или две клетки.\n" +
  99.                 "Разработать рекурсивную функцию, определяющую количество способов продвижения шашки на n-ю клетку.");
  100.  
  101.         mainPartOfProgram();
  102.  
  103.         scConsole.close();
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment