gpt4 book ai didi

c - 递归算法计算小于给定值的节点

转载 作者:行者123 更新时间:2023-11-30 20:09:09 25 4
gpt4 key购买 nike

Hello, I am studying for my final exam, so from my previous exam, I got partial credit for this problem. recursive algorithm such that it counts how many nodes of a given linked list have info values less than the given threshold value

 typedef struct LLNode {
int info;
struct LLNode* pNext;
}LLNode;

int recCount(LLNode *front, int threshold)
{


}

my answer was

int count = 0;
int total_less;
if(front == NULL)
return 0 ;
if(count < threshold)
count = 1 + recCount(front->next, front->info);
total_less++;

return total_

最佳答案

较短的版本:

如果值较小,则对结果加 +1 并检查列表中的下一个节点,否则只检查下一个节点而不添加到计数。

int recCount(struct NODE *N, int value)
{
if(N == NULL)
return 0;

if(N->data < value)
return 1 + recCount(N->next, value);

return recCount(N->next, value);
}

示例代码: http://tpcg.io/36qFkO

关于c - 递归算法计算小于给定值的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53774556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com