Advertisement
awsmpshk

Практика по c# дз

Oct 8th, 2020 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.21 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Xml.Serialization;
  4.  
  5. namespace Practice_n3_and_n4
  6. {
  7.     class Practice_n3
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.OutputEncoding = Encoding.Unicode;
  12.             // Task_4();
  13.             Task_6();
  14.         }
  15.  
  16.         public static void Task_4()
  17.         {
  18.             Console.Write("Введите число а: ");
  19.             int a = Convert.ToInt32(Console.ReadLine());
  20.             Console.Write("Введите число b: ");
  21.             int b = Convert.ToInt32(Console.ReadLine());
  22.             for (int i = (a % 3 == 0 ? a : ((a % 3 == 1) ? a + 2 : a + 1)); i <= b; i += 3)
  23.             {
  24.                 Console.Write((i % 2 == 0) ? i + " " : "");
  25.             }
  26.         }
  27.  
  28.         public static void Task_6()
  29.         {
  30.             Console.Write("Введите число а: ");
  31.             double a = Convert.ToDouble(Console.ReadLine());
  32.             Console.Write("Введите число b: ");
  33.             double b = Convert.ToDouble(Console.ReadLine());
  34.             Console.Write("Введите число h: ");
  35.             double h = Convert.ToDouble(Console.ReadLine());
  36.             double eps = 1E-12;
  37.             double y;
  38.             Console.WriteLine("Таблица значений функции Func(x, y):");
  39.             for (double x = a; Math.Abs(x - b) >= eps; x += h)
  40.             {
  41.                 Func(x, out y);
  42.                 Console.WriteLine("{0:f3}\t{1:f3}", x, y);
  43.             }
  44.         }
  45.  
  46.         public static void Func(double x, out double y)
  47.         {
  48.             double eps = 1E-12;
  49.             y = 0;
  50.             if (x + 2 <= 1 - eps)
  51.             {
  52.                 y = Power(x, 2);
  53.             }
  54.             else if (x + 2 > 1 + eps && x + 2 < 10 - eps)
  55.             {
  56.                 y = 1 / (x + 2);
  57.             }
  58.             else if (x + 2 >= 10 + eps)
  59.             {
  60.                 y = x + 2;
  61.             }
  62.         }
  63.  
  64.         public static double Power(double num, byte n)
  65.         {
  66.             double res = 1;
  67.             while (num > 0)
  68.             {
  69.                 if (n % 2 == 1) res *= num;
  70.                 num *= num;
  71.                 n /= 2;
  72.             }
  73.             return res;
  74.         }
  75.     }
  76.     class Practice_n4
  77.     {
  78.         static void Main(string[] args)
  79.         {
  80.             Console.OutputEncoding = Encoding.Unicode;
  81.             // Task_6();
  82.             Task_7();
  83.         }
  84.  
  85.         public static void Task_6()
  86.         {
  87.             Console.Write("Введите число из условия: ");
  88.             int number = Convert.ToInt32(Console.ReadLine());
  89.             Pair<int, int> res = MinAndMaxDels(number);
  90.             Console.WriteLine("Ответ: {0}", res.first + res.second);
  91.         }
  92.  
  93.         public static Pair<int, int> MinAndMaxDels(int num)
  94.         {
  95.             int min = 1, max = num;
  96.             bool flag = true;
  97.             for (int i = 2; i <= Math.Sqrt(num); ++i)
  98.             {
  99.                 if (CountDel(i) == 2 && num % i == 0)
  100.                 {
  101.                     if (flag)
  102.                     {
  103.                         min = i;
  104.                         max = i;
  105.                         flag = false;
  106.                     }
  107.                     else
  108.                     {
  109.                         max = i;
  110.                     }
  111.                 }
  112.             }
  113.             Pair<int, int> res = Pair<int, int>.MakePair(min, max);
  114.             return res;
  115.         }
  116.  
  117.         public static int CountDel(int num)
  118.         {
  119.             int cnt = 2;
  120.             for (int i = 2; i < Math.Sqrt(num); ++i)
  121.             {
  122.                 if (num % i == 0) ++cnt;
  123.             }
  124.             return cnt;
  125.         }
  126.  
  127.         public static void Task_7()
  128.         {
  129.             Console.Write("Введите число N: ");
  130.             int number = Convert.ToInt32(Console.ReadLine());
  131.             Console.Write("Введите число C: ");
  132.             int c = Convert.ToInt32(Console.ReadLine());
  133.             while (!IsDigitSumEqualsC(number, c))
  134.             {
  135.                 number++;
  136.             }
  137.             Console.WriteLine("Найденное число: {0}", number);
  138.         }
  139.  
  140.         public static bool IsDigitSumEqualsC(int num, int c)
  141.         {
  142.             int sum = 0;
  143.             while (num > 0)
  144.             {
  145.                 sum += num % 10;
  146.                 num /= 10;
  147.             }
  148.             if (sum % c == 0) return true;
  149.             return false;
  150.         }
  151.     }
  152.  
  153.     class Pair<T, U> : Practice_n4
  154.     {
  155.         public T first;
  156.         public U second;
  157.  
  158.         public Pair(T first, U second)
  159.         {
  160.             this.first = first;
  161.             this.second = second;
  162.         }
  163.        
  164.         public Pair<T, U> CopyPair(Pair<T, U> p)
  165.         {
  166.             Pair<T, U> newPair = new Pair<T, U>(p.first, p.second);
  167.             return newPair;
  168.         }
  169.  
  170.         public static Pair<T, U> MakePair(T first, U second)
  171.         {
  172.             Pair<T, U> newPair = new Pair<T, U>(first, second);
  173.             return newPair;
  174.         }
  175.     }
  176.     class Practice_n5
  177.     {
  178.         public static void Main(string[] args)
  179.         {
  180.             Console.OutputEncoding = Encoding.Unicode;
  181.             Practice_n5.Task_4();
  182.         }
  183.  
  184.         public static double power(double num, byte n)
  185.         {
  186.             double res = 1;
  187.             while (num > 0)
  188.             {
  189.                 if (n % 2 == 1) res *= num;
  190.                 num *= num;
  191.                 n /= 2;
  192.             }
  193.             return res;
  194.         }
  195.  
  196.         public static void Task_2a()
  197.         {
  198.             Console.Write("Введите число а: ");
  199.             int a = Convert.ToInt32(Console.ReadLine());
  200.             Console.Write("Введите число b: ");
  201.             int b = Convert.ToInt32(Console.ReadLine());
  202.             Console.Write("Введите число h: ");
  203.             int h = Convert.ToInt32(Console.ReadLine());
  204.             byte number = 1;
  205.             for (int i = a; i <= b; i += h)
  206.             {
  207.                 Console.Write("{0} ", Practice_n5.power(i, number));
  208.                 number++;
  209.             }
  210.         }
  211.  
  212.         public static void Task_2b()
  213.         {
  214.             Console.Write("Введите число а: ");
  215.             int a = Convert.ToInt32(Console.ReadLine());
  216.             Console.Write("Введите число b: ");
  217.             int b = Convert.ToInt32(Console.ReadLine());
  218.             Console.Write("Введите число h: ");
  219.             int h = Convert.ToInt32(Console.ReadLine());
  220.             double sum = 0;
  221.             byte number = 1;
  222.             for (int i = a; i <= b; i += h)
  223.             {
  224.                 sum += Practice_n5.power(i, number);
  225.                 number++;
  226.             }
  227.             Console.WriteLine("{0}", sum);
  228.         }
  229.  
  230.         public static void Task_2c()
  231.         {
  232.             Console.Write("Введите число а: ");
  233.             int a = Convert.ToInt32(Console.ReadLine());
  234.             Console.Write("Введите число b: ");
  235.             int b = Convert.ToInt32(Console.ReadLine());
  236.             Console.WriteLine("Все подходящие тройки:");
  237.             for (int i = a; i <= b - 2; ++i)
  238.             {
  239.                 for (int j = i + 1; j <= b - 1; ++j)
  240.                 {
  241.                     for (int k = j + 1; k <= b; ++k)
  242.                     {
  243.                         if (Practice_n5.power(i, 2) + Practice_n5.power(j, 2) == Practice_n5.power(k, 2))
  244.                         {
  245.                             Console.WriteLine("{0} {1} {2}", i, j, k);
  246.                         }
  247.                     }
  248.                 }
  249.             }
  250.         }
  251.  
  252.         public static void Task_2d()
  253.         {
  254.             Console.Write("Введите число а: ");
  255.             int a = Convert.ToInt32(Console.ReadLine());
  256.             byte n = 1;
  257.             while (!(a >= Practice_n5.power(2, (byte) (n - 1)) && a < Practice_n5.power(2, n)))
  258.             {
  259.                 n++;
  260.             }
  261.             Console.WriteLine("Искомая степень n = {0}", n);
  262.         }
  263.  
  264.         public static void Task_3()
  265.         {
  266.             Console.Write("Введите число n: ");
  267.             int n = Convert.ToInt32(Console.ReadLine());
  268.             Console.Write("Введите число x: ");
  269.             double x = Convert.ToDouble(Console.ReadLine());
  270.             Console.WriteLine("Ответ: {0}", ChainDivision(n, x));
  271.         }
  272.  
  273.         public static double ChainDivision(int n, double x, int i = 1)
  274.         {
  275.             if (i < n)
  276.             {
  277.                 return x / (i + ChainDivision(n, x, ++i));
  278.             }
  279.             return i + x;
  280.         }
  281.  
  282.         public static void Task_4()
  283.         {
  284.             Console.Write("Введите количество строк со звездочками: ");
  285.             int n = Convert.ToInt32(Console.ReadLine());
  286.             PrintStairs(n);
  287.         }
  288.  
  289.         public static void PrintStairs(int n)
  290.         {
  291.             Practice_n5.PrinStarLine(n);
  292.             if (n > 1)
  293.             {
  294.                 PrintStairs(n - 1);
  295.             }
  296.         }
  297.  
  298.         public static void PrinStarLine(int n)
  299.         {
  300.             for (int i = 0; i < n; ++i)
  301.             {
  302.                 Console.Write("*");
  303.             }
  304.             Console.WriteLine("");
  305.         }
  306.     }
  307.     class Practice_n7
  308.     {
  309.  
  310.         /*public static void Task_4()
  311.         {
  312.             Console.Write("Введите размерность квадратной матрицы: ");
  313.             int n = Convert.ToInt32(Console.ReadLine());
  314.             int[,] mas = InitMas(n);
  315.             int[] res = new int[n];
  316.             for (int i = 0; i < mas.GetLength(0); ++i)
  317.             {
  318.                 int max = mas[0, i];
  319.                 for (int j = 1; j < mas.GetLength(1); ++j)
  320.                 {
  321.                     if (mas[j, i] > max) max = mas[j, i];
  322.                 }
  323.                 res[i] = max;
  324.             }
  325.             PrintResultArray(res);
  326.         }*/
  327.  
  328.         /*public static int[,] InitMas(int n)
  329.         {
  330.             int[,] mas = new int[n, n];
  331.             for (int i = 0; i < mas.GetLength(0); ++i)
  332.             {
  333.                 for (int j = 0; j < mas.GetLength(1); ++j)
  334.                 {
  335.                     Console.Write("mas[{0}, {1}] = ", i, j);
  336.                     mas[i, j] = Convert.ToInt32(Console.ReadLine());
  337.                 }
  338.             }
  339.             return mas;
  340.         }*/
  341.  
  342.         public static void PrintResultArray(int[] res)
  343.         {
  344.             for (int i = 0; i < res.Length; ++i)
  345.             {
  346.                 Console.Write("{0} ", res[i]);
  347.             }
  348.         }
  349.  
  350.         public static void Task_5()
  351.         {
  352.             Console.Write("Введите количество элементов в массиве: ");
  353.             int n = Convert.ToByte(Console.ReadLine());
  354.             Console.Write("Введите элемент, который нужно добавлять: ");
  355.             int x = Convert.ToInt32(Console.ReadLine());
  356.             Console.WriteLine("Введите элементы массива: ");
  357.             int[] mas = InitMas(n);
  358.             for (byte i = 1; i <= n; ++i)
  359.             {
  360.                 if (mas[i - 1] % (i + 1) == 0)
  361.                 {
  362.                     ShiftMas(mas, i, ref n);
  363.                     mas[i] = x;
  364.                     ++i;
  365.                 }
  366.             }
  367.             PrintMas(mas, n);
  368.         }
  369.  
  370.         public static int[] InitMas(int n)
  371.         {
  372.             int[] mas = new int[2 * n];
  373.             for (int i = 0; i < n; ++i)
  374.             {
  375.                 mas[i] = Convert.ToInt32(Console.ReadLine());
  376.             }
  377.             return mas;
  378.         }
  379.  
  380.         public static void ShiftMas(int[] mas, int index, ref int n)
  381.         {
  382.             for (int i = n; i >= index; --i)
  383.             {
  384.                 mas[i] = mas[i - 1];
  385.             }
  386.             ++n;
  387.         }
  388.  
  389.         public static void PrintMas(int[] mas, int n)
  390.         {
  391.             for (int i = 0; i < n; ++i)
  392.             {
  393.                 Console.Write("{0} ", mas[i]);
  394.             }
  395.         }
  396.  
  397.         public static void Task_6()
  398.         {
  399.  
  400.         }
  401.     }
  402. }
  403.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement