2. 有趣的數列
string k = "1";
Console.Write("N=");
int t = int.Parse(Console.ReadLine());
Console.WriteLine(k);
for (int i = 0; i < t; i++)
{
string str = "";
char? last = null;
int n = 0;
for (global::System.Int32 j = 0; j < k.Length; j++)
{
var c = k[j];
if (last == null)
{
n = 1;
last = c;
}
else if(c == last)
{
n++;
}
else
{
str += $"{n}{last}";
last = c;
n = 1;
}
}
str += $"{n}{last}";
k = str;
Console.WriteLine(k);
}Last updated