/* © MATTO MATTI 2017 http://mattomatti.com/pl/a35bi napisane przy użyciu Visual Studio Community 2015 2017-08-21 v 1.0 */ using System; namespace plcode1cs { class Program { public class pair { public T1 first { get; set; } public T2 second { get; set; } public pair(T1 first, T2 second) { this.first = first; this.second = second; } } static pair podajZakres(int dl) { pair p = new pair(0, 0); int y = 0; bool wgore = true; for (int i = 0; i < dl; i++) { y += wgore ? 1 : -1; if (y == p.second + 1) { p.second++; wgore = false; } else if (y == p.first - 1) { p.first--; wgore = true; } } return p; } static string szyfruj(string tekst) { pair p = podajZakres(tekst.Length); string wynik = ""; bool wgore = true; for (int j = p.second; j >= p.first; j--) { int y = 0, ymin = 0, ymax = 0; for (int i = 0; i < tekst.Length; i++) { if (y == j) wynik += tekst[i]; y += wgore ? 1 : -1; if (y == ymax + 1) { ymax++; wgore = false; } else if (y == ymin - 1) { ymin--; wgore = true; } } } return wynik; } static string odszyfruj(string tekst) { pair p = podajZakres(tekst.Length); char[] wynik = new char[tekst.Length]; bool wgore = true; int odczyt = 0; for (int j = p.second; j >= p.first; j--) { int y = 0, ymin = 0, ymax = 0; for (int i = 0; i < tekst.Length; i++) { if (y == j) wynik[i] = tekst[odczyt++]; y += wgore ? 1 : -1; if (y == ymax + 1) { ymax++; wgore = false; } else if (y == ymin - 1) { ymin--; wgore = true; } } } return new string(wynik); } static void Main(string[] args) { Console.WriteLine("Podaj tekst do zaszyfrowania:"); string tekst = Console.ReadLine(); string szyfrogram = szyfruj(tekst); Console.Write("Podaj tekst do zaszyfrowania:\n" + szyfrogram); string tekstjawny = odszyfruj(szyfrogram); Console.Write("Tekst jawny to:\n" + tekstjawny); Console.ReadKey(); } } }