为什么我会在hackerrank中收到这个消息在标准输出上没有响应;?我不知道';我不知道我错过了什么>;
为什么我会在hackerrank中收到这样的消息“~标准输出没有响应”?我不知道我错过了什么? 我现在有点沮丧,因为我不知道该做什么。 所以我只能选择在Stackoverflow上发布这个查询 这是 这是我的完整代码:为什么我会在hackerrank中收到这个消息在标准输出上没有响应;?我不知道';我不知道我错过了什么>;,c,C,为什么我会在hackerrank中收到这样的消息“~标准输出没有响应”?我不知道我错过了什么? 我现在有点沮丧,因为我不知道该做什么。 所以我只能选择在Stackoverflow上发布这个查询 这是 这是我的完整代码: char* readline(); // Complete the countingValleys function below. int countingValleys(int n, char* s) { int dwnhl = 0, level = 0; bool
char* readline();
// Complete the countingValleys function below.
int countingValleys(int n, char* s)
{
int dwnhl = 0, level = 0;
bool frmsurface = true;
int k = strlen(s);
for (int i = 0; i < k; i++)
{
if (level == 0)
{
frmsurface = true;
}
if (s[i] == 'D')
{
level--;
if ((level < 0) && (frmsurface == true))
{
dwnhl++;
frmsurface = false;
//printf("went downhill %d ",i);
}
}
else if (s[i] == 'U')
{ //printf("went uphill %d ",i);
level++;
}
// printf("\nhello - %c",s[i]);
}
printf("\nNumber of downhill = %d \n", dwnhl);
return (dwnhl);
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char* n_endptr;
char* n_str = readline();
int n = strtol(n_str, &n_endptr, 10);
if (n_endptr == n_str || *n_endptr != '\0')
{
exit(EXIT_FAILURE);
}
char* s = readline();
int result = countingValleys(n, s);
printf("%d\n", result);
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*readline();
//完成下面的CountingValley功能。
int countingvalley(int n,char*s)
{
int dwnhl=0,级别=0;
bool frmsurface=true;
int k=strlen(s);
for(int i=0;i size\u t new\u length=alloc\u length一个问题是如何处理frmsurface
第一次进入循环时,frmsurface
设置为true。如果事件是UUDD
,则代码仍将计算为“谷”,因为您在上升时未清除frmsurface
而不是
if(level==0)
{
frmsurface=true;
}
你可以试试:
frmsurface = (level == 0);
但我真的不明白你为什么想要布尔值。只需测试level==0
。类似于:
if(s[i]=='D')
{
if(level==0)
{
dwnhl++;
}
level--;
}
else if (s[i]=='U')
{
level++;
}
我还想知道这句话:
printf("\nNumber of downhill = %d \n", dwnhl);
必须删除
注意
int k=strlen(s);
for(int i=0;i<k;i++)
int k=strlen(s);
对于(int i=0;i一个问题是处理frmsurface
第一次进入循环时,frmsurface
设置为true。如果事件是UUDD
,则代码仍将计算为“谷”,因为您在上升时未清除frmsurface
而不是
if(level==0)
{
frmsurface=true;
}
你可以试试:
frmsurface = (level == 0);
但我真的不明白你为什么想要布尔值。只需测试level==0
。类似于:
if(s[i]=='D')
{
if(level==0)
{
dwnhl++;
}
level--;
}
else if (s[i]=='U')
{
level++;
}
我还想知道这句话:
printf("\nNumber of downhill = %d \n", dwnhl);
必须删除
注意
int k=strlen(s);
for(int i=0;i<k;i++)
int k=strlen(s);
对于(int i=0;i您想做什么?崩溃时行在哪里?如果没有崩溃,您从哪里得到消息?可能与您的问题无关,您在readline
函数中有一个致命缺陷,您可以在字符串数组中使用负索引。还有两个致命缺陷,您没有检查readline
return。这个问题(我无法阅读链接)是否真的要求从printf(“\n下坡数量=%d\n”,dwnhl)获得此输出
?这似乎不寻常。别忘了有一个机器人正在检查输出,它必须与问题所述的输出完全匹配。@WeatherVane注意,OP只在CountingValley
函数中编写了代码。如果机器人正在执行一个lin,则所有其他代码都是由Hackerrank在我之前的评论中添加的然后,您的消息以一个空行开始。您想做什么?崩溃时该行在哪里?如果没有崩溃,您从哪里获得消息?可能与您的问题无关,您在readline
函数中有一个致命缺陷,您可以在字符串数组中使用负索引。还有两个致命缺陷这里不检查readline
返回的内容。问题(我无法读取链接)是否真的要求从printf(“\n下坡数=%d\n”,dwnhl)获得此输出
?这似乎不寻常。别忘了有一个机器人正在检查输出,它必须与问题所述的输出完全匹配。@WeatherVane注意,OP只在CountingValley
函数中编写了代码。如果机器人正在执行一个lin,则所有其他代码都是由Hackerrank在我之前的评论中添加的然后你的消息以一个空行开始。问题解决了。我不知道问题到底是什么。但是我重置了代码并再次键入,它成功了。也许我遗漏了什么。顺便说一句,谢谢你的回复。问题解决了。我不知道问题到底是什么。但是我重置了代码并再次键入,它成功了。也许我遗漏了一些东西。顺便说一句,谢谢大家的回复。