C# - HackerRank simpleArraySum

时间:2017-07-03 02:20:23

标签: c#

现在,我可能会得到负面的观点,因为也许在广阔的互联网上的某个地方已经有了答案,但我试图寻找它,我根本找不到它。

问题的关键在于HackerRanks希望您创建一个由用户决定的大小的数组,然后让用户添加其值(整数),最后让程序对其值求和。

有很多方法可以做到这一点我已经知道如何但我的问题是我无法理解Hackerrank在C#中给我的代码示例。我评论了我不理解的部分,其中大部分是:

    static int simpleArraySum(int n, int[] ar) {
    // Complete this function
    int sum = 0;
    foreach( var item in ar){
        sum += item;
    }
    return sum;
}

static void Main(String[] args) {
    //I know what this does
    int n = Convert.ToInt32(Console.ReadLine());
    //I am lost here, just why create a string array and add the split method?
    string[] ar_temp = Console.ReadLine().Split(' ');
    //I dont understand here neither, what is it converting? What is the parse for?
    int[] ar = Array.ConvertAll(ar_temp,Int32.Parse);
    //Why send the n when all you need is the array itself?
    int result = simpleArraySum(n, ar);
    Console.WriteLine(result);
}

我知道有些人讨厌HackerRank,老实说,我也是这样,但它确实给了我一些很好的方法来测试我用c#编写的有限技能并测试我的逻辑。因此,如果有更好的网站可以帮助您测试您的逻辑作为CS,请与我分享。

以下是我在Visual Studio中解决此问题的代码,但由于一些愚蠢的原因,除非我自定义输入,否则Hackerrank不会接受它:

    //This code can be potentially shorter using the code commented further below.
    //For practice's sake, it was made longer.
    static int simpleArraySum(int[] arr_temp)
    {
        int total = 0;
        foreach (var item in arr_temp)
        {
            total += item;              
        }
        return total;
    }

    static void Main(String[] args)
    {
        int n = Convert.ToInt32(Console.ReadLine());
        int[] arr_temp = new int[n];

        for (int i = 0; i < n; i++)
        {
            arr_temp[i] = Convert.ToInt32(Console.ReadLine());
        }

        int result = simpleArraySum(arr_temp);
        //int result = arr_temp.Sum();
        Console.WriteLine(result);
        Console.ReadLine();
    }

2 个答案:

答案 0 :(得分:0)

你需要转换为字符串数组,因为如果你在主方法上,它所获得的只是参数列表中的字符串值。要获得总和,您需要将字符串转换为可用的数字/整数。

我同意在n中发送第一个参数simpleArraySum是没有意义的,因为n根本就没用过。

对于部分int[] ar = Array.ConvertAll(ar_temp,Int32.Parse);,它只是尝试将所有整数接收到数组中。它也有风险,因为如果你不小心传入一个字符串,那么它会抛出错误,即传入&#34; 3 4 1 f&#34; &lt; - f将抛出异常,除非这是所需的行为。

我个人认为主要方法不应该对数据过多涉及,应该在方法中进行繁重的工作。更好的版本可能是修改simpleArraySum并重构该行:

static int simpleArraySum(string input)
    {
        String[] fields = input.Split(null);
        List<int> vals = new List<int>();
        foreach (string i in fields)
        {
            var j = 0;
            if (Int32.TryParse(i, out j)) vals.Add(j);

        }

        int sum = 0;
        foreach (var item in vals)
        {
            sum += item;
        }
        return sum;
    }

我介绍了泛型列表的使用,因为它更具可读性,如果不是更清晰,虽然使用List可能看起来对某些程序员来说过于苛刻,并且可能不像使用数组那么轻,因此另一方面你可以很容易地坚持使用数组,除了它需要用int[] vals = new int[fields.Length];粗略地初始化{<1}}大致:

static int simpleArraySum(string input)
    {
        String[] fields = input.Split(null);

        int[] vals = new int[fields.Length];
        for (int i = 0; i < fields.Length; i++)
        {
            var j = 0;
            if (Int32.TryParse(fields[i], out j)) vals[i] = j;

        }

        int sum = 0;
        foreach (var item in vals)
        {
            sum += item;
        }
        return sum;
    }

答案 1 :(得分:0)

我的代码希望对您有帮助

static int simpleArraySum(int[] ar,int count) {
        if (count > 0 && count <= 10000)
            {
                if (count == ar.Length)
                {
                    if (!ar.Any(item => (item < 0 || item >= 10000)))
                    {
                        return ar.Sum();
                    }
                }
            }
            return 0;        
    }

和主要

            int arCount = Convert.ToInt32(Console.ReadLine());
            int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp));
            int result = simpleArraySum(arr, arCount);
            Console.WriteLine(result);

因为 Array.ConvertAll() 将一个字符串转换为一个类型数组 例如int或float