• sa
    2023-06-05 0 Deschis 0 închis

    s

  • 2
    2023-05-28 0 Deschis 0 închis

    using System.Diagnostics; using System.Text;

    namespace MyApp {

    internal class Program
    {
        static void Main(string[] args)
        {
            FileInfo trace = new("Log.txt");
            trace.Create().Close();
            _ = Trace.Listeners.Add(new TextWriterTraceListener(trace.OpenWrite()));
            Trace.AutoFlush = true;
            Solution.Coding();
        }
    }
    
    public class Solution
    {
        static int[,] graph = ReadAndWrite.ReadData("Files/Graph.csv");
        public static string Coding()
        {
            string code = "";
            for (int j = 0; j < graph.GetLength(0); j++)
            {
                for (int i = 0; i < graph.GetLength(0); i++)
                {
                    if ((graph[i, 0] == GetMin()[0]) && (graph[i, 1] == GetMin()[1]))
                    {
                        code += graph[i, 0] + " ";
                        graph[i, 0] = 0;
                        graph[i, 1] = 0;
                    }
                }
            }
            code = code.Remove(code.Length - 2);
            Console.WriteLine("Код Прюфера = " + code);
            ReadAndWrite.WriteSolution(code);
            return code;
        }
    
    
        static int[] GetMin()
        {
            int min = 11;
            int last_min = 11;
            int min2 = 11;
            int last_min2 = 11;
            bool find = false;
            for (int i = 0; i < graph.GetLength(0); i++)
            {
                for (int j = 0; j < graph.GetLength(0); j++)
                {
                    if ((graph[i, 1] == graph[j, 0]) && (graph[i, 1] != 0))
                    {
                        find = true;
                        break;
                    }
                    else if ((graph[i, 1] < min) && (graph[i, 0] != 0))
                    {
                        min = graph[i, 1];
                        min2 = graph[i, 0];
                    }
                }
                if (find)
                {
                    min = last_min;
                    min2 = last_min2;
                }
                else
                {
                    last_min = min;
                    last_min2 = min2;
                }
                find = false;
            }
            int[] rezult = { min2, min };
            return rezult;
        }
    }
    
    public class ReadAndWrite
    {
        public static int[,] ReadData(string path)
        {
            int count_column = 0, count_row = 0;
            using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
            {
                while (!sr.EndOfStream)
                {
                    count_row = sr.ReadLine().Split(";").Count();
                    count_column++;
                }
                sr.Close();
            }
            int[,] graph = new int[count_column, count_row];
            using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
            {
                int i = 0;
                while (!sr.EndOfStream)
                {
                    string[] array = sr.ReadLine().Split(";");
                    for (int j = 0; j < array.Length; j++)
                    {
                        graph[i, j] = int.Parse(array[j]);
                    }
                    i++;
                }
            }
            return graph;
        }
        public static void WriteSolution(string code)
        {
            using (StreamWriter sw = new StreamWriter(File.Open("Files/Code.txt", FileMode.OpenOrCreate)))
            {
                sw.WriteLine(code);
            }
        }
    }
    

    }

  • 1
    2023-06-25 0 Deschis 0 închis

    using System.Diagnostics; using System.Text;

    namespace MyApp {

    internal class Program
    {
        static void Main(string[] args)
        {
            FileInfo trace = new("Log.txt");
            trace.Create().Close();
            _ = Trace.Listeners.Add(new TextWriterTraceListener(trace.OpenWrite()));
            Trace.AutoFlush = true;
            Solution.GetSolution();
        }
    }
    
    public class Solution
    {
        static int[,] n;
        static int[,] m;
        static int[,] data;
        static int[,] rezult;
        public static int[,] DataReader(string path_data)
        {
            int count_lines = 0, count_columns = 0;
            using (StreamReader sr = new StreamReader(path_data, Encoding.UTF8))
            {
                while (!sr.EndOfStream)
                {
                    count_lines++;
                    count_columns = sr.ReadLine().Split(";").Length;
                }
                sr.Close();
            }
            int[,] data = new int[count_lines, count_columns];
            using (StreamReader sr = new StreamReader(path_data, Encoding.UTF8))
            {
                int i = 0;
                while (!sr.EndOfStream)
                {
                    string[] array = sr.ReadLine().Split(";");
                    for (int j = 0; j < array.Length; j++)
                    {
                        data[i, j] = int.Parse(array[j]);
                    }
                    i++;
                }
            }
            return data;
        }
    
        public static void ShowData()
        {
            n = DataReader("Files/N.csv");
            if (n[0, 0] > 0)
            {
                Console.WriteLine("Вывод N:");
                for (int i = 0; i < n.GetLength(1); i++)
                {
                    Console.Write(n[0, i] + " ");
                }
            }
    
            m = DataReader("Files/M.csv");
            if (m[0, 0] > 0)
            {
                Console.WriteLine("\n\nВывод M:");
                for (int i = 0; i < m.GetLength(1); i++)
                {
                    Console.Write(m[0, i] + " ");
                }
            }
    
            data = DataReader("Files/Data.csv");
            Console.WriteLine("\n\nПоставки:");
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    Console.Write(data[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
        public static int GetMin()
        {
            int min = data[0, 0];
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    if ((data[i, j] < min) && (data[i, j] != 0))
                    {
                        min = data[i, j];
                    }
                }
            }
            return min;
        }
    
        static int GetSumm(int[,] array)
        {
            int sum = 0;
            for (int i = 0; i < array.GetLength(1); i++)
            {
                sum += array[0, i];
            }
            return sum;
        }
        static void Min()
        {
            rezult = new int[data.GetLength(0), data.GetLength(1)]; //новый массив поставок
    
            while ((GetSumm(m) > 0) && (GetSumm(n) > 0))
            {
                for (int i = 0; i < rezult.GetLength(0); i++)
                {
                    for (int j = 0; j < rezult.GetLength(1); j++)
                    {
                        if (data[i, j] == GetMin())
                        {
                            if ((n[0, i] > m[0, j]) && (n[0, i] != 0) && (m[0, j] != 0))
                            {
                                rezult[i, j] = m[0, j];
                                n[0, i] -= m[0, j];
                                m[0, j] = 0;
                            }
                            else if ((n[0, i] <= m[0, j]) && (n[0, i] != 0) && (m[0, j] != 0))
                            {
                                rezult[i, j] = n[0, i];
                                m[0, j] -= n[0, i];
                                n[0, i] = 0;
                            }
                            data[i, j] = 0;
                        }
                    }
                }
            }
        }
    
        public static void GetSolution()
        {
            ShowData();
            Min();
            Console.WriteLine("\nРезультат:");
            for (int i = 0; i < rezult.GetLength(0); i++)
            {
                for (int j = 0; j < rezult.GetLength(1); j++)
                {
                    Console.Write(rezult[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("Функция = "+Functuon());
            WriteSolution();
        }
        static int Functuon()
        {
            int fun = 0;
            data = DataReader("Files/Data.csv");
            for (int i = 0;i < data.GetLength(0); i++)
            {
                for (int j = 0;j < data.GetLength(1); j++)
                {
                    fun+= data[i,j]*rezult[i,j];
                }
            }
            return fun;
        }
    
        static void WriteSolution()
        {
            string[] rezults = new string[rezult.GetLength(0)];
            for (int i = 0;i < rezult.GetLength(0);i++)
            {
                for (int j = 0;j < rezult.GetLength(1); j++)
                {
                    rezults[i] += rezult[i, j] + ";";
                }
            }
            using (StreamWriter sr = new StreamWriter("Files/Solution.csv", true, Encoding.UTF8))
            {
                for (int i = 0; i < rezults.Length; i++)
                {
                    sr.WriteLine(rezults[i]);
                }
                sr.WriteLine(Functuon());
            }
        }
    }
    

    }