c - 如何修复 HackerRank 中的 "~ no response on stdout ~"错误?

标签 c

我正在尝试在 HackerRank 上解决这个问题,一旦我提交了我的解决方案,编译器就会显示 wrong answer 并且在输出中显示 ~ no response on stdout ~ window 。我该如何解决这个问题?

Question:

Given a 6X6 2D Array A,

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

We define an hourglass in A to be a subset of values with indices falling in this pattern in A's graphical representation:

a b c
  d
e f g

There are 16 hourglasses in A, and an hourglass sum is the sum of an hourglass' values.

Task

Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum.

我已经尝试解决了这个问题。我就是无法摆脱这个错误信息。逻辑和解决方案似乎是正确的。

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *readline();
char **split_string(char *);

void func(int arr[][100]) {
    int hgs[4][4];
    int i = 0, j = 0;
    for (i = 0; i < 4; i++) {
       for (j = 0; j < 4; j++) {
           hgs[i][j] = arr[i][j] + arr[i][j+1] + arr[i][j+2] +
                       arr[i+1][j+1] +
                       arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
        }
    }
    int max = hgs[0][0];
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            if (hgs[i][j] > max)
                max = hgs[i][j];
        }
    }
    printf("%d", max);
}

int main() {
    int **arr = malloc(6 * sizeof(int*));

    for (int i = 0; i < 6; i++) {
        *(arr + i) = malloc(6 * (sizeof(int)));

        char **arr_item_temp = split_string(readline());

        for (int j = 0; j < 6; j++) {
            char* arr_item_endptr;
            char* arr_item_str = *(arr_item_temp + j);
            int arr_item = strtol(arr_item_str, &arr_item_endptr, 10);

            if (arr_item_endptr == arr_item_str || *arr_item_endptr != 
'\0') {
                exit(EXIT_FAILURE);
            }
            *(*(arr + i) + j) = arr_item;
       }
    }
    func(**arr);
    return 0;
}

char *readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);

    while (true) {
        char *cursor = data + data_length;
        char *line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) {
            break;
        }
        data_length += strlen(cursor);
        if (data_length < alloc_length - 1 || data[data_length - 1] == 
'\n') {
            break;
        }

        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);
        if (!data) {
            break;
        }

        alloc_length = new_length;
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    }

    data = realloc(data, data_length);

    return data;
}

char **split_string(char *str) {
    char **splits = NULL;
    char *token = strtok(str, " ");

    int spaces = 0;

    while (token) {
        splits = realloc(splits, sizeof(char*) * ++spaces);
        if (!splits) {
            return splits;
        }

        splits[spaces - 1] = token;

        token = strtok(NULL, " ");
    }
    return splits;
}

示例输入的预期输出为 19,但我没有得到任何输出。

最佳答案

对于这个简单的程序,你的代码很复杂,但问题可能很简单:

  • func 的原型(prototype)应该是:

    void func(int **arr)
    
  • 您应该以换行符结束您的输出:

        printf("%d\n", max);
    
  • 您应该以这种方式将数组传递给 func:

        func(arr);
    

这是 func 代码的简化版本:

void func(int **arr) {
    int i, j, cur, max = INT_MIN;
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            cur = arr[i][j] + arr[i][j+1] + arr[i][j+2] +
                  arr[i+1][j+1] +
                  arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
            if (max < cur)
                max = cur;
        }
    }
    printf("%d\n", max);
}

下面是整个程序的简化版:

#include <limits.h>
#include <stdio.h>

int main() {
    int grid[6][6];
    int i, j, cur, max = INT_MIN;

    for (i = 0; i < 6; i++) {
        for (j = 0; j < 6; j++) {
            if (scanf("%d", &grid[i][j]) != 1)
                return 1;
        }
    }
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            cur = arr[i][j] + arr[i][j+1] + arr[i][j+2] +
                  arr[i+1][j+1] +
                  arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
            if (max < cur)
                max = cur;
        }
    }
    printf("%d\n", max);
    return 0;
}

关于c - 如何修复 HackerRank 中的 "~ no response on stdout ~"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429961/

相关文章:

C 读取文件并从输出中删除字符,无数组

计算字符串的单词

c++ - 为 C++ 示例设置基本 Esent

c - 在 C 中将原始 24 位像素图缩放到大于 50% 的算法

java - java中的引用类型和C中的指针的区别

检查 C 中特定字符值的字符串?

c - 将0赋给一个int后,int的值立即为134518533

c - 使用 Linux 内核中的循环缓冲区宏

c - 如何让 doxygen 为 c 函数生成调用和调用者图

c - 获取文件中元素的数量