|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
public class Fak { // factorial recursive (not tail recursive) public static int facRec(int n) { if (n <= 1) { return 1; } else { return n * facRec(n - 1); } } // factorial tail recursive public static int facTailRec(int n) { return facTailRecHelper(n, 1); } // helper function factorial tail recursive private static int facTailRecHelper(int n, int k) { if (n <= 1) { return k; } else { return facTailRecHelper(n - 1, n * k); } } // factorial "tail" iterative public static int facIt(int n, int k) { // tailRec into iterative version while (true) { if (n <= 1) { return k; } else { k = n * k; n = n - 1; } } } // factorial iterative "intuitively" public static int facItNice(int n) { int k = 1; for (int i = 2; i <= n; i++) k *= i; return k; } public static void main(String[] args) { int n = 5; System.out.println("iterative: " + facIt(n, 1)); System.out.println("tail-recursiv: " + facTailRec(n)); System.out.println("recursive: " + facRec(n)); System.out.println("iterative cleaned up: " + facItNice(n)); } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
public class ToolboxSolution { // public static boolean isEven(int n) { // if (n > 0) return isOdd(n - 1); // else if (n < 0) return isOdd(n + 1); // else return true; // } // // public static boolean isOdd(int n) { // if (n > 0) return isEven(n - 1); // else if (n < 0) return isEven(n + 1); // else return false; // } // // a sligthly faster version is given below: // public static boolean isOdd(int n) { if (n == 1 || n == -1) return true; else if (n == 0) return false; else if (n > 1) return isOdd(n - 2); else return isOdd(n + 2); } public static int evenSum(int n) { if (isOdd(n)) { n = n < 0 ? n + 1 : n - 1; // n is even, after this statement } return n < 0 ? -evenSumHelper(-n, 0) : evenSumHelper(n, 0); } private static int evenSumHelper(int n, int acc) { if (n == 0) return acc; else return evenSumHelper(n - 2, acc + n); } public static int multiplication(int x, int y) { if (x == 0 || y == 0) return 0; int n = multiplication(x < 0 ? -x : x, y, 0); return x < 0 ? -n : n; } private static int multiplication(int x, int y, int acc) { if (x == 0) return acc; return multiplication(x - 1, y, acc + y); } public static void reverse(int[] m) { if (m.length > 0) reverse(m, 0); } private static void reverse(int[] m, int i) { if (i >= m.length / 2) return; int tmp = m[i]; m[i] = m[m.length - 1 - i]; m[m.length - 1 - i] = tmp; reverse(m, i + 1); } public static int numberOfOddIntegers(int[] m) { return numberOfOddIntegers(m, 0, 0); } private static int numberOfOddIntegers(int[] m, int i, int j) { if (i == m.length) return j; if ((m[i] & 1) == 1) return numberOfOddIntegers(m, i + 1, j + 1); else return numberOfOddIntegers(m, i + 1, j); } public static int[] filterOdd(int[] m) { int[] n = new int[numberOfOddIntegers(m)]; filterOdd(m, n, 0, 0); return n; } private static void filterOdd(int[] m, int[] n, int i, int j) { if (i == m.length) return; if ((m[i] & 1) == 1) { n[j] = m[i]; filterOdd(m, n, i + 1, j + 1); } else filterOdd(m, n, i + 1, j); } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
public class Tailrec1 { // x^y public static int pow(int x, int y) { return java.math.BigInteger.valueOf(x).pow(y).intValueExact(); } // function f recursive public static int frec(int x) { return grec(x, 0); } // helper function g recursive public static int grec(int x, int pos) { if (x / 10 == 0) { return pow(x, pos); } return pow(x % 10, pos) + grec(x / 10, ++pos); } // function f tail recursive public static int ftailrec(int x) { return gtailrec(x, 0, 0); } // helper function g tail recursive public static int gtailrec(int x, int pos, int calc) { if (x / 10 == 0) return calc + pow(x, pos); int nextpos = pos + 1; return gtailrec(x / 10, nextpos, calc + pow(x % 10, pos)); } public static void main(String[] args) { int n = 12345; System.out.println("f recursive: " + frec(n)); System.out.println("f tailrec: " + ftailrec(n)); } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
public class Quicksort { private static void quicksort(int[] numbers, int left, int right) { if (right > left) { // sonst ist nichts (mehr) zu tun // Partitionieren und neuen Index des Pivot-Elements sichern: // int pivotIndex = partition(numbers, left, right); int pivotIndex = partitionRek(numbers, left, right); // Sortiere linke Seite: quicksort(numbers, left, pivotIndex - 1); // Sortiere rechte Seite: quicksort(numbers, pivotIndex + 1, right); } } private static int partition(int[] numbers, int left, int right) { int leftScan = left; // Links auf das erste Element int rightScan = right; // Rechts auf das letzte Element int pivot = numbers[right]; // Wahl des Pivot-Elements while (leftScan < rightScan) { while (leftScan < rightScan && numbers[leftScan] < pivot) { leftScan++; // eins weiter nach rechts } while (leftScan < rightScan && numbers[rightScan] >= pivot) { rightScan--; // eins weiter nach links } if (leftScan < rightScan) { // Falls leftScan und rightScan sich noch // nicht kreuzen, dann Elemente tauschen swap(numbers, leftScan, rightScan); } // Ansonsten ist die Partition fertig } // Pivot-Element an die Nahtstelle bringen swap(numbers, leftScan, right); return leftScan; // Index der Nahtstelle ausliefern } private static void swap(int[] numbers, int index1, int index2) { int temp = numbers[index1]; numbers[index1] = numbers[index2]; numbers[index2] = temp; } public static void main(String[] args) { // Initialisierung int count = 20; int[] test = new int[count]; for (int i = 0; i < test.length; i++) { test[i] = (int) (10 * Math.random()); System.out.print(test[i] + " "); } System.out.println(); // Sortieren quicksort(test, 0, test.length - 1); // Ausgabe des sortierten Feldes for (int i = 0; i < test.length; i++) { System.out.print(test[i] + " "); } System.out.println(); } // Ohne Schleifchen partitionieren: private static int partitionRek(int[] numbers, int left, int right) { return partitionRek(numbers, right, left, right); } private static int partitionRek(int[] numbers, int pivotIndex, int left, int right) { if (left >= right) { swap(numbers, pivotIndex, right); return right; } int pivot = numbers[pivotIndex]; if (numbers[left] < pivot) { return partitionRek(numbers, pivotIndex, left + 1, right); } if (numbers[right] >= pivot) { return partitionRek(numbers, pivotIndex, left, right - 1); } swap(numbers, left, right); return partitionRek(numbers, pivotIndex, left, right); } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
import java.awt.*; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.*; import java.io.File; public class PenguinPen extends JPanel { public static final int WALL = -3; public static final int FREE = -2; public static final int OUTSIDE = -1; public static final int ZOOKEEPER = 0; public static final int PENGUIN_OOO = 1; public static final int PENGUIN_OOI = 2; public static final int PENGUIN_OIO = 3; public static final int PENGUIN_OII = 4; public static final int PENGUIN_IOO = 5; public static final int MOVE_LEFT = 0; public static final int MOVE_RIGHT = 1; public static final int MOVE_UP = 2; public static final int MOVE_DOWN = 3; public static final int NO_MOVE = -1; private class Field extends JPanel { Point p; int x,y; public Field(int x, int y) { this.x = x; this.y = y; p = getLocation(); } public void paint(Graphics g) { super.paint(g); if (myState[x][y] == WALL) { GradientPaint gradient = new GradientPaint(10, 50, Color.GRAY, getWidth(), 0, Color.DARK_GRAY); ((Graphics2D) g).setPaint(gradient); } else { GradientPaint gradient = new GradientPaint(0, 50, Color.WHITE, getWidth(), 0, Color.GRAY); ((Graphics2D) g).setPaint(gradient); } g.fillRect(p.getLocation().x, p.getLocation().y, getWidth() * 2, getHeight()); switch (myState[x][y]) { case ZOOKEEPER: paintSymbol(g, Color.RED); break; case PENGUIN_OOO: drawPeng(g, 0); break; case PENGUIN_OOI: drawPeng(g, 1); break; case PENGUIN_OIO: drawPeng(g, 2); break; case PENGUIN_OII: drawPeng(g, 3); break; case PENGUIN_IOO: drawPeng(g, 4); break; default: break; } } private void paintSymbol(Graphics g, Color c) { GradientPaint gradient = new GradientPaint(15, 0, c, getWidth(), 0, Color.LIGHT_GRAY); ((Graphics2D) g).setPaint(gradient); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.fillOval((int) (getWidth() * 0.3), (int) (getHeight() * 0.3), (int) (getWidth() * 0.5), (int) (getHeight() * 0.5)); } private void drawPeng(Graphics g, int index) { if (peng[index] == null) { if (index == 0) paintSymbol(g, Color.YELLOW); if (index == 1) paintSymbol(g, Color.BLUE); if (index == 2) paintSymbol(g, Color.BLACK); if (index == 3) paintSymbol(g, Color.MAGENTA); if (index == 4) paintSymbol(g, Color.GREEN); return; } ((Graphics2D) g).drawImage (peng[index], 0, 0, getWidth(), getHeight(), 0, 0, peng[index].getWidth(null), peng[index].getHeight(null), null); } } private int[][] myState; private static PenguinPen myPenguinPen; private JPanel fieldPanel = new JPanel(); private JFrame myFrame; public PenguinPen() { for (int i = 1; i <= 5; i++) { File f = new File("tux" + i + ".png"); if(f.exists() && !f.isDirectory()) { peng[i-1] = Toolkit.getDefaultToolkit().getImage(f.getAbsolutePath()); } } } private PenguinPen(int[][] state) { this(); myState = new int[state.length][state[0].length]; Field[][] field = new Field[myState.length][myState[0].length]; for (int y = 0; y < myState[0].length; y++) { for (int x = 0; x < myState.length; x++) { field[x][y] = new Field(x, y); fieldPanel.add(field[x][y]); myState[x][y] = state[x][y]; } } myFrame = new JFrame("Hilf Pingu!"); fieldPanel.setLayout(new GridLayout(myState[0].length, myState.length)); myFrame.getContentPane().add(fieldPanel); myFrame.setSize ((int)(IWH * scale) * myState.length, (int)(IWH * scale) * myState[0].length); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.addComponentListener(new ComponentHandler()); myFrame.addKeyListener(new KeyHandler()); myFrame.setVisible(true); update(state); } private void update(int[][] state) { for (int x = 0; x < myState.length; x++) for (int y = 0; y < myState[0].length; y++) myState[x][y] = state[x][y]; fieldPanel.repaint(); } public static void draw(int[][] myState) { if (myPenguinPen == null) { myPenguinPen = new PenguinPen(myState); try { Thread.sleep(100); } catch (InterruptedException ie) { /* Intentionally left blank */ } } while (myPenguinPen.pause) { try { Thread.sleep(50); } catch (InterruptedException ie) {} } myPenguinPen.update(myState); try { Thread.sleep(wannaSleep); } catch (InterruptedException ie) { /* Intentionally left blank */ } } private class ComponentHandler extends ComponentAdapter { @Override public void componentResized(ComponentEvent e) { repaint(); } } private class KeyHandler extends KeyAdapter { @Override public void keyPressed(KeyEvent ke) { switch (ke.getKeyCode()) { case KeyEvent.VK_ESCAPE: System.exit(0); break; case KeyEvent.VK_SPACE: pause = !pause; System.out.println(pause?"break":"continue"); break; case KeyEvent.VK_LEFT: step(MOVE_LEFT); break; case KeyEvent.VK_RIGHT: step(MOVE_RIGHT); break; case KeyEvent.VK_UP: step(MOVE_UP); break; case KeyEvent.VK_DOWN: step(MOVE_DOWN); break; default: break; } } } private Image[] peng = new Image[5]; private static final long wannaSleep = 100; private static final int IWH = 40; private static double scale = 1.0; private boolean pause = false; private static Object cLock = new Object(); private static String moveQueue = ""; private void step(int direction) { synchronized(cLock) { try { Thread.sleep(10); } catch (InterruptedException ie) { /* Intentionally left blank */ } moveQueue+=""+direction; } } public static int nextStep() { synchronized(cLock) { if(moveQueue.length() == 0) { return NO_MOVE; } char c = moveQueue.charAt(0); moveQueue = moveQueue.substring(1,moveQueue.length()); return c-(int)'0'; } } public static int[][] generatePenguinPen(int width, int height) { int seed = (new Random()).nextInt(); System.out.println("generatePenguinPen: seed=" + seed); return generatePenguinPen(width, height, seed); } public static int[][] generateStandardPenguinPen(int width, int height) { return generatePenguinPen(width, height, 0); } public static int[][] generatePenguinPen(int width, int height, int seed) { width = Math.max(width, 3); height = Math.max(height, 3); int[][] myState = new int[width][height]; // Borders: for (int i = 0; i < myState.length; i++) { for (int j = 0; j < myState[i].length; j++) { myState[i][j] = FREE; } myState[i][0] = WALL; myState[i][myState[i].length - 1] = WALL; } for (int j = 0; j < myState[0].length; j++) { myState[0][j] = WALL; } for (int j = 0; j < myState[myState.length - 1].length; j++) { myState[myState.length - 1][j] = WALL; } // Random obstacles: Random random = new Random(); random.setSeed(seed); for (int i = 0; i < myState.length; i++) { for (int j = 0; j < myState[i].length; j++) { if (random.nextInt(6) == 0) { myState[i][j] = WALL; } } } // Random penguins: for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if(WALL != myState[x][y]) { switch (random.nextInt(31)) { case 0 : myState[x][y] = PENGUIN_OOO; break; case 1 : myState[x][y] = PENGUIN_OOI; break; case 2 : myState[x][y] = PENGUIN_OIO; break; case 3 : myState[x][y] = PENGUIN_OII; break; case 4 : myState[x][y] = PENGUIN_IOO; break; default : break ; } } } } // Entrance myState[1][0] = ZOOKEEPER; myState[1][1] = FREE; myState[2][1] = FREE; return myState; } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public class HilfPingu extends PenguinPen { private static final int[][] penguinPen = generatePenguinPen(24, 17); public static void move(int direction) { System.out.println(direction); } /*********************************************/ /* Ab hier soll nichts mehr geändert werden! */ /*********************************************/ public static void main(String[] args) { draw(penguinPen); handleUserInput(); } private static void handleUserInput() { while(true) { try { Thread.sleep(10); } catch (InterruptedException ie) { /* Intentionally left blank */ } int step = nextStep(); if (step != NO_MOVE) { // System.out.print(step+","); move(step); } } } } |