123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 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<double> list = program.ReadMethod().Item1;
- List<List<double>> list2 = program.ReadMethod().Item2;
- investment.Decision(list2, list);
- }
- private (List<double>, List<List<double>>) ReadMethod()
- {
- List<double> list = new List<double>();
- List<List<double>> list2 = new List<List<double>>();
- 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<double>());
- }
- }
- 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<List<double>> CopyList2(List<List<double>> list2)
- {
- List<List<double>> list2_copy = new List<List<double>>();
- for (int i = 0; i < list2.Count; i++)
- {
- list2_copy.Add(new List<double>());
- for (int j = 0; j < list2[i].Count; j++)
- {
- list2_copy[i].Add(list2[i][j]);
- }
- }
- return list2_copy;
- }
- public void Decision(List<List<double>> list2, List<double> list)
- {
- List<List<double>> list2_copy = CopyList2(list2);
- double difference = list[1] - list[0];
- List<List<double>> list3 = new List<List<double>>();
- 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<double>() { firstCompany, secondCompany });
- }
- else
- {
- firstCompany += difference;
- secondCompany -= difference;
- list3.Add(new List<double>() { firstCompany, secondCompany });
- }
- }
- }
- List<List<InfInvest>> temporaryValue = new List<List<InfInvest>>();
- bool flag = true;
- while (flag)
- {
- temporaryValue.Add(new List<InfInvest>());
- 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>());
- InfInvest inf = new InfInvest();
- inf.maxValue = max;
- inf.countInvest = invest;
- temporaryValue[temporaryValue.Count - 1].Add(inf);
- flag = false;
- }
- }
- double amountOfInvestments;
- List<double> result = new List<double>();
- 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<double> 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();
- }
- }
- }
|