Fenrir112

4 %u0437%u0430%u0434%u0430%u0447%u0438 C# %u0434%u043B%u044F %u041A%u043E%u0441%u0442%u0438

Dec 1st, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Count()
  6.     {
  7.         Console.WriteLine("Введите строку для подсчета в ней слов и букв");
  8.         int ch = 0, wd = 1;
  9.         string st = Console.ReadLine();
  10.         while (st != "")
  11.         {
  12.             if (st[0] == ' ')
  13.             {
  14.                 wd++;
  15.             }
  16.             else
  17.             {
  18.                 ch++;
  19.             }
  20.             st = st.Remove(0,1);
  21.         }
  22.         Console.WriteLine("Букв: {0}\nСлов: {1}",ch, wd);
  23.     }
  24.  
  25.     static int fact(int i)
  26.     {
  27.         if (i > 1)
  28.             return i * fact(i - 1);
  29.         else
  30.             return 1;
  31.  
  32.     }
  33.  
  34.     static int fib(int i)
  35.     {
  36.         if (i > 2)
  37.             return fib(i - 1) + fib(i - 2);
  38.         else
  39.             return 1;
  40.     }
  41.  
  42.     static void game (int x, int y)
  43.     {
  44.         Console.WriteLine("\nВаши координаты (x,y) - ({0} ; {1})", x, y);
  45.         Console.WriteLine("Используйте клавиши W A S D для перемещения, 0 для выхода");
  46.         char dir = ' ';
  47.         Console.Write("({0};{1}) - ", x, y);
  48.         while (dir != '0')
  49.         {
  50.             dir = (char)Console.Read();
  51.             switch (dir)
  52.             {
  53.                 default: Console.WriteLine("Попробуйте еще раз..."); Console.Write("({0};{1}) - ", x, y); continue;
  54.                 case'\r':   ; continue;
  55.                 case'\n':   ; continue;
  56.                 case 'w': y++; Console.Write("({0};{1}) - ", x, y); continue;
  57.                 case 'a': x--; Console.Write("({0};{1}) - ", x, y); continue;
  58.                 case 's': y--; Console.Write("({0};{1}) - ", x, y); continue;
  59.                 case 'd': x++; Console.Write("({0};{1}) - ", x, y); continue;
  60.                 case '0':    ; continue;
  61.                 //Console.WriteLine("Попробуйте еще раз..."); break;
  62.             }
  63.             break;
  64.         }
  65.         Console.WriteLine("Вы закончили игру в координатах ({0};{1})", x, y);
  66.     }
  67.  
  68.     static bool IsSimple(int x)
  69.     {
  70.         for (int i = 2; i < Math.Sqrt(x); i++)
  71.         {
  72.             if (x % i == 0)
  73.             {
  74.                 return false;
  75.             }
  76.         }
  77.         return true;
  78.     }
  79.  
  80.     static void Simple(int x)
  81.     {
  82.         Console.WriteLine("\nРаспределение простых чисел до {0}:", x);
  83.         for (int i = 1; i <= x; i++)
  84.         {
  85.             if (IsSimple(i))
  86.             {
  87.                 Console.Write('@');
  88.             }
  89.             else
  90.             {
  91.                 Console.Write('?');
  92.             }
  93.         }
  94.         Console.WriteLine();
  95.     }
  96.  
  97.     static void Main()
  98.     {
  99.         //счетчик букв
  100.         Count();
  101.         Console.WriteLine();
  102.  
  103.         //факториал от 5
  104.         int f1 = fact(5);
  105.         Console.WriteLine("Факториал пяти - {0}",f1);
  106.         Console.WriteLine();
  107.  
  108.         //пятый член фиббоначи
  109.         int f2 = fib(5);
  110.         Console.WriteLine("Пятый член фиббоначи - {0}",f2);
  111.  
  112.         // странная игра в координатах
  113.         game(10, 10);
  114.  
  115.         // простые числа
  116.         Simple(50);
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment