コード例 #1
0
ファイル: IceCreamParlor.cs プロジェクト: Gutse/hackerrank
        public TAnswer BruteForce(TSample Sample)
        {
            IceCreamParlorSample sample = Sample as IceCreamParlorSample;

            Int32[] cost  = sample.cost;
            Int32   money = sample.money;

            Int32[] result = new Int32[2];

            Int32[] idx = Enumerable.Range(0, cost.Length).ToArray();

            for (int i = 0; i < cost.Length; i++)
            {
                idx[i] = i + 1;
            }

            QS(cost, 0, cost.Length - 1, idx);
            Int32 LeftPtr  = 0;
            Int32 RightPtr = cost.Length - 1;

            while (cost[RightPtr] + cost[LeftPtr] > money)
            {
                RightPtr--;
            }

            for (int i = RightPtr; i > LeftPtr; i--)
            {
                for (int j = LeftPtr; j < i; j++)
                {
                    if (cost[i] + cost[j] == money)
                    {
                        //Console.WriteLine($"{cost[j]} {cost[i]} ");
                        //Console.WriteLine($"{Math.Min(idx[i], idx[j])} {Math.Max(idx[i], idx[j])}");
                        return(new IceCreamParlorAnswer()
                        {
                            arr = new Int32[] { Math.Min(idx[i], idx[j]), Math.Max(idx[i], idx[j]) }
                        });
                    }
                }
            }
            return(new IceCreamParlorAnswer()
            {
                arr = result
            });
        }
コード例 #2
0
ファイル: IceCreamParlor.cs プロジェクト: Gutse/hackerrank
        public override void CreateSamples(System.IO.StreamReader reader)
        {
            int t = Convert.ToInt32(reader.ReadLine());

            for (int tItr = 0; tItr < t; tItr++)
            {
                int money = Convert.ToInt32(reader.ReadLine());

                int n = Convert.ToInt32(reader.ReadLine());

                int[] cost = Array.ConvertAll(reader.ReadLine().Trim().Split(' '), costTemp => Convert.ToInt32(costTemp));

                IceCreamParlorSample sample = new IceCreamParlorSample()
                {
                    cost = cost, money = money
                };
                Samples.Add(sample);
            }
        }