コード例 #1
0
        //formula that, given an array of integers, find the maximum product between two numbers from the array, that is a multiple of 3


        public static BigBrothers getBestBrothers2(int[] list)
        {
            BigBrothers bigBrothers = new BigBrothers(int.MinValue);

            //safe = not null and more than 1 elements
            if (list.isSafe())
            {
                for (int a = 0; a < list.Length; a++)
                {
                    var value = list[a];
                    if ((value > bigBrothers.getB2()) && value.isDivisibleBy3())
                    {
                        if (bigBrothers.getB2() > bigBrothers.getB1())
                        {
                            bigBrothers.setB1(bigBrothers.getB2());
                        }

                        bigBrothers.setB2(value);
                    }
                    else if ((value > bigBrothers.getB1()) && (bigBrothers.getB2() != value))
                    {
                        bigBrothers.setB1(value);
                    }
                }
            }

            return(bigBrothers);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            int[] list = loadArray();

            BigBrothers bestBrothers = getBestBrothers2(list);

            if (bestBrothers == null)
            {
                Console.WriteLine("There were no valid combination!");
            }
            else
            {
                Console.WriteLine("bestBrothers.A = " + bestBrothers.getB1());
                Console.WriteLine("bestBrothers.B = " + bestBrothers.getB2());
                Console.WriteLine("bestBrothers.Multi = " + bestBrothers.getProduct());
            }

            Console.ReadLine();
        }