Program.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. namespace ConsoleApp4
  2. {
  3. internal class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. int lastTop = 0;
  8. int lastSheet = 0;
  9. List<int> codPrufer = new List<int>();
  10. using (StreamReader sr = new StreamReader("CodPrufera.txt"))
  11. {
  12. while (sr.EndOfStream != true)
  13. {
  14. string[] array = sr.ReadLine().Split(" ");
  15. for(int i = 0; i < array.Length; i++)
  16. {
  17. codPrufer.Add(Convert.ToInt32(array[i]));
  18. }
  19. }
  20. }
  21. Console.WriteLine("Код Прюфера: ");
  22. foreach (int i in codPrufer)
  23. {
  24. Console.Write(i+" ");
  25. }
  26. List<int> tree1 = new List<int>();
  27. List<int> tree2 = new List<int>();
  28. List<int> sheet = new List<int>();
  29. int a = codPrufer.Count + 1;
  30. for(int i = 0;i< codPrufer.Count + 2; i++)
  31. {
  32. sheet.Add(i+1);
  33. }
  34. Console.WriteLine("\nДополнительный массив: ");
  35. foreach (int i in sheet)
  36. {
  37. Console.Write(i + " ");
  38. }
  39. lastTop = codPrufer.Last();
  40. while (codPrufer.Count <= a && codPrufer.Count>0)
  41. {
  42. int minimum = 10;
  43. int minimumIndex = 0;
  44. for (int i = 0; i < sheet.Count; i++)
  45. {
  46. if (codPrufer.Contains(sheet[i]) != true)
  47. {
  48. if (minimum > sheet[i])
  49. {
  50. minimum = sheet[i];
  51. minimumIndex = i;
  52. }
  53. }
  54. }
  55. tree1.Add(codPrufer[0]);
  56. tree2.Add(minimum);
  57. codPrufer.RemoveAt(0);
  58. sheet.RemoveAt(minimumIndex);
  59. lastSheet = sheet.Last();
  60. Console.WriteLine();
  61. }
  62. tree1.Add(lastTop);
  63. tree2.Add(lastSheet);
  64. Console.WriteLine("\nРебра графа");
  65. for(int i = 0;i < tree2.Count;i++)
  66. {
  67. Console.WriteLine(tree1[i]+" - " + tree2[i]);
  68. }
  69. using (StreamWriter sw = new StreamWriter("tree.txt", false))
  70. {
  71. for (int i = 0; i < tree2.Count; i++)
  72. {
  73. sw.WriteLine(tree1[i] + " - " + tree2[i]);
  74. }
  75. }
  76. }
  77. }
  78. }