Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // на самом деле довольно очевидно, что ответы совпадают с последовательностью Фиббоначи, так что в рекурсии нет необходимости
- package com.company;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Main {
- private static Scanner scConsole;
- private static int bruteForce(short n, short CellIsNowOn) {
- int answer = 0;
- if (CellIsNowOn < n) {
- answer += bruteForce(n, (short) (CellIsNowOn + 1));
- if (CellIsNowOn < n - 1) {
- answer += bruteForce(n, (short) (CellIsNowOn + 2));
- }
- } else
- answer = 1;
- return answer;
- }
- private static short choose(String sQuestion, String[] Options) { // не больше 9-ти вариантов ответа
- short nChoice;
- String answer;
- StringBuilder optionsDigits = new StringBuilder();
- System.out.println(sQuestion + "\n\tВарианты: ");
- for (int i = 0; i < Options.length; i++) {
- System.out.println("\t" + (i + 1) + " - " + Options[i]);
- optionsDigits.append(i + 1);
- }
- answer = getAnythingFromConsole("", "^[" + optionsDigits + "]$", "Нужно ввести " +
- "цифру (одну из предложенных).");
- nChoice = (short) (Short.parseShort(answer) - 1);
- return nChoice;
- }
- private static String getAnythingFromConsole(String Question, String regEx, String clarification) {
- String sInput;
- String sOutput = "";
- if (!Question.equals("")) {
- System.out.println(Question);
- }
- while (sOutput.equals("")) {
- sInput = scConsole.nextLine();
- sInput = sInput.trim();
- sOutput = findRegEx(sInput, regEx, "")[0];
- if (sOutput.equals("")) {
- System.out.println("Данные введены неверно. " + clarification + " Повторите попытку:");
- }
- }
- return sOutput;
- }
- private static String[] findRegEx(String sInput, String regEx, String outputIfNothingFound) {
- ArrayList<String> arrStringOutput = new ArrayList<>();
- Pattern pattern = Pattern.compile(regEx);
- Matcher matcher = pattern.matcher(sInput);
- if (matcher.find()) {
- do {
- arrStringOutput.add(matcher.group());
- } while (matcher.find());
- } else
- arrStringOutput.add(outputIfNothingFound);
- return arrStringOutput.toArray(new String[0]);
- }
- public static void mainPartOfProgram() {
- short answerOnQuestion;
- int answer;
- do {
- int n = Integer.parseInt(getAnythingFromConsole("Введите число N:",
- "^[0]*([1-3]\\d|40|[3-9])$", "Надо ввести число от 3 до 40."));
- answer = bruteForce((short) n, (short) 1);
- System.out.println("Ответ: " + answer + "\n");
- answerOnQuestion = choose("Хотите ввести другое число N?", new String[]{"Да.",
- "Нет, закрыть программу."});
- } while (answerOnQuestion == 0);
- }
- public static void main(String[] args) {
- scConsole = new Scanner(System.in);
- System.out.println("Задание: Имеется полоска клетчатой бумаги шириной в одну клетку и длиной в n клеток.\n" +
- "На первой клетке установлена шашка. Одним ходом шашку можно передвигать на одну или две клетки.\n" +
- "Разработать рекурсивную функцию, определяющую количество способов продвижения шашки на n-ю клетку.");
- mainPartOfProgram();
- scConsole.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment