Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Get NZEC错误解析HackerAth上的输入_C# - Fatal编程技术网

C#Get NZEC错误解析HackerAth上的输入

C#Get NZEC错误解析HackerAth上的输入,c#,C#,我想学习C#,所以我开始使用hackerearth并从他们的网站上解决问题,但我遇到了一些问题。所以我有以下代码 using System; namespace ConsoleApp6 { class Program { static void Main(string[] args) { long N, i, answer = 1; do {

我想学习C#,所以我开始使用hackerearth并从他们的网站上解决问题,但我遇到了一些问题。所以我有以下代码

using System;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            long N, i, answer = 1;
            do
            {
                N = Convert.ToInt32(Console.ReadLine());

            } while (N < 1 && N > 1000);

            long[] A = new long[N];

            for (i = 0; i < N; i++)
            {
                do
                {
                    A[i] = Convert.ToInt32(Console.ReadLine());
                } while (A[i] < 1 && A[i] > 1000);
            }

            for(i = 0; i < N; i++)
            {
                answer = (answer * A[i]) % (1000000007);
            }

            Console.WriteLine(answer);
        }
    }
}
使用系统;
名称空间控制台App6
{
班级计划
{
静态void Main(字符串[]参数)
{
长N,i,答案=1;
做
{
N=Convert.ToInt32(Console.ReadLine());
}而(N<1&&N>1000);
long[]A=新的long[N];
对于(i=0;i1000);
}
对于(i=0;i

当我编译它时,我得到了正确的答案,一切都很好,但当我将它提交给hackerearth编译器时,它给了我NZEC错误。我觉得我错过了一些东西,因为我刚开始C几天前,所以我写了一遍,但在C++中,它给了我在网站上的最高分数。我知道在我的变量声明中可能会有一些问题,因为我不完全理解如何读取数字,我希望您能帮助我解决这个问题。谢谢大家!

假设您被困在,正如您所怀疑的,数据的输入是一行表示N,然后一行表示需要相乘的所有N个数字,用空格分隔。你可以用LINQ快速解析数字行(我建议你尽快地进入LINQ),这将使你远离C++命令式思维模式。 那么:

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

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = Convert.ToInt32(Console.ReadLine()); // Less than 2^31 integers to be read
            var A = Console.ReadLine() // Read the line of space delimited numbers
                .Split(' ') // Split out by the separator
                .Select(n => Convert.ToInt64(n)) // Parse each number to long
                .ToArray(); // Convert to a materialized array

            Debug.Assert(A.Length == N, "Site lied to us about N numbers");

            long answer = 1; // or var answer = 1L;
            for(var i = 0; i < N; i++)
            {
                answer = (answer * A[i]) % (1000000007);
            }

            Console.WriteLine(answer);
        }
    }
}

任何时候你遇到错误都应该告诉我们错误。我告诉过你错误。当我提交用C#编写的代码时,出现了运行时错误NZEC。什么?你能解释一下如何写正确的陈述吗?和完全相同的条件,我在C++中得到了最大的分数。
var answer = A.Aggregate((subtotal, next) => (subtotal * next) % (1000000007));