Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.Iterator;
- public class lab4_2 {
- static Scanner scan = new Scanner(System.in);
- static int counter = 0;
- public static void main(String[] args) {
- System.out.print("Welcome to the program\nSelect the source for entering numbers:\n1:Console\n2:File\nEnter 1 or 2: ");
- int inputSource = takeSource();
- String path = "";
- if (inputSource == 2){
- path = takeInPath();
- }
- int n = takeNum(inputSource, path, 1);
- int[] arr = fillArr(n);
- int sum = takeNum(inputSource,path, 2);
- System.out.print("Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ");
- int outSource = takeSource();
- int[][] answer = new int[100][n];
- answer = findAllSubsets(arr, n, sum, null, answer);
- output(answer, outSource);
- }
- static void output (final int[][] answer, int source){
- String outPath;
- if (source == 1) {
- consoleOutput(answer);
- } else {
- outPath = takeOutPath();
- FileOutput(outPath, answer);
- }
- }
- static void consoleOutput(final int[][] answer){
- int i = 0;
- int j = 0;
- System.out.println("Answer:");
- while (answer[i][j] != 0){
- while (answer[i][j] != 0){
- System.out.print("[" + answer[i][j]+"] ");
- j++;
- }
- i++;
- j = 0;
- System.out.println();
- }
- }
- static void FileOutput(final String path, final int[][] answer) {
- try (FileWriter fw = new FileWriter(path)) {
- int i = 0;
- int j = 0;
- fw.write("Answer:\n");
- while (answer[i][j] != 0){
- while (answer[i][j] != 0){
- fw.write("[" + answer[i][j]+"] ");
- j++;
- }
- i++;
- j = 0;
- fw.write("\n");
- }
- System.out.println("Done");
- fw.close();
- } catch (Exception e) {
- System.out.println("File error!");
- }
- }
- static int takeSource() {
- final byte CONSOLE = 1;
- final byte FILE = 2;
- boolean isIncorrect;
- byte choice = 0;
- do {
- isIncorrect = false;
- try {
- choice = Byte.parseByte(scan.nextLine());
- } catch (Exception e) {
- System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
- System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice;
- }
- static String takeInPath() {
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.print("File is not found\nEnter file path: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (!path.endsWith(".txt"))) {
- System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- static String takeOutPath() {
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (!path.endsWith(".txt")) {
- System.out.print("It should be a \".txt\" file\nEnter file path: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- private static int[] fillArr(final int n) {
- int[] arr = new int[n];
- for (int i = 0; i < n; i++) {
- arr[i] = i + 1;
- }
- return arr;
- }
- private static int takeNum(final int inputSource, String path, final int goal) {
- int num = 0;
- if (inputSource == 1) {
- num = takeInt(1, (goal == 1) ? 15 : 100, goal);
- } else {
- num = takeNumFromFile(path, goal);
- while (num == 0) {
- path = takeInPath();
- num = takeNumFromFile(path, goal);
- }
- }
- return num;
- }
- private static int takeNumFromFile(final String path, final int goal) {
- final int SET_LINE_N = 1;
- int lineCounter = 0;
- String line;
- boolean isCorrect = true;
- int num = 0;
- try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
- while ((isCorrect) && ((line = reader.readLine()) != null)) {
- lineCounter++;
- if ((lineCounter == SET_LINE_N) && (goal == 1)){
- try {
- num = Integer.parseInt(line);
- } catch (NumberFormatException e) {
- System.out.println("Incorrect file content!!! Incorrect information in the set line!!!");
- isCorrect = false;
- }
- }
- if ((lineCounter == SET_LINE_N + 1) && (goal == 2)){
- try {
- num = Integer.parseInt(line);
- } catch (NumberFormatException e) {
- System.out.println("Incorrect file content!!! Incorrect information in the set line!!!");
- isCorrect = false;
- }
- }
- }
- } catch (IOException e) {
- System.out.println("Input/Output error!!!");
- isCorrect = false;
- }
- if ((isCorrect) && (lineCounter > SET_LINE_N + 1)) {
- System.out.println("Incorrect file content!!!");
- isCorrect = false;
- }
- if (isCorrect) {
- return num;
- } else {
- return 0;
- }
- }
- private static int takeInt(final int min, final int max, final int goal) {
- boolean isIncorrect;
- int num = -1;
- do {
- isIncorrect = false;
- System.out.printf("Enter " + (goal == 1? "n" : "A") + " value in range (%s, %s): \n", min, max);
- try {
- num = Integer.parseInt(scan.nextLine());
- } catch (NumberFormatException e) {
- isIncorrect = true;
- }
- if (!isIncorrect) {
- isIncorrect = num > max || num < min;
- }
- System.out.print(isIncorrect ? "Incorrect input, try again!" : "\n");
- } while (isIncorrect);
- return num;
- }
- static void findSubsetsRec(int[] arr, int i, int sum, ArrayList<Integer> indexes, boolean[][] isFine, int[][] ans) {
- if (i == 0 && sum != 0 && isFine[0][sum]) {
- indexes.add(arr[i]);
- Iterator<Integer> iterator = indexes.iterator();
- for (int k = 0; k < indexes.size(); k++) {
- ans[counter][k] = indexes.get(k);
- }
- counter++;
- indexes.clear();
- return;
- }
- if (i == 0 && sum == 0) {
- Iterator<Integer> iterator = indexes.iterator();
- for (int k = 0; k < indexes.size(); k++) {
- ans[counter][k] = indexes.get(k);
- }
- counter++;
- indexes.clear();
- return;
- }
- if (isFine[i - 1][sum]) {
- ArrayList<Integer> b = new ArrayList<>(indexes);
- findSubsetsRec(arr, i - 1, sum, b, isFine, ans);
- }
- if (sum >= arr[i] && isFine[i - 1][sum - arr[i]]) {
- indexes.add(arr[i]);
- findSubsetsRec(arr, i - 1, sum - arr[i], indexes, isFine, ans);
- }
- }
- static int[][] findAllSubsets(int[] arr, int n, int sum, boolean[][] isFine, int[][] ans) {
- isFine = new boolean[n][sum + 1];
- for (int i = 0; i < n; ++i) {
- isFine[i][0] = true;
- }
- if (arr[0] <= sum)
- isFine[0][arr[0]] = true;
- ArrayList<Integer> p = new ArrayList<>();
- for (int i = 1; i < n; ++i)
- for (int j = 0; j < sum + 1; ++j) {
- if (arr[i] <= j)
- isFine[i][j] = isFine[i - 1][j] || isFine[i - 1][j - arr[i]];
- else isFine[i][j] = isFine[i - 1][j];
- }
- if (!isFine[n - 1][sum]) {
- findAllSubsets(arr, n, sum - 1, isFine, ans);
- return ans;
- }
- findSubsetsRec(arr, n - 1, sum, p, isFine, ans);
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement