Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections;
  7.  
  8. namespace rep_c_sharp
  9. {
  10.     class Program
  11.     {
  12.         static List<int[]> InsertR()
  13.         {
  14.             int countPairs = 0;
  15.             List<int[]> R = new List<int[]>();
  16.             Console.Write("Введите количесвто пар: ");
  17.             countPairs = int.Parse(Console.ReadLine());
  18.             for (int i = 0; i < countPairs; i++)
  19.             {
  20.                 int[] pair = new int[2];
  21.                 Console.WriteLine("---------------------------\nВведите " + i + " пару\n---------------------------");
  22.                 for (int j = 0; j < pair.Length; j++)
  23.                 {
  24.                     Console.WriteLine("Введите " + j + " значение");
  25.                     pair[j] = int.Parse(Console.ReadLine());
  26.                 }
  27.  
  28.                 R.Add(pair);
  29.             }
  30.             return R;
  31.         }
  32.  
  33.         static void ShowPairs(List<int[]> R)
  34.         {
  35.             Console.WriteLine("\nВывод R");
  36.             foreach (int[] pair in R)
  37.             {
  38.                 for (int i = 0; i < pair.Length; i++)
  39.                 {
  40.                     if (i == 0)
  41.                         Console.Write("{" + pair[i] + ", ");
  42.                     else
  43.                         Console.Write(pair[i] + "}");
  44.                 }
  45.                 Console.WriteLine();
  46.             }
  47.         }
  48.  
  49.         static int[,] ConvertToMatrix(List<int[]> R, List<int> X)
  50.         {
  51.             int len = 0;
  52.             len = X.Count();
  53.  
  54.             int[,] matrix = new int[len, len];
  55.  
  56.             for (int i = 0; i < len; i++)
  57.             {
  58.                 for (int j = 0; j < len; j++)
  59.                 {
  60.                     matrix[i, j] = 0;
  61.                 }
  62.             }
  63.  
  64.             foreach (int[] pair in R)
  65.             {
  66.                 matrix[X.IndexOf(pair[0]), X.IndexOf(pair[1])] = 1;
  67.             }
  68.  
  69.             return matrix;
  70.         }
  71.  
  72.         static void ShowMatrix(int[,] matrix, List<int> X)
  73.         {
  74.             for (int i = 0; i < matrix.GetLength(0); i++)
  75.             {
  76.                
  77.                 for (int j = 0; j < matrix.GetLength(1); j++)
  78.                 {
  79.                  
  80.                     Console.Write(matrix[i, j] + "\t");
  81.                 }
  82.                 Console.WriteLine();
  83.             }
  84.         }
  85.  
  86.         static void Main(string[] args)
  87.         {
  88.             List<int> X = new List<int>();
  89.  
  90.  
  91.             Console.Write("Введите размер 'X': ");
  92.             int lenX = int.Parse(Console.ReadLine());
  93.             for(int i = 0; i < lenX; i++)
  94.             {
  95.                 Console.Write("Введите X[" + i + "]: ");
  96.                 X.Add(int.Parse(Console.ReadLine()));
  97.             }
  98.  
  99.             Console.Write("X: ");
  100.             Console.WriteLine(String.Join(", ", X));
  101.  
  102.  
  103.  
  104.  
  105.             List<int[]> R = InsertR();
  106.             ShowPairs(R);
  107.             int[,] matrixR = ConvertToMatrix(R, X);
  108.             ShowMatrix(matrixR, X);
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement