C# 阵列拆分方案

C# 阵列拆分方案,c#,arrays,C#,Arrays,因此,我目前正尝试这样做: 给定一个非空数组,如果存在拆分数组的位置,则返回true,以便一侧的数字之和等于另一侧的数字之和。 示例: canBalance([1, 1, 1, 2, 1]) → true canBalance([2, 1, 1, 2, 1]) → false canBalance([10, 10]) → true 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Task

因此,我目前正尝试这样做: 给定一个非空数组,如果存在拆分数组的位置,则返回true,以便一侧的数字之和等于另一侧的数字之和。 示例:

canBalance([1, 1, 1, 2, 1]) → true
canBalance([2, 1, 1, 2, 1]) → false
canBalance([10, 10]) → true
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间数组分割
{
班级计划
{
公共静态布尔余额(int[]数据)
{
int-lpoint=0;
int rpoint=data.Length-1;
int ltotal=数据[lpoint];
int rtotal=数据[rpoint];
while(lpoint

预期结果应该是真、假、真。但是,当两个值相等时,它会给出false、true、true,不要同时更改
lpoint
rpoint
。考虑在<代码> {1,2,1} < /代码>中发生的事情-它们都试图在索引1中“声明”值2,并且错误地返回该数组的真值。因此,只需更改
lpoint
(或
rpoint
——但不能同时更改两者):


因为你所做的与当
ltotal
时一样,你可以通过检查
ltotal将它与该块结合起来。我只是为了好玩才给它打了一枪

using System;
using System.Linq;
using System.Collections.Generic;


namespace SO_58400592_array_split {
    class Program {
        private static List<int[]> _testCases = new List<int[]>() {
            {new int[] {1,1,1,2,1}},
            {new int[] {2,1,1,2,1}},
            {new int[] {10,10}}
        };

        static void Main(string[] args) {
            for (int index = 0; index < _testCases.Count; index++) {
                Console.WriteLine(CanSplitArray(_testCases[index]));
            }
        }



        static bool CanSplitArray(int[] model) {
            //  special case of 2 elements
            if(2 == model.Length) {
                return model[0] == model[1];
            }

            // sum the model
            int sum = model.Sum();

            //  caculate the value from each end
            int leftSum = 0;
            int rightSum = sum;
            //  start at the first index and end at the penultimate index.
            for (int index = 1; index < model.Length - 1; index++) {
                leftSum += model[index - 1];
                rightSum -= model[index - 1];
                if (leftSum == rightSum) {
                    return true;
                }
            }

            return false;
        }   //  CanSplitArray()

    }   //  Program
}       //  ns
使用系统;
使用System.Linq;
使用System.Collections.Generic;
名称空间SO_58400592_数组_拆分{
班级计划{
私有静态列表_testCases=newlist(){
{new int[]{1,1,1,2,1}},
{new int[]{2,1,1,2,1}},
{new int[]{10,10}}
};
静态void Main(字符串[]参数){
对于(int index=0;index<\u testCases.Count;index++){
WriteLine(CanSplitArray(_testCases[index]);
}
}
静态布尔CanSplitArray(int[]模型){
//2个元素的特例
如果(2==模型长度){
返回模型[0]==模型[1];
}
//总结模型
int sum=model.sum();
//计算每一端的值
int leftSum=0;
int rightSum=sum;
//从第一个索引开始,到倒数第二个索引结束。
for(int index=1;index
类似的解决方案:但可以提供一些好处:

    var sum = balance.ToList().Sum();
    if (sum % 2 == 0)
    {
        var left = 0;
        for (int i = 0; left < sum / 2; i++)
        {
            left+=balance[i];   
        }           
        return (left - (sum - left) == 0);
    }
    return false;
等等


然后代码不同,但算法类似,以确保左右匹配。

下一步,调试代码。使用断点并逐行遍历它,查看它返回意外结果的原因。您可以在Visual Studio中使用手表、快速手表、区域设置等来查看正在调试的变量/字段/属性的值。
if (ltotal <= rtotal)
{
    lpoint++;
    ltotal = ltotal + data[lpoint];
}
else
{
    rpoint--;
    rtotal = rtotal + data[rpoint];
}
while (lpoint + 1 < rpoint)
using System;
using System.Linq;
using System.Collections.Generic;


namespace SO_58400592_array_split {
    class Program {
        private static List<int[]> _testCases = new List<int[]>() {
            {new int[] {1,1,1,2,1}},
            {new int[] {2,1,1,2,1}},
            {new int[] {10,10}}
        };

        static void Main(string[] args) {
            for (int index = 0; index < _testCases.Count; index++) {
                Console.WriteLine(CanSplitArray(_testCases[index]));
            }
        }



        static bool CanSplitArray(int[] model) {
            //  special case of 2 elements
            if(2 == model.Length) {
                return model[0] == model[1];
            }

            // sum the model
            int sum = model.Sum();

            //  caculate the value from each end
            int leftSum = 0;
            int rightSum = sum;
            //  start at the first index and end at the penultimate index.
            for (int index = 1; index < model.Length - 1; index++) {
                leftSum += model[index - 1];
                rightSum -= model[index - 1];
                if (leftSum == rightSum) {
                    return true;
                }
            }

            return false;
        }   //  CanSplitArray()

    }   //  Program
}       //  ns
    var sum = balance.ToList().Sum();
    if (sum % 2 == 0)
    {
        var left = 0;
        for (int i = 0; left < sum / 2; i++)
        {
            left+=balance[i];   
        }           
        return (left - (sum - left) == 0);
    }
    return false;
Sum(5,4,9) % 2 = 0
Sum(5,5,9) % 2 = 1