.net C#中的Diagonal Difference Challenge在HackerRank上失败测试

nkoocmlb  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(126)

我对在线代码挑战比较陌生,对HackerRank的工作原理有些困惑。
以下实现应该可以很好地用于对角差异质询:

// Test case 0
List<List<int>> arr = new List<List<int>>();
arr.Add(new List<int> { 3 });
arr.Add(new List<int> { 11, 2, 4 });
arr.Add(new List<int> { 4, 5, 6 });
arr.Add(new List<int> { 10, 8, -12 });
// Sum across the primary diagonal: 11 + 5 - 12 = 4
// Sum across the secondary diagonal: 4 + 5 + 10 = 19
// Absolute Difference: |4 - 19| = 15

// Test case 1
/*
List<List<int>> arr = new List<List<int>>();
arr.Add(new List<int> { 4 });
arr.Add(new List<int> { -1, 1, -7, -8 });
arr.Add(new List<int> { -10, -8, -5, -2 });
arr.Add(new List<int> { 0, 9, 7, -1 });
arr.Add(new List<int> { 4, 4, -2, 1 });
*/

// By definition, the data structure already provides the size.
arr.RemoveAt(0);
List<List<int>> matrix = arr;

List<int> principalDiagonal = new List<int>();
for (int i = 0; i < matrix.Count; i++)
{
    principalDiagonal.Add(matrix[i][i]);
}

List<int> secondaryDiagonal = new List<int>();
for (int i = 0; i < matrix.Count; i++)
{
    secondaryDiagonal.Add(matrix[i][matrix.Count - 1 - i]);
}

int absoluteDifference = Math.Abs(principalDiagonal.Sum() - secondaryDiagonal.Sum());

字符串
我甚至在一个.NET Fiddle上用他们自己的几个测试用例试过它,它像预期的那样工作:https://dotnetfiddle.net/6s4aU2
然而,当我提交代码时,它被认为是一个错误的答案,奇怪的是没有任何错误,只是基于测试用例的不同结果。

// HackerRank's "Console" message 

/*
    Compiler Message
    Wrong Answer
    Input (stdin)
    3
    11 2 4
    4 5 6
    10 8 -12
    Your Output (stdout)
    3
*/


有没有人在这个平台上有类似的经历?

qkf9rpyu

qkf9rpyu1#

问题是您更改了读取数据的方式。(所以现在您是从创建的静态表中阅读数据)
下面的代码是工作的(是你的解决方案内提供的方法)

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{

    /*
     * Complete the 'diagonalDifference' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts 2D_INTEGER_ARRAY arr as parameter.
     */

    public static int diagonalDifference(List<List<int>> matrix)
    {
        List<int> principalDiagonal = new List<int>();
        for (int i = 0; i < matrix.Count; i++)
        {
            principalDiagonal.Add(matrix[i][i]);
        }

        List<int> secondaryDiagonal = new List<int>();
        for (int i = 0; i < matrix.Count; i++)
        {
            secondaryDiagonal.Add(matrix[i][matrix.Count - 1 - i]);
        }

        int absoluteDifference = Math.Abs(principalDiagonal.Sum() - secondaryDiagonal.Sum());
        return absoluteDifference;
    }

}

class Solution
{
    public static void Main(string[] args)
    {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine().Trim());

        List<List<int>> arr = new List<List<int>>();

        for (int i = 0; i < n; i++)
        {
            arr.Add(Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt32(arrTemp)).ToList());
        }

        int result = Result.diagonalDifference(arr);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}

字符串

相关问题