我正在尝试在HackerRank上解决此问题,一旦提交解决方案,编译器将在输出窗口中显示wrong answer
,并显示~ no response on stdout ~
。我该如何解决?
问题:
给出一个6X6 2D阵列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 g
A中有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
,但没有任何输出。
答案 0 :(得分:0)
对于这个简单的程序,您的代码非常复杂,但是问题可能非常简单:
func
的原型应为:
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;
}