C# :: Aufgabe #12
8 Lösungen
Zahlenfolge berechnen und fortsetzen
Anfänger - C#
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
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:
Hier ist meine Lösung:
Startfolge und Anzahl der Folgen kann einfach geändert werden.
C#-Code
Startfolge und Anzahl der Folgen kann einfach geändert werden.
using System;
using System.Collections.Generic;
using System.Linq;
namespace UE12
{
class Program
{
private static void Main()
{
const string first = "112";
const int sequences = 15;
List<string> rows = new List<string> { first };
Console.WriteLine("Die Startfolge lautet: {0}", first);
for (int i = 0; i < sequences; i++)
{
string s = CalculateRow(rows[i]);
rows.Add(s);
}
Console.WriteLine("Die {0}. Folge lautet: {1}", sequences, rows[sequences-1]);
Console.ReadKey();
}
/// <summary>
/// Berechnet eine neue Folge aus der übergebenen.
/// </summary>
/// <param name="row">Ausgangsfolge</param>
/// <returns>neu berechnete Folge</returns>
private static string CalculateRow(string row)
{
string newRow = string.Empty;
//Folge in integer Liste zerlegen
IEnumerable<int> numbers = row.ToCharArray().Select(x => int.Parse(x.ToString()));
int last = numbers.First();
int count = 0;
//Jede Zahl wird mit vorheriger verglichen.
foreach (var number in numbers)
{
if (number == last)
{
count++;
}
else
{
//bei Unterschied werden Werte zur neuen Folge hinzugefügt
newRow += count.ToString() + last.ToString();
last = number;
count = 1;
}
}
newRow += count.ToString() + last.ToString();
return newRow;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Zahlenfolge
{
class Program
{
static string input;
static void Main(string[] args)
{
Console.WriteLine("Geben Sie die Anfangszahl ein:");
input = Console.ReadLine();
int zahl;
if (!int.TryParse(input, out zahl))
Console.WriteLine("Sie haben keine gültige Ganzzahl eingegeben");
else
{
for (int i = 0; i < 15; i++)
{
Berechnen(ref input);
Console.WriteLine(input);
}
}
Console.ReadKey();
}
private static void Berechnen(ref string zahl)
{
string tempZahl = zahl;
string temp = string.Empty;
int anzahl = 1;
for (int i = 0; i < tempZahl.Length; i++)
{
if (i < tempZahl.Length - 1)
{
if (tempZahl[i].ToString() == tempZahl[i + 1].ToString())
{
anzahl++;
continue;
}
}
temp += anzahl + tempZahl[i].ToString();
anzahl = 1;
}
zahl = temp;
}
}
}
using System;
using System.Text;
namespace trainYourProgrammer
{
class MainClass
{
public static string NextString(string text)
{
int length;
StringBuilder result = new StringBuilder ();
while ((length = text.Length) > 0)
{
char letter = text [0];
int lengthNew = (text = text.TrimStart(letter)).Length;
result.Append ((length - lengthNew).ToString() + letter);
}
return result.ToString ();
}
public static void Main (string[] args)
{
string text = "112";
for (int i = 1; i <= 15; i++)
{
Console.WriteLine ("{0:00}: {1}", i, text);
text = NextString (text);
}
}
}
}
class Program
{
static void Main(string[] args)
{
Zahlenfolge zf = new Zahlenfolge("112");
Console.WriteLine("01. Folge :" + zf.ToString(""));
for (int i = 2; i < 15; i++)
{
zf.next();
Console.WriteLine(i.ToString("00") + ". Folge :" + zf.ToString(""));
}
Console.ReadKey();
}
}
class Zahlenfolge
{
private string folge;
public Zahlenfolge(string folge)
{
this.folge = folge;
}
public string ToString(string seperator)
{
return String.Join(seperator, folge);
}
public void next()
{
StringBuilder sb = new StringBuilder();
char s = ' ';
int count = 0;
foreach (char c in folge)
{
if (c != s)
{
if (count != 0) sb.Append(count).Append(s);
count = 1;
s = c;
}
else
{
count++;
}
}
if (count != 0) sb.Append(count).Append(s);
folge = sb.ToString();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
public static string BerechneIteration(string s)
{
int anzahl = 1;
string returnString = "";
for (int i = 0; i < s.Length; i++)
{
if (i < (s.Length - 1))
{
if (s[i] == s[i + 1])
{
anzahl++;
continue;
}
else
{
returnString += ("" + anzahl + s[i]);
anzahl = 1;
continue;
}
}
else
{
returnString += ("" + anzahl + s[i]);
}
}
return returnString;
}
static void Main(string[] args)
{
Console.WriteLine("Geben sie eine ganze Zahl ein: ");
string eingabe = Console.ReadLine();
long number;
while (!long.TryParse(eingabe, out number))
{
Console.Clear();
Console.WriteLine("Die Eingabe ist ungültig. Versuche erneut: ");
eingabe = Console.ReadLine();
}
string ausgabe = eingabe;
for (int i = 0; i < 15; i++)
{
ausgabe = BerechneIteration(ausgabe);
Console.WriteLine((i+1) + ". Iteration: " + ausgabe);
}
Console.ReadLine();
}
}
}
using System;
using System.Text;
namespace Exercise_12
{
internal static class Program
{
private static string NextString(string text)
{
int length;
var result = new StringBuilder();
while ((length = text.Length) > 0)
{
var letter = text[0];
var lengthNew = (text = text.TrimStart(letter)).Length;
result.Append((length - lengthNew).ToString() + letter);
}
return result.ToString();
}
public static void Main()
{
var text = "112";
for (var i = 1; i <= 15; i++)
{
Console.WriteLine("{0:00}: {1}", i, text);
text = NextString(text);
}
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TrainYourProgrammer12
{
class Program
{
static void Main(string[] args)
{
int abbruch = 0;
int anzahl = 1;
String folge = "112";
String neueFolge = "";
while (abbruch < 15)
{
for (int i = 0; i < folge.Length-1; i++)
{
int j = i + 1;
if (folge[i] == folge[j])
{
anzahl += 1;
}
else
{
neueFolge += anzahl + "" + folge[i];
anzahl = 1;
}
if(i == folge.Length-2)
{
neueFolge += anzahl + "" + folge[j];
}
}
folge = neueFolge;
neueFolge = "";
abbruch++;
}
Console.WriteLine("Folge Nummer " + abbruch + " ist: " + folge);
Console.ReadLine();
}
}
}
static void Main(string[] args)
{
Console.WriteLine("Geben Sie die Anfangszahl ein:");
while (true)
{
input = Console.ReadLine();
int zahl;
if (!int.TryParse(input, out zahl))
{
Console.WriteLine("Sie haben keine gültige Ganzzahl eingegeben");
Console.WriteLine("Gib eine neue Zahl ein: ");
}
else
{
for (int i = 1; i < 15; i++)
{
Berechnen(ref input);
Console.WriteLine(input);
}
break;
}
}
Console.ReadKey();
}
private static void Berechnen(ref string zahl)
{
string tempZahl = zahl;
string temp = string.Empty;
int anzahl = 1;
for (int i = 0; i < tempZahl.Length; i++)
{
if (i < tempZahl.Length - 1)
{
if (tempZahl[i].ToString() == tempZahl[i + 1].ToString())
{
anzahl++;
continue;
}
}
temp += anzahl + tempZahl[i].ToString();
anzahl = 1;
}
zahl = temp;
}
