Created
January 1, 2023 15:39
-
-
Save Zerumi/c2d23c01a4399604180234aa12009ad8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Типовой расчет №1 по дисциплине "Дискретная математика" (программный уровень) | |
// Преподователь: Гилев Павел Андреевич | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
// input | |
Scanner scanner = new Scanner(System.in); | |
int countVars = scanner.nextInt(); // n | |
// calculating table size | |
int valuesCount = (int)Math.pow(2, countVars); // 2^n | |
int rowValuesCount = countVars + 1; // n + 1 | |
int[][] istTable = new int[valuesCount][rowValuesCount]; // (2^n) x (n + 1) table | |
// input | |
for(int i = 0; i < valuesCount; i++) { | |
for (int j = 0; j < rowValuesCount; j++) | |
{ | |
istTable[i][j] = scanner.nextInt(); | |
} | |
} | |
// get last column | |
int size = valuesCount; | |
int[] currentArray = new int[size]; | |
for (int i = 0; i < size; i++) | |
{ | |
currentArray[i] = istTable[i][rowValuesCount - 1]; | |
} | |
// calculate Zhegalkin's polynomial variables & convert them to a boolean array | |
boolean[] zhpolynom = new boolean[size]; | |
for (int i = 0; size != 0; i++) | |
{ | |
zhpolynom[i] = currentArray[0] == 1; // push every first value to output result | |
int[] nextArray = new int[size - 1]; | |
for (int j = 0; j < size - 1; j++) | |
{ | |
int firstElement = currentArray[j]; | |
int secondElement = currentArray[j + 1]; | |
nextArray[j] = (firstElement + secondElement) % 2; | |
} | |
currentArray = nextArray; | |
size--; | |
} | |
// transform boolean array to output string | |
int countPluses = -1; | |
for (boolean value : zhpolynom) | |
{ | |
if(value) countPluses++; | |
} | |
for (int i = 0; i < zhpolynom.length; i++) | |
{ | |
if(zhpolynom[i]) | |
{ | |
if (i == 0) | |
{ | |
System.out.print("1 + "); | |
countPluses--; | |
continue; | |
} | |
String binaryIndex = String.format("%0" + countVars + "d", Integer.parseInt(Integer.toBinaryString(i))); | |
char[] binaryIndexChars = binaryIndex.toCharArray(); | |
for (int j = 0; j < binaryIndexChars.length; j++) | |
{ | |
if(binaryIndexChars[j] == '1') | |
{ | |
System.out.print((char)(97+j)); | |
} | |
} | |
if (countPluses-- > 0) | |
{ | |
System.out.print(" + "); | |
} | |
} | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Тесты:
Тест 1
Пример ввода:
2
0 0 0
0 1 1
1 0 1
1 1 1
Пример вывода:
b + a + ab
Тест 2
Пример ввода:
5
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 1 0 0
0 0 0 1 1 0
0 0 1 0 0 0
0 0 1 0 1 0
0 0 1 1 0 0
0 0 1 1 1 1
0 1 0 0 0 0
0 1 0 0 1 0
0 1 0 1 0 0
0 1 0 1 1 1
0 1 1 0 0 0
0 1 1 0 1 1
0 1 1 1 0 1
0 1 1 1 1 1
1 0 0 0 0 0
1 0 0 0 1 0
1 0 0 1 0 0
1 0 0 1 1 1
1 0 1 0 0 0
1 0 1 0 1 1
1 0 1 1 0 1
1 0 1 1 1 1
1 1 0 0 0 0
1 1 0 0 1 1
1 1 0 1 0 1
1 1 0 1 1 1
1 1 1 0 0 1
1 1 1 0 1 1
1 1 1 1 0 1
1 1 1 1 1 1
Пример вывода:
cde + bde + bce + bcd + bcde + ade + ace + acd + acde + abe + abd + abde + abc + abce + abcd