Java :: Aufgabe #13

3 Lösungen Lösungen öffentlich

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

Lösungen:

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
	}
}
vote_ok
von ElPapito (2690 Punkte) - 05.05.2015 um 15:43 Uhr
Quellcode ausblenden Java-Code

/**
 * @author ElPapito
 * @date 05.05.2015
 */

public class ZahlenfolgeBerechnenUndFortsetzen {

	public static void main(String[] args) {
		String test = "112";

		for (int i = 1; i <= 15; i++) {
			System.out.println(i + ". Folge: " + test);
			test = next(test);
		}
	}

	public static String next(String string) {
		String newString = "";

		char digit = string.charAt(0);
		int counter = 1;

		for (int i = 1; i < string.length(); i++) {
			if (string.charAt(i) == digit) {
				counter++;
			} else {
				newString = newString + counter + digit;
				digit = string.charAt(i);
				counter = 1;
			}
		}

		newString = newString + counter + digit;
		return newString;
	}

}

vote_ok
von 0 (0 Punkte) - 22.01.2021 um 22:18 Uhr
Quellcode ausblenden Java-Code
package de.patrick260.trainYourProgrammer.exercise_13;

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

public class ZahlenfolgeBerechnenUndFortsetzen {

    public static void main(String[] args) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Bitte gebe eine Zahlenfolge ein: ");

        String input = "";
        try {
            input = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (!isNumber(input)) {
            System.out.println("Invalid input!");
            System.out.print("Bitte gebe eine Zahlenfolge ein: ");
            try {
                input = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        for (int i = 0; i < 15; i++) {
            input = getNext(input);
            System.out.println((i + 1) + ". Folge: " + input);
        }

    }

    public static String getNext(String s) {

        String ss = "";
        char last = s.charAt(0);
        int count = 1;

        for (int i = 1; i < s.length(); i++) {

            if (last == s.charAt(i)) {
                count++;
            } else {
                ss = ss + count + last;
                count = 1;
                last = s.charAt(i);
            }

        }

        ss = ss + count + last;
        return ss;
    }

    public static boolean isNumber(String s) {

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != '1' && s.charAt(i) != '2' && s.charAt(i) != '3' && s.charAt(i) != '4' && s.charAt(i) != '5' && s.charAt(i) != '6' && s.charAt(i) != '7' && s.charAt(i) != '8' && s.charAt(i) != '9' && s.charAt(i) != '0') {
                return false;
            }
        }
        return true;
    }

}
package de.patrick260.trainYourProgrammer.exercise_13;

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

public class ZahlenfolgeBerechnenUndFortsetzen {

    public static void main(String[] args) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Bitte gebe eine Zahlenfolge ein: ");

        String input = "";
        try {
            input = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (!isNumber(input)) {
            System.out.println("Invalid input!");
            System.out.print("Bitte gebe eine Zahlenfolge ein: ");
            try {
                input = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        for (int i = 0; i < 15; i++) {
            input = getNext(input);
            System.out.println((i + 1) + ". Folge: " + input);
        }

    }

    public static String getNext(String s) {

        String ss = "";
        char last = s.charAt(0);
        int count = 1;

        for (int i = 1; i < s.length(); i++) {

            if (last == s.charAt(i)) {
                count++;
            } else {
                ss = ss + count + last;
                count = 1;
                last = s.charAt(i);
            }

        }

        ss = ss + count + last;
        return ss;
    }

    public static boolean isNumber(String s) {

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != '1' && s.charAt(i) != '2' && s.charAt(i) != '3' && s.charAt(i) != '4' && s.charAt(i) != '5' && s.charAt(i) != '6' && s.charAt(i) != '7' && s.charAt(i) != '8' && s.charAt(i) != '9' && s.charAt(i) != '0') {
                return false;
            }
        }
        return true;
    }

}
2112045

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.