using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp10 { internal class Program { static void Main(string[] args) { Investment investment = new Investment(); Program program = new Program(); List list = program.ReadMethod().Item1; List> list2 = program.ReadMethod().Item2; investment.Decision(list2, list); } private (List, List>) ReadMethod() { List list = new List(); List> list2 = new List>(); int n = File.ReadAllLines("InitiaData.txt").Length; using (StreamReader reader = new StreamReader("InitiaData.txt")) { for (int i = 0; i < n; i++) { string text = reader.ReadLine(); string[] mas = text.Split(new char[] { ' ' }); if (i == 0) { for (int j = 0; j < mas.Length - 1; j++) { list2.Add(new List()); } } for (int j = 0; j < mas.Length; j++) { if (j == 0) { list.Add(double.Parse(mas[j])); } else { list2[j - 1].Add(double.Parse(mas[j])); } } } } return (list, list2); } } public class Investment { struct InfInvest { public double maxValue; public double countInvest; } private List> CopyList2(List> list2) { List> list2_copy = new List>(); for (int i = 0; i < list2.Count; i++) { list2_copy.Add(new List()); for (int j = 0; j < list2[i].Count; j++) { list2_copy[i].Add(list2[i][j]); } } return list2_copy; } public void Decision(List> list2, List list) { List> list2_copy = CopyList2(list2); double difference = list[1] - list[0]; List> list3 = new List>(); for (int i = 0; i < list.Count; i++) { double firstCompany = 0; double secondCompany = list[i]; for (int j = 0; j < i + 1; j++) { if (j == 0) { list3.Add(new List() { firstCompany, secondCompany }); } else { firstCompany += difference; secondCompany -= difference; list3.Add(new List() { firstCompany, secondCompany }); } } } List> temporaryValue = new List>(); bool flag = true; while (flag) { temporaryValue.Add(new List()); double max = Double.MinValue; double invest = 0; double counter = 2; int add = 3; for (int i = 0; i < list3.Count; i++) { int firstInvest = list.IndexOf(list3[i][0]); int secondInvest = list.IndexOf(list3[i][1]); double profit = list2_copy[list2_copy.Count - 2][firstInvest] + list2_copy[list2_copy.Count - 1][secondInvest]; // ошибка if (profit > max) { max = profit; invest = list3[i][0]; } if (counter == i) { InfInvest inf = new InfInvest(); inf.maxValue = max; inf.countInvest = invest; temporaryValue[temporaryValue.Count - 1].Add(inf); counter += add; add++; max = Double.MinValue; } } list2_copy.RemoveAt(list2_copy.Count - 1); list2_copy[list2_copy.Count - 1][0] = 0; for (int i = 1; i < list2_copy[list2_copy.Count - 1].Count; i++) { list2_copy[list2_copy.Count - 1][i] = temporaryValue[temporaryValue.Count - 1][i - 1].maxValue; } if (list2_copy.Count == 2) { int i = (int)(list3.Count - 1 - (list[list.Count - 1] / difference)); for (; i < list3.Count; i++) { int firstInvest = list.IndexOf(list3[i][0]); int secondInvest = list.IndexOf(list3[i][1]); double profit = list2_copy[list2_copy.Count - 2][firstInvest] + list2_copy[list2_copy.Count - 1][secondInvest]; if (profit > max) { max = profit; invest = list3[i][0]; } } temporaryValue.Add(new List()); InfInvest inf = new InfInvest(); inf.maxValue = max; inf.countInvest = invest; temporaryValue[temporaryValue.Count - 1].Add(inf); flag = false; } } double amountOfInvestments; List result = new List(); result.Add(temporaryValue[temporaryValue.Count - 1][0].countInvest); amountOfInvestments = result[result.Count - 1]; for (int i = temporaryValue.Count - 2; i >= 0; i--) { int index = list.IndexOf(list[list.Count - 1] - result[result.Count - 1]); result.Add(temporaryValue[i][index - 1].countInvest); amountOfInvestments += result[result.Count - 1]; } result.Add(list[list.Count - 1] - amountOfInvestments); double func = 0; for (int i = 0; i < result.Count; i++) { int ind = (int)(result[i] / difference); func += list2[i][ind]; } Writing(result, func); } private void Writing(List result, double func) { FileStream file1 = new FileStream("rezult.txt", FileMode.OpenOrCreate); StreamWriter writer = new StreamWriter(file1); writer.Write("Распределение инвестиций: "); for (int i = 0; i < result.Count; i++) { writer.Write(result[i] + " "); } writer.Write('\n'); writer.WriteLine("Функция: " + func); writer.Close(); } } }