Пример #1
0
        static private cmpr compare(ref triplet a, ref triplet b)
        {
            cmpr Comparison = new cmpr();

            Comparison.Init();

            if (a.zed > b.zed)
            {
                Comparison.a++;
            }
            else if (a.zed < b.zed)
            {
                Comparison.b++;
            }

            if (a.one > b.one)
            {
                Comparison.a++;
            }
            else if (a.one < b.one)
            {
                Comparison.b++;
            }

            if (a.two > b.two)
            {
                Comparison.a++;
            }
            else if (a.two < b.two)
            {
                Comparison.b++;
            }

            return(Comparison);
        }
 private bool isValidTriplet(triplet tri)
 {
     if ((tri.a * tri.a) + (tri.b * tri.b) == (tri.c * tri.c))
     {
         return(true);
     }
     return(false);
 }
Пример #3
0
        public static IList <IList <int> > performThreeSum(int[] input)
        {
            IList <IList <int> >   result    = new List <IList <int> >();
            Dictionary <pair, int> twoSum    = new Dictionary <pair, int>();
            HashSet <triplet>      three_sum = new HashSet <triplet>();

            //Console.WriteLine("Pairs");
            for (int i = 0; i < input.Length; i++)
            {
                for (int j = i + 1; j < input.Length; j++)
                {
                    pair pair = new pair(input[i], input[j], i, j);

                    if (!twoSum.ContainsKey(pair))
                    {
                        twoSum.Add(pair, input[i] + input[j]);
                        //Console.WriteLine(pair.ToString());
                    }
                }
            }
            //Console.WriteLine("Triplets");

            //https://stackoverflow.com/questions/12177596/get-key-by-value-in-hash-table-c-sharp#answer-12177642
            int index = 0;

            foreach (int i in input)
            {
                if (twoSum.ContainsValue(-i))
                {
                    foreach (pair pair in twoSum.Keys.Where(v => twoSum[v] == -i))
                    {
                        if (pair.getIndexA() == index || pair.getIndexB() == index)
                        {
                            //Console.WriteLine("indexA: " + pair.getIndexA() + " .indexB: " + pair.getIndexB() + " .index: " + index);
                            continue;
                        }
                        triplet triplet = new triplet(pair.getX1(), pair.getX2(), i);
                        if (!three_sum.Contains(triplet))
                        {
                            three_sum.Add(triplet);
                            //Console.WriteLine(i + ": " + triplet.ToString());
                            result.Add(new List <int>()
                            {
                                triplet.x1, triplet.x2, triplet.x3
                            });
                        }
                    }
                }
                index++;
            }

            return(result);
        }
Пример #4
0
        public static void Main(String[] args)
        {
            bool a_valid = false;
            bool b_valid = false;

            string[] tokens_a0 = Console.ReadLine().Split(' ');
            string[] tokens_b0 = Console.ReadLine().Split(' ');
            triplet  a         = new triplet();

            a.Init();
            triplet b = new triplet();

            b.Init();
            if (!String.IsNullOrEmpty(tokens_a0 [0]) && !String.IsNullOrEmpty(tokens_a0 [1]) && !String.IsNullOrEmpty(tokens_a0 [2]))
            {
                a.zed   = Convert.ToInt32(tokens_a0 [0]);
                a.one   = Convert.ToInt32(tokens_a0 [1]);
                a.two   = Convert.ToInt32(tokens_a0 [2]);
                a_valid = true;
            }
            else
            {
                Console.WriteLine("Error reading triplet A");
            }

            if (!String.IsNullOrEmpty(tokens_a0 [0]) && !String.IsNullOrEmpty(tokens_a0 [1]) && !String.IsNullOrEmpty(tokens_a0 [2]))
            {
                b.zed   = Convert.ToInt32(tokens_b0 [0]);
                b.one   = Convert.ToInt32(tokens_b0 [1]);
                b.two   = Convert.ToInt32(tokens_b0 [2]);
                b_valid = true;
            }
            else
            {
                Console.WriteLine("Error reading triplet B");
            }

            if (a_valid && b_valid)
            {
                cmpr result = compare(ref a, ref b);
                Console.WriteLine(result.a.ToString() + " " + result.b.ToString());
            }
        }
        private triplet findTheTriplet(int sum)
        {
            //a^2 + b^2 = c^2
            //a + b +c = sum
            var tri = new triplet();

            for (tri.c = (sum / 2); tri.c > (sum / 3); tri.c--)
            {
                for (tri.a = 1; tri.a <= ((sum - tri.c) / 2); tri.a++)
                {
                    tri.b = sum - tri.a - tri.c;
                    if (isValidTriplet(tri))
                        return tri;
                }
            }

            tri.a = 0;
            tri.b = 0;
            tri.c = 0;

            return tri;
        }
        private triplet findTheTriplet(int sum)
        {
            //a^2 + b^2 = c^2
            //a + b +c = sum
            var tri = new triplet();

            for (tri.c = (sum / 2); tri.c > (sum / 3); tri.c--)
            {
                for (tri.a = 1; tri.a <= ((sum - tri.c) / 2); tri.a++)
                {
                    tri.b = sum - tri.a - tri.c;
                    if (isValidTriplet(tri))
                    {
                        return(tri);
                    }
                }
            }

            tri.a = 0;
            tri.b = 0;
            tri.c = 0;

            return(tri);
        }
 private bool isValidTriplet(triplet tri)
 {
     if ((tri.a * tri.a) + (tri.b * tri.b) == (tri.c * tri.c))
         return true;
     return false;
 }
        public int calc(int sum)
        {
            triplet tp = findTheTriplet(sum);

            return(tp.a * tp.b * tp.c);
        }