add UE09_Rekursion2

This commit is contained in:
AlexBa16
2024-11-29 11:28:20 +01:00
parent 6cfc5bdac9
commit 830acf091c
4 changed files with 144 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
package UE07_221024_RegExp3;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChemicalEquations {
public static void main(String[] args) {
/*
HCl + Na -> NaCl + H2
2 HCl + 2 Na -> 2 NaCl + H2
12 CO2 + 6 H2O -> 2 C6H12O6 + 12 O2
N2 + H2 -> NH3
2 KClO3 -> 2 KCl + 3 O2
CaCO3 + HCl -> CaCl2 + H2O + CO2
C3H8 + 5 O2 -> 3 CO2 + 4 H2O
2 Ag + O2 -> 2 AgO
Fe2O3 + 3 CO -> 2 Fe + 3 CO2
Mg + O2 -> MgO
*/
Scanner scanner = new Scanner(System.in);
String input;
while (!(input = scanner.nextLine()).equals("end"))
System.out.println((analyze(input) ? "formally correct" : "incorrect") + "\n");
}
public static boolean analyze(String formular) {
String[] s = formular.split("->");
try {
Map<String, Integer> first = mapElements(s[0]);
Map<String, Integer> second = mapElements(s[1]);
if (first.isEmpty() || second.isEmpty()) return false;
return first.equals(second);
} catch (Exception e) {
return false;
}
}
public static Map<String, Integer> mapElements(String s) {
Map<String, Integer> elements = new TreeMap<>();
String[] parts = s.replaceAll(" ", "").split("\\+");
Pattern pattern = Pattern.compile("\\d*([A-Z][a-z]?|[A-Z])(\\d*)");
Pattern patternC = Pattern.compile("^(\\d+)[a-zA-Z]+");
int coefficient;
int number;
for (String part : parts) {
coefficient = 1;
Matcher matcher = pattern.matcher(part);
Matcher matcherC = patternC.matcher(part);
while (matcherC.find()) try {
coefficient = Integer.parseInt(matcherC.group(1));
} catch (Exception e) {
coefficient = 1;
}
while (matcher.find()) {
try {
number = Integer.parseInt(matcher.group(2));
} catch (Exception e) {
number = 1;
}
elements.put(matcher.group(1), elements.getOrDefault(matcher.group(1), 0) + (number * coefficient));
}
}
return elements;
}
}

View File

@@ -0,0 +1,57 @@
package UE09_191124_Rekursion2;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class UE09_Rekursion2 {
public static void main(String[] args) {
// int[][] file1 = readFile("src/UE09_191124_Rekursion2/UE09_triangle1.txt");
// int[][] file2 = readFile("src/UE09_191124_Rekursion2/UE09_triangle2.txt");
// int[][] file3 = readFile("src/UE09_191124_Rekursion2/UE09_triangle.txt");
// System.out.println("\n" + maxTotalRekursion(file1, 0, 0));
// System.out.println("\n" + maxTotalRekursion(file2, 0, 0));
// System.out.println("\n" + maxTotalRekursion(file3, 0, 0)); //Braucht ewig lange, da File sehr groß
System.out.println(collatz());
}
public static int[][] readFile(String file) {
try {
List<String> lines = Files.readAllLines(Path.of(file));
int[][] output = new int[lines.size()][];
for (int i = 0; i < output.length; i++) {
String[] line = lines.get(i).trim().split(" ");
output[i] = new int[line.length];
for (int j = 0; j < line.length; j++) output[i][j] = Integer.parseInt(line[j]);
}
return output;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static long maxTotalRekursion(int[][] input, int x, int y) {
return (x >= input.length - 1) ? input[x][y] : Long.max(maxTotalRekursion(input, x + 1, y), maxTotalRekursion(input, x + 1, y + 1)) + input[x][y];
}
public static long collatz() {
long startingNumber = 0, maxLength = 0;
for (int i = 1; i < 1000000; i++) {
long length = longestCollatzSequence(new ArrayList<>(), i).size();
if (length > maxLength) {
maxLength = length;
startingNumber = i;
}
}
return startingNumber;
}
public static List<Long> longestCollatzSequence(List<Long> l, long n) {
if (n == 1) return l;
l.add(n);
n = ((n & 1) == 0) ? (n / 2) : (3 * n + 1);
return longestCollatzSequence(l, n);
}
}

View File

@@ -0,0 +1,4 @@
3
7 4
2 4 6
8 5 9 3

View File

@@ -0,0 +1,15 @@
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23