Scale Balancing

版权声明:本文为博主原创文章,采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权。欢迎转载,但请注明作者姓名和文章出处。 https://blog.csdn.net/njit_77/article/details/79748334

Challenge

Using the C# language, have the function  ScaleBalancing(strArr) read  strArr which will contain two elements, the first being the two positive integer weights on a balance scale (left and right sides) and the second element being a list of available weights as positive integers. Your goal is to determine if you can balance the scale by using the least amount of weights from the list, but using at most only 2 weights. For example: if  strArr is ["[5, 9]", "[1, 2, 6, 7]"] then this means there is a balance scale with a weight of 5 on the left side and 9 on the right side. It is in fact possible to balance this scale by adding a 6 to the left side from the list of weights and adding a 2 to the right side. Both scales will now equal 11 and they are perfectly balanced. Your program should return a common separated string of the weights that were used from the list in ascending order, so for this example your program should return the string  2,6 

There will only ever be one unique solution and the list of available weights will not be empty. It is also possible to add two weights to only one side of the scale to balance it. If it is not possible to balance the scale then your program should return the string  not possible
Sample Test Cases

Input:"[3, 4]", "[1, 2, 7, 7]"

Output:"1"


Input:"[13, 4]", "[1, 2, 3, 6, 14]"

Output:"3,6"


Scale Balancing算法

1、第一个字符串是平衡尺上的两个权重;第二个字符串是可用权重列表;

2、最多使用两个权重,是平衡尺上数值相等,权重可以加在一侧,另一侧不加;

3、把需要用到的权重,升序返回;如果没有返回"not possible";

        public static string ScaleBalancing(string[] strArr)
        {
            string XY = strArr[0];
            int X = 0;
            int Y = 0;
            bool IsFirst = true;

            Regex mRegex = new Regex("[a-zA-Z0-9]+");
            MatchCollection mMactchCol = mRegex.Matches(XY);
            foreach (Match item in mMactchCol)
            {
                if (IsFirst)
                {
                    int.TryParse(item.Value, out X);
                    IsFirst = false;
                }
                else
                {
                    int.TryParse(item.Value, out Y);
                }
            }

            {
                //不使用权重
                if (X == Y)
                {
                    return "";
                }
            }

            int NUM = 0;
            List<int> NUMlist = new List<int>();
            XY = strArr[1];
            mMactchCol = mRegex.Matches(XY);
            foreach (Match item in mMactchCol)
            {
                int.TryParse(item.Value, out NUM);
                NUMlist.Add(NUM);
            }

            {
                //一端有一个权重
                int value = Math.Abs(X - Y);
                for (int i = 0; i < NUMlist.Count; i++)
                {
                    if (NUMlist[i] == value)
                    {
                        return NUMlist[i].ToString();
                    }
                }
            }

            {
                //一端有两个权重
                int value = Math.Abs(X - Y);

                for (int i = 0; i < NUMlist.Count; i++)
                {
                    for (int j = i + 1; j < NUMlist.Count; j++)
                    {
                        if (((NUMlist[i] + NUMlist[j]) == value)
                            || (Math.Abs(NUMlist[i] - NUMlist[j]) == value))
                        {
                            if (NUMlist[i] < NUMlist[j])
                            {
                                return NUMlist[i].ToString() + "," + NUMlist[j].ToString();
                            }
                            else
                            {
                                return NUMlist[j].ToString() + "," + NUMlist[i].ToString();
                            }
                        }
                    }
                }
            }

            {
                //左右两端都有一个权重
                int[,] LeftValue = new int[NUMlist.Count, 3];
                int[,] RightValue = new int[NUMlist.Count, 3];
                for (int i = 0; i < NUMlist.Count; i++)
                {
                    LeftValue[i, 0] = NUMlist[i];
                    LeftValue[i, 1] = X + NUMlist[i];
                    LeftValue[i, 2] = X - NUMlist[i];

                    RightValue[i, 0] = NUMlist[i];
                    RightValue[i, 1] = Y + NUMlist[i];
                    RightValue[i, 2] = Y - NUMlist[i];
                }

                List<int> Leftlist = new List<int>();
                List<int> Rightlist = new List<int>();
                for (int i = 0; i < NUMlist.Count; i++)
                {
                    Leftlist.Add(LeftValue[i, 1]);
                    Leftlist.Add(LeftValue[i, 2]);
                    Rightlist.Add(RightValue[i, 1]);
                    Rightlist.Add(RightValue[i, 2]);
                }

                foreach (int L in Leftlist)
                {
                    foreach (int R in Rightlist)
                    {
                        if (L == R)
                        {
                            int left = 0;
                            int right = 0;
                            for (int i = 0; i < NUMlist.Count; i++)
                            {
                                if (LeftValue[i, 1] == L || LeftValue[i, 2] == L)
                                {
                                    left = LeftValue[i, 0];
                                }
                                else if (RightValue[i, 1] == L || RightValue[i, 2] == L)
                                {
                                    right = RightValue[i, 0];
                                }
                            }
                            if (left != 0 && right != 0)
                            {
                                if (left < right)
                                {
                                    return left.ToString() + "," + right.ToString();
                                }
                                else
                                {
                                    return right.ToString() + "," + left.ToString();
                                }
                            }
                        }
                    }
                }
            }

            return "not possible";
        }



猜你喜欢

转载自blog.csdn.net/njit_77/article/details/79748334