Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Soulstorm50/85dc5ac15267352fb6d1f61364995ea9 to your computer and use it in GitHub Desktop.
Save Soulstorm50/85dc5ac15267352fb6d1f61364995ea9 to your computer and use it in GitHub Desktop.
C# Последовательность чисел - вывод по N-й элемент
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter some number: ");
string userText = Console.ReadLine();
bool onlyDigits = true;
int length = 30;
foreach (var item in userText)
{
if (!Char.IsDigit(item))
{
onlyDigits = false;
Console.WriteLine("\nWrong symbol: " + item + ". Only digits are allowed.\n");
break;
}
}
if (!onlyDigits)
Environment.Exit(0);
else
length = Convert.ToInt32(userText);
string[] sequence = new string[length];
for (int i = 0; i < length; i++)
{
if (i == 0)
{
sequence[0] = "1";
}
else
{
string new_item = "";
string str = sequence[i - 1];
int symbolCount = 0;
char currentSymbol = '0';
foreach (var item in str)
{
if (currentSymbol != item)
{
if (symbolCount > 0)
{
new_item = new_item + Convert.ToString(symbolCount) + currentSymbol;
symbolCount = 0;
}
currentSymbol = item;
}
symbolCount++;
}
if (symbolCount > 0)
{
new_item = new_item + Convert.ToString(symbolCount) + currentSymbol;
symbolCount = 0;
}
sequence[i] = new_item;
}
}
for (int i = 0; i < sequence.Length; i++)
{
Console.Write(sequence[i]);
if (i < sequence.Length - 1)
Console.Write(", ");
}
Console.Write("\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment