Advertisement
Egor_Vakar

lab4_2 ebala recursia

Feb 21st, 2022
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.99 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4. import java.util.Iterator;
  5.  
  6. public class lab4_2 {
  7.     static Scanner scan = new Scanner(System.in);
  8.  
  9.     static int counter = 0;
  10.  
  11.     public static void main(String[] args) {
  12.         System.out.print("Welcome to the program\nSelect the source for entering numbers:\n1:Console\n2:File\nEnter 1 or 2: ");
  13.         int inputSource = takeSource();
  14.         String path = "";
  15.         if (inputSource == 2){
  16.             path = takeInPath();
  17.         }
  18.         int n = takeNum(inputSource, path, 1);
  19.         int[] arr = fillArr(n);
  20.         int sum = takeNum(inputSource,path, 2);
  21.         System.out.print("Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ");
  22.         int outSource = takeSource();
  23.         int[][] answer = new int[100][n];
  24.         answer = findAllSubsets(arr, n, sum, null, answer);
  25.         output(answer, outSource);
  26.     }
  27.  
  28.     static void output (final int[][] answer, int source){
  29.         String outPath;
  30.         if (source == 1) {
  31.             consoleOutput(answer);
  32.         } else {
  33.             outPath = takeOutPath();
  34.             FileOutput(outPath, answer);
  35.         }
  36.     }
  37.  
  38.     static void consoleOutput(final int[][] answer){
  39.         int i = 0;
  40.         int j = 0;
  41.         System.out.println("Answer:");
  42.         while (answer[i][j] != 0){
  43.             while (answer[i][j] != 0){
  44.                 System.out.print("[" + answer[i][j]+"] ");
  45.                 j++;
  46.             }
  47.             i++;
  48.             j = 0;
  49.             System.out.println();
  50.         }
  51.     }
  52.  
  53.     static void FileOutput(final String path, final int[][] answer) {
  54.         try (FileWriter fw = new FileWriter(path)) {
  55.             int i = 0;
  56.             int j = 0;
  57.             fw.write("Answer:\n");
  58.             while (answer[i][j] != 0){
  59.                 while (answer[i][j] != 0){
  60.                     fw.write("[" + answer[i][j]+"] ");
  61.                     j++;
  62.                 }
  63.                 i++;
  64.                 j = 0;
  65.                 fw.write("\n");
  66.             }
  67.             System.out.println("Done");
  68.             fw.close();
  69.         } catch (Exception e) {
  70.             System.out.println("File error!");
  71.         }
  72.     }
  73.  
  74.     static int takeSource() {
  75.         final byte CONSOLE = 1;
  76.         final byte FILE = 2;
  77.         boolean isIncorrect;
  78.         byte choice = 0;
  79.         do {
  80.             isIncorrect = false;
  81.             try {
  82.                 choice = Byte.parseByte(scan.nextLine());
  83.             } catch (Exception e) {
  84.                 System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  85.                 isIncorrect = true;
  86.             }
  87.             if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
  88.                 System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  89.                 isIncorrect = true;
  90.             }
  91.         } while (isIncorrect);
  92.         return choice;
  93.     }
  94.  
  95.     static String takeInPath() {
  96.         String path;
  97.         boolean isIncorrect;
  98.         System.out.print("Enter file path: ");
  99.         do {
  100.             isIncorrect = false;
  101.             path = scan.nextLine();
  102.             File file = new File(path);
  103.             if (!file.exists()) {
  104.                 System.out.print("File is not found\nEnter file path: ");
  105.                 isIncorrect = true;
  106.             }
  107.             if (!isIncorrect && (!path.endsWith(".txt"))) {
  108.                 System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
  109.                 isIncorrect = true;
  110.             }
  111.         } while (isIncorrect);
  112.         return path;
  113.     }
  114.  
  115.     static String takeOutPath() {
  116.         String path;
  117.         boolean isIncorrect;
  118.         System.out.print("Enter file path: ");
  119.         do {
  120.             isIncorrect = false;
  121.             path = scan.nextLine();
  122.             if (!path.endsWith(".txt")) {
  123.                 System.out.print("It should be a \".txt\" file\nEnter file path: ");
  124.                 isIncorrect = true;
  125.             }
  126.         } while (isIncorrect);
  127.         return path;
  128.     }
  129.  
  130.     private static int[] fillArr(final int n) {
  131.         int[] arr = new int[n];
  132.         for (int i = 0; i < n; i++) {
  133.             arr[i] = i + 1;
  134.         }
  135.         return arr;
  136.     }
  137.  
  138.     private static int takeNum(final int inputSource, String path, final int goal) {
  139.         int num = 0;
  140.         if (inputSource == 1) {
  141.             num = takeInt(1, (goal == 1) ? 15 : 100, goal);
  142.         } else {
  143.             num = takeNumFromFile(path, goal);
  144.             while (num == 0) {
  145.                 path = takeInPath();
  146.                 num = takeNumFromFile(path, goal);
  147.             }
  148.         }
  149.         return num;
  150.     }
  151.  
  152.     private static int takeNumFromFile(final String path, final int goal) {
  153.         final int SET_LINE_N = 1;
  154.         int lineCounter = 0;
  155.         String line;
  156.         boolean isCorrect = true;
  157.         int num = 0;
  158.         try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
  159.             while ((isCorrect) && ((line = reader.readLine()) != null)) {
  160.                 lineCounter++;
  161.                 if ((lineCounter == SET_LINE_N) && (goal == 1)){
  162.                     try {
  163.                         num = Integer.parseInt(line);
  164.                     } catch (NumberFormatException e) {
  165.                         System.out.println("Incorrect file content!!! Incorrect information in the set line!!!");
  166.                         isCorrect = false;
  167.                     }
  168.                 }
  169.                 if ((lineCounter == SET_LINE_N + 1) && (goal == 2)){
  170.                     try {
  171.                         num = Integer.parseInt(line);
  172.                     } catch (NumberFormatException e) {
  173.                         System.out.println("Incorrect file content!!! Incorrect information in the set line!!!");
  174.                         isCorrect = false;
  175.                     }
  176.                 }
  177.             }
  178.         } catch (IOException e) {
  179.             System.out.println("Input/Output error!!!");
  180.             isCorrect = false;
  181.         }
  182.         if ((isCorrect) && (lineCounter > SET_LINE_N + 1)) {
  183.             System.out.println("Incorrect file content!!!");
  184.             isCorrect = false;
  185.         }
  186.         if (isCorrect) {
  187.             return num;
  188.         } else {
  189.             return 0;
  190.         }
  191.     }
  192.  
  193.     private static int takeInt(final int min, final int max, final int goal) {
  194.         boolean isIncorrect;
  195.         int num = -1;
  196.         do {
  197.             isIncorrect = false;
  198.             System.out.printf("Enter " + (goal == 1? "n" : "A") + " value in range (%s, %s): \n", min, max);
  199.             try {
  200.                 num = Integer.parseInt(scan.nextLine());
  201.             } catch (NumberFormatException e) {
  202.                 isIncorrect = true;
  203.             }
  204.             if (!isIncorrect) {
  205.                 isIncorrect = num > max || num < min;
  206.             }
  207.             System.out.print(isIncorrect ? "Incorrect input, try again!" : "\n");
  208.         } while (isIncorrect);
  209.         return num;
  210.     }
  211.  
  212.  
  213.     static void findSubsetsRec(int[] arr, int i, int sum, ArrayList<Integer> indexes, boolean[][] isFine, int[][] ans) {
  214.         if (i == 0 && sum != 0 && isFine[0][sum]) {
  215.             indexes.add(arr[i]);
  216.             Iterator<Integer> iterator = indexes.iterator();
  217.             for (int k = 0; k < indexes.size(); k++) {
  218.                 ans[counter][k] = indexes.get(k);
  219.             }
  220.             counter++;
  221.             indexes.clear();
  222.             return;
  223.         }
  224.         if (i == 0 && sum == 0) {
  225.             Iterator<Integer> iterator = indexes.iterator();
  226.             for (int k = 0; k < indexes.size(); k++) {
  227.                 ans[counter][k] = indexes.get(k);
  228.             }
  229.             counter++;
  230.             indexes.clear();
  231.             return;
  232.         }
  233.         if (isFine[i - 1][sum]) {
  234.             ArrayList<Integer> b = new ArrayList<>(indexes);
  235.             findSubsetsRec(arr, i - 1, sum, b, isFine, ans);
  236.         }
  237.         if (sum >= arr[i] && isFine[i - 1][sum - arr[i]]) {
  238.             indexes.add(arr[i]);
  239.             findSubsetsRec(arr, i - 1, sum - arr[i], indexes, isFine, ans);
  240.  
  241.         }
  242.     }
  243.  
  244.     static int[][] findAllSubsets(int[] arr, int n, int sum, boolean[][] isFine, int[][] ans) {
  245.         isFine = new boolean[n][sum + 1];
  246.         for (int i = 0; i < n; ++i) {
  247.             isFine[i][0] = true;
  248.         }
  249.         if (arr[0] <= sum)
  250.             isFine[0][arr[0]] = true;
  251.         ArrayList<Integer> p = new ArrayList<>();
  252.         for (int i = 1; i < n; ++i)
  253.             for (int j = 0; j < sum + 1; ++j) {
  254.                 if (arr[i] <= j)
  255.                     isFine[i][j] = isFine[i - 1][j] || isFine[i - 1][j - arr[i]];
  256.                 else isFine[i][j] = isFine[i - 1][j];
  257.             }
  258.         if (!isFine[n - 1][sum]) {
  259.             findAllSubsets(arr, n, sum - 1, isFine, ans);
  260.             return ans;
  261.         }
  262.         findSubsetsRec(arr, n - 1, sum, p, isFine, ans);
  263.         return ans;
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement