gpt4 book ai didi

c - 尝试递归匹配 2 个硬编码数组中的数字,无法进行超过 1 个成功循环

转载 作者:行者123 更新时间:2023-11-30 18:34:10 24 4
gpt4 key购买 nike

我试图递归地创建一个循环遍历数组中所有元素的调用,并将每个元素与另一个元素进行比较。类似于排序而不实际更改数组,只是假装使用 print 语句。

这是我所拥有的:

#include <stdio.h>
// Send each pair of array indexes to a recursive
// Function that compares the values received and returns
// if a corresponding 'index' (will be named drawer within storage chest)
matches,
// "A match was found for nut 7' and bolt 7' in drawers 2 and 2."

void recurMatch(int nuts[], int bolts[], int position);

int main() {

int nuts[6] = {4, 7, 9, 8, 2, 5};
int bolts[6] = {5, 2, 9, 9, 7, 4};
printf("Let's clean up this Storage Chest...\n");
recurMatch(nuts, bolts, 0);
return 0;

}

void recurMatch(int nuts[], int bolts[], int position) {
int i, j;

for (i = 0, j = 0; nuts[i] <= 6; ++i, ++j) {

//printf("Position is %d\n", position);

++position;
//printf("Nuts #%d is: Size-%d\n", i + 1, nuts[i]);
//printf("Bolts #%d is: Size-%d\n\n", j + 1, bolts[j]);

if (nuts[i] == bolts[j]) {
printf("Nut Size-'%d' found in Nut-Drawer #%d, is equal to Bolt Size-%d found in Bolt-Drawer #%d.\n",
nuts[i], i + 1, bolts[j], position);
//nuts[i] = nuts[i + 1];
position = 1;
//i;
//return recurMatch(nuts, bolts, position);
//recurMatch(nuts + 1, bolts, 0);
} else {
return recurMatch(nuts, bolts + 1, position);
}

}

}

输出:

Let's clean up this Storage Chest...
Nut Size-'4' found in Nut-Drawer #1, is equal to Bolt Size-4 found in Bolt-Drawer #6.

哪个是正确的,但是我如何继续这个过程(递归地)直到它通过所有 6 个坚果及其正确的对?

感谢您的帮助。

最佳答案

我认为递归不是解决此任务的正确方法。使用两个 for 循环似乎是更好的解决方案。喜欢:

for(i=0; i<6, ++i)
for(j=0; j<6, ++j)
if (nuts[i] == bolts[j])
{
// print …

}
}
}

无论如何 - 如果你真的想要递归来完成这个任务,它可能看起来像:

#include <stdio.h>

#define NUM_ELEMENTS 6

void recurMatch(int nuts[], int nuts_pos, int bolts[], int bolts_pos);

int main() {
int nuts[NUM_ELEMENTS] = {4, 7, 9, 8, 2, 5};
int bolts[NUM_ELEMENTS] = {5, 2, 9, 9, 7, 4};
printf("Let's clean up this Storage Chest...\n");
recurMatch(nuts, 0, bolts, 0);
return 0;
}

void recurMatch(int nuts[], int nuts_pos, int bolts[], int bolts_pos)
{
if (nuts_pos == NUM_ELEMENTS)
return; // All done - just return

if (bolts_pos == NUM_ELEMENTS)
return recurMatch(nuts, nuts_pos+1, bolts, 0); // Next nuts element

if (nuts[nuts_pos] == bolts[bolts_pos]) // Check current elements
{
printf("Nut Size-'%d' found in Nut-Drawer #%d, is equal"
" to Bolt Size-%d found in Bolt-Drawer #%d.\n",
nuts[nuts_pos], nuts_pos, bolts[bolts_pos], bolts_pos);
}

return recurMatch(nuts, nuts_pos, bolts, bolts_pos+1); // Next bolts element
}

输出:

Let's clean up this Storage Chest...
Nut Size-'4' found in Nut-Drawer #0, is equal to Bolt Size-4 found in Bolt-Drawer #5.
Nut Size-'7' found in Nut-Drawer #1, is equal to Bolt Size-7 found in Bolt-Drawer #4.
Nut Size-'9' found in Nut-Drawer #2, is equal to Bolt Size-9 found in Bolt-Drawer #2.
Nut Size-'9' found in Nut-Drawer #2, is equal to Bolt Size-9 found in Bolt-Drawer #3.
Nut Size-'2' found in Nut-Drawer #4, is equal to Bolt Size-2 found in Bolt-Drawer #1.
Nut Size-'5' found in Nut-Drawer #5, is equal to Bolt Size-5 found in Bolt-Drawer #0.

关于c - 尝试递归匹配 2 个硬编码数组中的数字,无法进行超过 1 个成功循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53126502/

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