Inital commit
This commit is contained in:
commit
792fc2f410
7 changed files with 210 additions and 0 deletions
6
.classpath
Normal file
6
.classpath
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
.project
Normal file
17
.project
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<projectDescription>
|
||||
<name>algueb1</name>
|
||||
<comment/>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
7
.settings/org.eclipse.jdt.core.prefs
Normal file
7
.settings/org.eclipse.jdt.core.prefs
Normal file
|
@ -0,0 +1,7 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"configurations": [
|
||||
{
|
||||
"type": "java",
|
||||
"name": "CodeLens (Launch) - App",
|
||||
"request": "launch",
|
||||
"mainClass": "app.App",
|
||||
"projectName": "algueb1"
|
||||
}
|
||||
]
|
||||
}
|
BIN
Uebungsblatt1.pdf
Normal file
BIN
Uebungsblatt1.pdf
Normal file
Binary file not shown.
BIN
bin/app/App.class
Normal file
BIN
bin/app/App.class
Normal file
Binary file not shown.
169
src/app/App.java
Normal file
169
src/app/App.java
Normal file
|
@ -0,0 +1,169 @@
|
|||
package app;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println(sumTo(30));
|
||||
System.out.println(sumToRecursive(30));
|
||||
System.out.println(fibonacciRecursive(new ArrayList<Integer>(), 200));
|
||||
System.out.println(fibonacci(new ArrayList<Integer>(), 200));
|
||||
System.out.println(euklRecursive(544, 391));
|
||||
System.out.println(eukl(544, 391));
|
||||
|
||||
int[] a = { 1, 2, 3, 4 };
|
||||
System.out.println(sumArray(a));
|
||||
int[] b = { 3, 2, 4, 1 };
|
||||
System.out.println(max(b));
|
||||
int[] c = { 1, 2, 3, 4 };
|
||||
System.out.println(Arrays.toString(reverse(c)));
|
||||
double[][] d = { { 1, 2 }, { 10, 20 }, { -5, 5 } };
|
||||
System.out.println(Arrays.toString(average(d)));
|
||||
double[][] e = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
|
||||
double f = 2;
|
||||
System.out.println(Arrays.deepToString(multiply(e, f)));
|
||||
double[][] g = { { 1, 2 }, { 3, 4 } };
|
||||
double[][] h = { { 5, 6 }, { 7, 8 } };
|
||||
System.out.println(Arrays.deepToString(multiply(g, h)));
|
||||
double[] i = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
|
||||
System.out.println(maxSum(i));
|
||||
}
|
||||
|
||||
/** Aufgabe 1 */
|
||||
public static int sumTo(int target) {
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= target; i++) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static int sumToRecursive(int target) {
|
||||
if (target == 0)
|
||||
return target;
|
||||
return target + sumToRecursive(target - 1);
|
||||
}
|
||||
|
||||
/** Aufgabe 2.1 */
|
||||
public static List<Integer> fibonacciRecursive(List<Integer> fib, int max) {
|
||||
// Initialize List
|
||||
if (fib.size() == 0) {
|
||||
fib.add(0);
|
||||
fib.add(1);
|
||||
fib.add(1);
|
||||
}
|
||||
fib.add(fib.get(fib.size() - 1) + fib.get(fib.size() - 2));
|
||||
if (fib.get(fib.size() - 1) >= max)
|
||||
return fib;
|
||||
return fibonacciRecursive(fib, max);
|
||||
}
|
||||
|
||||
/** Aufgabe 2.2 */
|
||||
public static List<Integer> fibonacci(List<Integer> fib, int max) {
|
||||
// Initialize List
|
||||
if (fib.size() == 0) {
|
||||
fib.add(0);
|
||||
fib.add(1);
|
||||
fib.add(1);
|
||||
}
|
||||
/** Bessere Effizienz da Abbruchbedinngung einduetig erkennbar */
|
||||
while (fib.get(fib.size() - 1) <= max) {
|
||||
fib.add(fib.get(fib.size() - 1) + fib.get(fib.size() - 2));
|
||||
}
|
||||
return fib;
|
||||
}
|
||||
|
||||
/** Aufgabe 3 */
|
||||
public static int euklRecursive(int a, int b) {
|
||||
int rest = a % b;
|
||||
if (rest == 0)
|
||||
return b;
|
||||
return euklRecursive(b, rest);
|
||||
}
|
||||
|
||||
public static int eukl(int a, int b) {
|
||||
int rest = a % b;
|
||||
while (rest != 0) {
|
||||
a = b;
|
||||
b = rest;
|
||||
rest = a % b;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/** Aufgabe 4 */
|
||||
public static int sumArray(int[] a) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
sum += a[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/** Aufgabe 5 */
|
||||
public static int max(int[] a) {
|
||||
int max = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (a[i] > max)
|
||||
max = a[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
/** Aufgabe 6 */
|
||||
public static int[] reverse(int[] a) {
|
||||
int[] b = new int[a.length];
|
||||
for (int i = a.length - 1; i >= 0; i--) {
|
||||
b[a.length - 1 - i] = a[i];
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/** Aufgabe 7 */
|
||||
public static double[] average(double[][] a) {
|
||||
double[] b = new double[a.length];
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
b[i] = (a[i][0] + a[i][1]) / 2;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/** Aufgabe 8 */
|
||||
public static double[][] multiply(double[][] a, double b) {
|
||||
for (int i = 0; i < a.length; i++)
|
||||
for (int ii = 0; ii < a[i].length; ii++)
|
||||
a[i][ii] = a[i][ii] * b;
|
||||
return a;
|
||||
}
|
||||
|
||||
/** Aufgabe 9 */
|
||||
public static double[][] multiply(double[][] a, double[][] b) {
|
||||
double[][] c = new double[a.length][a.length];
|
||||
for (int i = 0; i < a.length; i++)
|
||||
for (int ii = 0; ii < a[i].length; ii++)
|
||||
// a[i][ii] = a[i][ii] * b[ii][i];
|
||||
for (int iii = 0; iii < a[i].length; iii++)
|
||||
c[i][ii] = a[i][iii] * b[iii][ii] + c[i][ii];
|
||||
return c;
|
||||
}
|
||||
|
||||
/** Aufgabe 10 */
|
||||
public static int maxSum(double[] a) {
|
||||
int chunkSize = 4;
|
||||
int maxSum = 0;
|
||||
int currSum = 0;
|
||||
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (i < a.length - chunkSize)
|
||||
for (int ii = 0; ii < chunkSize; ii++) {
|
||||
currSum += a[i + ii];
|
||||
}
|
||||
if (maxSum < currSum)
|
||||
maxSum = currSum;
|
||||
currSum = 0;
|
||||
}
|
||||
return maxSum;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue