【发布时间】:2019-03-30 09:23:43
【问题描述】:
我正在尝试在 HackerRank 上解决这个问题,一旦我提交了我的解决方案,编译器就会显示 wrong answer 并在输出窗口中显示 ~ no response on stdout ~。我该如何解决这个问题?
问题:
给定一个 6X6 二维数组 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我们将 A 中的沙漏定义为值的子集,其索引在 A 的图形表示中属于这种模式:
a b c d e f gA 中有 16 个沙漏,沙漏和是沙漏值的总和。
任务
计算 A 中每个沙漏的沙漏总和,然后打印最大沙漏总和。
我已经尝试过解决问题。我就是无法摆脱这个错误信息。逻辑和解决方案似乎是正确的。
#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,但我没有得到任何输出。
【问题讨论】:
标签: c