Софтуерно Инженерство
Loading...
+ Нов въпрос
Jaihertz avatar Jaihertz 2 Точки

EX09 - Kamino Factory

Някой добър човечец дали знае какъв е инпута на тестчета 6 и 9? Нищо не се троши, всичките ми подмолни проби вадят коректни резултати, но тестовете са си Х-ове :)

Пускам и код...аа мазаляка, който можех и по-човешки да се напиша, с методи и прочее, но просто искам да ми се маха от главата тая задача :D

using System;
using System.Linq;

namespace EX09_Kamino_Factory
{
    class BestSample
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            int arrSize = 20;
            string input = null;
            int sampleCounter = 0;
            string[] sampleSaver = new string[arrSize];

            //Sorting the inputs:
            while (input != "Clone them!")
            {
                input = Console.ReadLine();
                sampleSaver[sampleCounter] = input;
                sampleCounter++;
            }

            //Creating numbers array
            int[,] onlyNumbers = new int[sampleCounter - 1, N];
            for (int i = 0; i < sampleCounter - 1; i++)
            {
                input = sampleSaver[i].Replace("!", string.Empty);
                int[] arr = input.ToArray().Select(x => int.Parse(x.ToString())).ToArray();

                for (int j = 0; j < N; j++)
                {
                    onlyNumbers[i, j] = arr[j];
                }
            }

            //Parsing and calculating
            int longestTemp = 0;
            int longest = 0;

            int highestSum = 0;

            int earliestIndex = int.MaxValue;
            int index = 0;

            int sample = 0;

            bool isFirst = true;
            bool sumNeeded = false;

            string sampleToPrint = null;
            string printed = null;
            for (int i = 0; i < sampleCounter - 1; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (onlyNumbers[i, j] == 1)
                    {
                        longestTemp++;

                        if (longestTemp == 2 && isFirst)
                        {
                            index = j - 1;
                            isFirst = false;
                        }

                        if (longestTemp > longest)
                        {
                            longest = longestTemp;
                            for (int k = 0; k < N; k++)
                            {
                                if (k != N - 1)
                                {
                                    sampleToPrint += onlyNumbers[i, k].ToString() + " ";
                                }
                                else
                                {
                                    sampleToPrint += onlyNumbers[i, k].ToString();
                                }
                            }
                            printed = sampleToPrint;
                            sampleToPrint = null;
                            sample = i + 1;
                            sumNeeded = false;
                            highestSum = 0;
                            earliestIndex = index;
                        }
                        else if (longestTemp == longest)
                        {
                            if (index < earliestIndex && longestTemp > 1)
                            {
                                earliestIndex = index;
                                for (int k = 0; k < N; k++)
                                {
                                    if (k != N - 1)
                                    {
                                        sampleToPrint += onlyNumbers[i, k].ToString() + " ";
                                    }
                                    else
                                    {
                                        sampleToPrint += onlyNumbers[i, k].ToString();
                                    }
                                }
                                printed = sampleToPrint;
                                sampleToPrint = null;
                                sample = i + 1;
                            }
                            else if (index == earliestIndex)
                            {
                                int lastPeakSum = 0;

                                for (int k = 0; k < N; k++)
                                {
                                    lastPeakSum += onlyNumbers[sample - 1, k];
                                }

                                int currentPeakSum = 0;
                                for (int k = 0; k < N; k++)
                                {
                                    currentPeakSum += onlyNumbers[i, k];
                                }

                                if (currentPeakSum > lastPeakSum)
                                {
                                    highestSum = currentPeakSum;

                                    for (int k = 0; k < N; k++)
                                    {
                                        if (k != N - 1)
                                        {
                                            sampleToPrint += onlyNumbers[i, k].ToString() + " ";
                                        }
                                        else
                                        {
                                            sampleToPrint += onlyNumbers[i, k].ToString();
                                        }
                                    }
                                    printed = sampleToPrint;
                                    sampleToPrint = null;
                                    sample = i + 1;
                                }
                                else
                                {
                                    highestSum = lastPeakSum;
                                }
                                sumNeeded = true;
                            }
                        }
                    }
                    else
                    {
                        longestTemp = 0;
                    }
                }
                longestTemp = 0;
                isFirst = true;
            }

            //Printing
            if (printed == null)
            {
                for (int k = 0; k < N; k++)
                {
                    if (k != N - 1)
                    {
                        sampleToPrint += onlyNumbers[0, k].ToString() + " ";
                    }
                    else
                    {
                        sampleToPrint += onlyNumbers[0, k].ToString();
                    }
                }
                printed = sampleToPrint;
                sample = 1;
            }

            if (sumNeeded)
            {
                Console.WriteLine($"Best DNA sample {sample} with sum: {highestSum}.");
                Console.WriteLine(printed);
            }
            else
            {
                Console.WriteLine($"Best DNA sample {sample} with sum: {longest}.");
                Console.WriteLine(printed);
            }
        }
    }
}

 

Тагове:
0
Programming Fundamentals
emo.999 avatar emo.999 398 Точки

Здравей, 

Струва ми се, че си я усложнил и просто на никой не му се бърка в кода, защото е труден за четене.

Има няколко теми из форума за конкретната задача - тук, тук и мое решение(на Java) тук

И още едно решение на колега тук

Надявам се да помогнат. Peace✌

1
12/02/2019 14:27:54