Scale Balancing

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";
        }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值