Last active
May 29, 2018 07:04
-
-
Save nikoladimitroff/1cb5911d8df8a19bfe85e28b5fcc6258 to your computer and use it in GitHub Desktop.
Решение на задачи 1 и 2 от ДИ 12.07.2016
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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package fmidemo; | |
/** | |
* | |
* @author Nikola | |
*/ | |
public class ArraySymmetricElements { | |
static double computeAverageOfElement(double matrix[][], int i, int j) { | |
double sum = 0; | |
int symmetricGroupCount = 1; | |
// the element itself | |
sum += matrix[i][j]; | |
// primary diagonal | |
if (i != j) { | |
sum += matrix[j][i]; | |
symmetricGroupCount++; | |
} | |
// secondary diagonal | |
if (i != matrix.length - 1 - j) { | |
sum += matrix[matrix.length - 1 - j][matrix.length - 1 - i]; | |
symmetricGroupCount++; | |
} | |
// symmetry about the center point | |
if (i != j && i != matrix.length - 1 - j) { | |
sum += matrix[matrix.length - 1 - i][matrix.length - 1 - j]; | |
symmetricGroupCount++; | |
} | |
return sum / symmetricGroupCount; | |
} | |
static void computeSymmetricGroupAverages(double matrix[][]) { | |
double[][] workingCopy = new double[matrix.length][matrix.length]; | |
for (int i = 0; i < matrix.length; i++) { | |
for (int j = 0; j < matrix.length; j++) { | |
workingCopy[i][j] = computeAverageOfElement(matrix, i, j); | |
} | |
} | |
for (int i = 0; i < matrix.length; i++) { | |
for (int j = 0; j < matrix.length; j++) { | |
matrix[i][j] = workingCopy[i][j]; | |
} | |
} | |
} | |
static void printMatrix(double matrix[][]) { | |
for (int i = 0; i < matrix.length; i++) { | |
for (int j = 0; j < matrix.length; j++) { | |
System.out.print(matrix[i][j]); | |
System.out.print(' '); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
double[][] matrixFromExample = { | |
{ 1, 2, 3 }, | |
{ 4, 5, 6 }, | |
{ 7, 8, 9 } | |
}; | |
double[][] anotherExample = { | |
{ 0, 1, 0, 1, 1 }, | |
{ 1, 0, 1, 0, 1 }, | |
{ 1, 0, 3, 1, 1 }, | |
{ 1, 0, 1, 1, 1 }, | |
{ 1, 1, 0, 1, 1 }, | |
}; | |
computeSymmetricGroupAverages(anotherExample); | |
printMatrix(anotherExample); | |
} | |
} |
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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package fmidemo; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
class TreeNode { | |
TreeNode(int v) { | |
this.value = v; | |
this.children = new ArrayList<TreeNode>(); | |
} | |
int value; | |
ArrayList<TreeNode> children; | |
} | |
/** | |
* | |
* @author Nikola | |
*/ | |
public class TreeMaxSumPath { | |
static TreeNode parseTree(String description) { | |
// Each subtree starts with a ( so ignore that | |
// The current node value is between the ( and the next ( | |
int childDescriptorStartIndex = description.indexOf("(", 1); | |
String valueSubstring = description.substring(1, childDescriptorStartIndex).trim(); | |
int value = Integer.parseInt(valueSubstring); | |
TreeNode tree = new TreeNode(value); | |
// We know the descriptor ends at the second to last ) because the last ) is the closing | |
// parenthesis for the whole subtree | |
int childDescriptorEndIndex = description.lastIndexOf(")", description.length() - 1); | |
String childDescriptor = description.substring(childDescriptorStartIndex + 1, childDescriptorEndIndex); | |
// Split all child subtrees by commas; make sure to only look at commas at the top level | |
int currentSubtreeStart = 0; | |
int possibleSubtreeEnd = 0; | |
int parenthesisCount = 0; | |
for (int i = 0; i < childDescriptor.length(); i++) { | |
if (childDescriptor.charAt(i) == '(') { | |
parenthesisCount++; | |
} | |
if (childDescriptor.charAt(i) == ')') { | |
parenthesisCount--; | |
// This might be the last ) in the current subtree so save it | |
possibleSubtreeEnd = i; | |
} | |
// A subtree must end either on a comma or the second-to-last ) | |
if (childDescriptor.charAt(i) == ',' || childDescriptor.charAt(i) == ')') { | |
// This symbol marks the end of a subtree only if it's top level | |
if (parenthesisCount == 0) { | |
// hooray! take the substring and remember to include the () at both ends | |
String subtreeDescriptor = childDescriptor.substring(currentSubtreeStart, possibleSubtreeEnd + 1); | |
TreeNode subtree = parseTree(subtreeDescriptor); | |
tree.children.add(subtree); | |
// Move the loop to the next subtree | |
currentSubtreeStart = childDescriptor.indexOf("(", i + 1); | |
if (currentSubtreeStart == -1) { | |
// No next subtree? Either the string is ill-formatted or we reached the end; | |
break; | |
} | |
i = currentSubtreeStart; | |
} | |
} | |
} | |
return tree; | |
} | |
static int computeMaxPathInTree(TreeNode tree) { | |
int currentMax = 0; | |
for (int i = 0; i < tree.children.size(); i++) { | |
int subtreeMax = computeMaxPathInTree(tree.children.get(i)); | |
if (subtreeMax > currentMax) { | |
currentMax = subtreeMax; | |
} | |
} | |
return tree.value + currentMax; | |
} | |
static void findMaxSumPath(String filepath) throws IOException { | |
String content = Files.readAllLines(Paths.get(filepath)).get(0); | |
TreeNode tree = parseTree(content); | |
int maxSum = computeMaxPathInTree(tree); | |
System.out.println(maxSum); | |
} | |
public static void main(String[] args) throws IOException { | |
String examExample = "examTree.txt"; | |
findMaxSumPath(examExample); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment