Programovanie (1) v C/C++
1-INF-127, ZS 2024/25
Riešenia testu č. 1
Skočit na navigaci
Skočit na vyhledávání
Úloha č. 1
import java.math.*;
public class BigFraction {
private BigInteger numerator;
private BigInteger denominator;
public BigFraction(BigInteger numerator, BigInteger denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public BigInteger getNumerator() {
return numerator;
}
public BigInteger getDenominator() {
return denominator;
}
public BigFraction reduce() {
BigInteger gcd = numerator.gcd(denominator);
BigInteger newNumerator = numerator.divide(gcd);
BigInteger newDenominator = denominator.divide(gcd);
if (denominator.signum() == -1) {
newNumerator = newNumerator.negate();
newDenominator = newDenominator.negate();
}
return new BigFraction(newNumerator, newDenominator);
}
}
Úloha č. 2
package trees;
public class And extends BinaryOperatorNode {
// ...
@Override
public Node negate() {
return new Or(getLeft().negate(), getRight().negate());
}
}
package trees;
public class Or extends BinaryOperatorNode {
// ...
@Override
public Node negate() {
return new And(getLeft().negate(), getRight().negate());
}
}
package trees;
public class Not extends UnaryOperatorNode {
// ...
@Override
public Node negate() {
return getChild();
}
}
package trees;
public class Variable extends Node {
// ...
@Override
public Node negate() {
return new Not(this);
}
}
Úloha č. 3
public class QuadraticForm {
private int[][] coefficients;
private int maximumVariableIndex;
public QuadraticForm() {
coefficients = new int[0][0];
maximumVariableIndex = Integer.MIN_VALUE;
}
public QuadraticForm(int coefficient, int i, int j) {
coefficients = new int[i + 1][j + 1];
coefficients[i][j] = coefficient;
if (coefficient != 0) {
maximumVariableIndex = Math.max(i, j);
} else {
maximumVariableIndex = Integer.MIN_VALUE;
}
}
public QuadraticForm(int[][] coefficients) {
maximumVariableIndex = Integer.MIN_VALUE;
this.coefficients = new int[coefficients.length][];
for (int i = 0; i <= coefficients.length - 1; i++) {
this.coefficients[i] = new int[coefficients[i].length];
for (int j = 0; j <= coefficients[i].length - 1; j++) {
this.coefficients[i][j] = coefficients[i][j];
if (coefficients[i][j] != 0) {
maximumVariableIndex = Math.max(maximumVariableIndex, Math.max(i, j));
}
}
}
}
public int getCoefficient(int i, int j) {
if (coefficients.length <= i) {
return 0;
}
if (coefficients[i].length <= j) {
return 0;
}
return coefficients[i][j];
}
public int maximumVariableIndex() {
return maximumVariableIndex;
}
public int evaluate(int[] values) {
int sum = 0;
for (int i = 0; i <= coefficients.length - 1; i++) {
int xi = 0;
if (i <= values.length - 1) {
xi = values[i];
}
for (int j = 0; j <= coefficients[i].length - 1; j++) {
int xj = 0;
if (j <= values.length - 1) {
xj = values[j];
}
sum += coefficients[i][j] * xi * xj;
}
}
return sum;
}
public QuadraticForm add(QuadraticForm other) {
int n = Math.max(Math.max(maximumVariableIndex(), other.maximumVariableIndex()), -1) + 1;
int[][] newCoefficients = new int[n][n];
for (int i = 0; i <= n - 1; i++) {
for (int j = 0; j <= n - 1; j++) {
newCoefficients[i][j] = getCoefficient(i, j) + other.getCoefficient(i, j);
}
}
return new QuadraticForm(newCoefficients);
}
}