Program.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ConsoleApp10
  8. {
  9. internal class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Investment investment = new Investment();
  14. Program program = new Program();
  15. List<double> list = program.ReadMethod().Item1;
  16. List<List<double>> list2 = program.ReadMethod().Item2;
  17. investment.Decision(list2, list);
  18. }
  19. private (List<double>, List<List<double>>) ReadMethod()
  20. {
  21. List<double> list = new List<double>();
  22. List<List<double>> list2 = new List<List<double>>();
  23. int n = File.ReadAllLines("InitiaData.txt").Length;
  24. using (StreamReader reader = new StreamReader("InitiaData.txt"))
  25. {
  26. for (int i = 0; i < n; i++)
  27. {
  28. string text = reader.ReadLine();
  29. string[] mas = text.Split(new char[] { ' ' });
  30. if (i == 0)
  31. {
  32. for (int j = 0; j < mas.Length - 1; j++)
  33. {
  34. list2.Add(new List<double>());
  35. }
  36. }
  37. for (int j = 0; j < mas.Length; j++)
  38. {
  39. if (j == 0)
  40. {
  41. list.Add(double.Parse(mas[j]));
  42. }
  43. else
  44. {
  45. list2[j - 1].Add(double.Parse(mas[j]));
  46. }
  47. }
  48. }
  49. }
  50. return (list, list2);
  51. }
  52. }
  53. public class Investment
  54. {
  55. struct InfInvest
  56. {
  57. public double maxValue;
  58. public double countInvest;
  59. }
  60. private List<List<double>> CopyList2(List<List<double>> list2)
  61. {
  62. List<List<double>> list2_copy = new List<List<double>>();
  63. for (int i = 0; i < list2.Count; i++)
  64. {
  65. list2_copy.Add(new List<double>());
  66. for (int j = 0; j < list2[i].Count; j++)
  67. {
  68. list2_copy[i].Add(list2[i][j]);
  69. }
  70. }
  71. return list2_copy;
  72. }
  73. public void Decision(List<List<double>> list2, List<double> list)
  74. {
  75. List<List<double>> list2_copy = CopyList2(list2);
  76. double difference = list[1] - list[0];
  77. List<List<double>> list3 = new List<List<double>>();
  78. for (int i = 0; i < list.Count; i++)
  79. {
  80. double firstCompany = 0;
  81. double secondCompany = list[i];
  82. for (int j = 0; j < i + 1; j++)
  83. {
  84. if (j == 0)
  85. {
  86. list3.Add(new List<double>() { firstCompany, secondCompany });
  87. }
  88. else
  89. {
  90. firstCompany += difference;
  91. secondCompany -= difference;
  92. list3.Add(new List<double>() { firstCompany, secondCompany });
  93. }
  94. }
  95. }
  96. List<List<InfInvest>> temporaryValue = new List<List<InfInvest>>();
  97. bool flag = true;
  98. while (flag)
  99. {
  100. temporaryValue.Add(new List<InfInvest>());
  101. double max = Double.MinValue;
  102. double invest = 0;
  103. double counter = 2;
  104. int add = 3;
  105. for (int i = 0; i < list3.Count; i++)
  106. {
  107. int firstInvest = list.IndexOf(list3[i][0]);
  108. int secondInvest = list.IndexOf(list3[i][1]);
  109. double profit = list2_copy[list2_copy.Count - 2][firstInvest] + list2_copy[list2_copy.Count - 1][secondInvest]; // ошибка
  110. if (profit > max)
  111. {
  112. max = profit;
  113. invest = list3[i][0];
  114. }
  115. if (counter == i)
  116. {
  117. InfInvest inf = new InfInvest();
  118. inf.maxValue = max;
  119. inf.countInvest = invest;
  120. temporaryValue[temporaryValue.Count - 1].Add(inf);
  121. counter += add;
  122. add++;
  123. max = Double.MinValue;
  124. }
  125. }
  126. list2_copy.RemoveAt(list2_copy.Count - 1);
  127. list2_copy[list2_copy.Count - 1][0] = 0;
  128. for (int i = 1; i < list2_copy[list2_copy.Count - 1].Count; i++)
  129. {
  130. list2_copy[list2_copy.Count - 1][i] = temporaryValue[temporaryValue.Count - 1][i - 1].maxValue;
  131. }
  132. if (list2_copy.Count == 2)
  133. {
  134. int i = (int)(list3.Count - 1 - (list[list.Count - 1] / difference));
  135. for (; i < list3.Count; i++)
  136. {
  137. int firstInvest = list.IndexOf(list3[i][0]);
  138. int secondInvest = list.IndexOf(list3[i][1]);
  139. double profit = list2_copy[list2_copy.Count - 2][firstInvest] + list2_copy[list2_copy.Count - 1][secondInvest];
  140. if (profit > max)
  141. {
  142. max = profit;
  143. invest = list3[i][0];
  144. }
  145. }
  146. temporaryValue.Add(new List<InfInvest>());
  147. InfInvest inf = new InfInvest();
  148. inf.maxValue = max;
  149. inf.countInvest = invest;
  150. temporaryValue[temporaryValue.Count - 1].Add(inf);
  151. flag = false;
  152. }
  153. }
  154. double amountOfInvestments;
  155. List<double> result = new List<double>();
  156. result.Add(temporaryValue[temporaryValue.Count - 1][0].countInvest);
  157. amountOfInvestments = result[result.Count - 1];
  158. for (int i = temporaryValue.Count - 2; i >= 0; i--)
  159. {
  160. int index = list.IndexOf(list[list.Count - 1] - result[result.Count - 1]);
  161. result.Add(temporaryValue[i][index - 1].countInvest);
  162. amountOfInvestments += result[result.Count - 1];
  163. }
  164. result.Add(list[list.Count - 1] - amountOfInvestments);
  165. double func = 0;
  166. for (int i = 0; i < result.Count; i++)
  167. {
  168. int ind = (int)(result[i] / difference);
  169. func += list2[i][ind];
  170. }
  171. Writing(result, func);
  172. }
  173. private void Writing(List<double> result, double func)
  174. {
  175. FileStream file1 = new FileStream("rezult.txt", FileMode.OpenOrCreate);
  176. StreamWriter writer = new StreamWriter(file1);
  177. writer.Write("Распределение инвестиций: ");
  178. for (int i = 0; i < result.Count; i++)
  179. {
  180. writer.Write(result[i] + " ");
  181. }
  182. writer.Write('\n');
  183. writer.WriteLine("Функция: " + func);
  184. writer.Close();
  185. }
  186. }
  187. }