Java :: Aufgabe #13 :: Lösung #1

3 Lösungen Lösungen öffentlich
#13

Zahlenfolge berechnen und fortsetzen

Anfänger - Java von pocki - 24.11.2012 um 19:50 Uhr
Es soll eine Zahlenfolge wiefolgt berechnet werden:
Die nacheinander folgenden gleichen Zahlen werden gezählt und zusammen mit der Zahl ausgegeben.
Aus der neu berechneten Zahlenfolge errechnet sich die nächste.

Beispiel:
1. Folge: 112
2. Folge: 2112 (2 Einser und 1 Zweier)
3. Folge: 122112 (1 Zweier, 2 Einser, und 1 Zweier)
usw.

Es soll nun mit dieser ersten Folge startend die 15. Folge berechnet und ausgegeben werden.
Wenn mehrstellige Zahlen vorkommen sind diese gleich auszugeben. Bsp: 12 Einser = 121
#1
vote_ok
von 0 (0 Punkte) - 20.08.2013 um 23:30 Uhr
Main.java
Quellcode ausblenden Java-Code
package de.trainyourprogrammer.java13;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Calculate the 15th step with the following pattern:
 * 
 * 1.) 112 2.) 2112 3.) 122112 ...
 * 
 * @author jsb
 */
public class Main {

	/**
	 * Execute the program.
	 * 
	 * @param args
	 *            Is ignored in our case.
	 */
	public static void main(String[] args) {
		String input = "";

		System.out.println("Geben Sie eine Folge ein:"); // print the request

		BufferedReader buffer = new BufferedReader(new InputStreamReader(
				System.in)); // start reading from the default input

		try { // try to...
			input = buffer.readLine();
		} catch (IOException e) { // ignore occurring IOExceptions
		} finally { // on quit...
			try { // try to...
				buffer.close(); // stop reading the input
			} catch (IOException e) { // again ignore occurring IOExceptions
			}
		}

		String out = getNext(input); // get the row for the input

		for (int counter = 0; counter < 15; counter++) {
			System.out.println(out); // print the row
			out = getNext(out); // get the next row
		}
	}

	/**
	 * Helper method to get the next row of a given String.
	 * 
	 * @param row
	 *            The String to get the row from.
	 * 
	 * @return The next row.
	 */
	private static String getNext(String row) {
		String result = ""; // initialise the result
		char current; // initialise the char storing the currently counted digit

		try {
			current = row.toCharArray()[0]; // set the current digit to the
											// first
											// digit of the former row
		} catch (ArrayIndexOutOfBoundsException e) {
			current = 0; // if that fails set the current digit to null
		}

		int counter = 0; // initialise the counter

		for (char digit : row.toCharArray()) { // for every char in the row...
			if (digit != current) { // check if the digit is still the same
				result = result + Integer.toString(counter) + current; // append
																		// to
																		// result
				current = digit; // set the next digit
				counter = 1; // reset the counter
			} else { // else...
				counter++; // increase the counter
			}
		}

		return result + Integer.toString(counter) + current; // return the
																// result
	}
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben
2112282

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.