鱼C论坛

 找回密码
 立即注册
查看: 1580|回复: 3

[已解决]代码上的一点小问题

[复制链接]
发表于 2023-8-6 11:38:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. int a[16]={0,5,6,8,10,13,18,22,26,27,29,30,36,58,59,87};

  2. void search(int x,int y,int num){
  3.     int index = (x+y)/2;
  4.     if(a[index]==num){  a[0]=index;   return; }
  5.     if(x==y)    return;
  6.     if(a[index]>num)    search(x,index,num);
  7.     else if(a[index]<num) search(index,y,num);
  8. }

  9. int main(){
  10.     search(1,15,60);
  11.     if(a[0]!=0){
  12.         printf("%d,在第%d个!\n",a[a[0]],a[0]);
  13.     }
  14.     else{
  15.         printf("查无此数!\n");
  16.     }
  17. }
复制代码


这段在查找成功的时侯可以成功输出,但是在查找失败却输出失败,求大佬们解答!
最佳答案
2023-8-6 11:39:01
问题出在搜索函数的递归调用上。在搜索函数中,当a[index]>num时,应该递归调用search函数时将y参数设为index-1,而不是y参数设为index。同样地,当a[index]<num时,应该递归调用search函数时将x参数设为index+1,而不是x参数设为index。

以下是修改后的代码:

  1. #include <stdio.h>

  2. int a[16]={0,5,6,8,10,13,18,22,26,27,29,30,36,58,59,87};

  3. void search(int x, int y, int num){
  4.     int index = (x+y)/2;
  5.     if(a[index] == num){
  6.         a[0] = index;
  7.         return;
  8.     }
  9.     if(x == y)
  10.         return;
  11.     if(a[index] > num)
  12.         search(x, index-1, num);
  13.     else if(a[index] < num)
  14.         search(index+1, y, num);
  15. }

  16. int main(){
  17.     search(1, 15, 60);
  18.     if(a[0] != 0){
  19.         printf("%d,在第%d个!\n", a[a[0]], a[0]);
  20.     }
  21.     else{
  22.         printf("查无此数!\n");
  23.     }
  24.     return 0;
  25. }
复制代码


这样修改后,当查找失败时会正确输出"查无此数!"。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-6 11:39:01 | 显示全部楼层
问题解答:

你提供的代码中存在两个问题导致查找失败时也会输出失败的情况:

1. 当查找失败时,应该在递归的最后一步进行输出失败的操作。你可以通过在 search() 函数中添加一个 else 条件来实现,在所有的判断条件都不满足时输出失败。

2. 在查找成功后,由于 a[0] 被设定为了查找到的索引值,因此在输出结果时需要使用 a[a[0]] 来查找实际的数值。

以下是修改后的完整代码:
  1. ++#include <stdio.h>
  2. int a[16] = {0, 5, 6, 8, 10, 13, 18, 22, 26, 27, 29, 30, 36, 58, 59, 87};
  3. void search(int x, int y, int num) {
  4.     int index = (x + y) / 2;
  5.     if (a[index] == num) {
  6.         a[0] = index;
  7.         return;
  8.     }
  9.     if (x == y)
  10.         return;
  11.     if (a[index] > num)
  12.         search(x, index, num);
  13.     else if (a[index] < num)
  14.         search(index, y, num);
  15.     else
  16.         printf("查无此数!\n");
  17. }
  18. int main() {
  19.     search(1, 15, 60);
  20.     if (a[0] != 0) {
  21.         printf("%d,在第%d个!\n", a[a[0]], a[0]);
  22.     }
  23.     else {
  24.         printf("查无此数!\n");
  25.     }
  26.     return 0;
  27. }
复制代码

修改后的代码会在查找失败时输出 "查无此数!"。希望这能解决你的问题,如有任何其他问题,请随时提问。

如果回答对你有帮助,请给我一个最佳答案!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-6 11:39:01 | 显示全部楼层    本楼为最佳答案   
问题出在搜索函数的递归调用上。在搜索函数中,当a[index]>num时,应该递归调用search函数时将y参数设为index-1,而不是y参数设为index。同样地,当a[index]<num时,应该递归调用search函数时将x参数设为index+1,而不是x参数设为index。

以下是修改后的代码:

  1. #include <stdio.h>

  2. int a[16]={0,5,6,8,10,13,18,22,26,27,29,30,36,58,59,87};

  3. void search(int x, int y, int num){
  4.     int index = (x+y)/2;
  5.     if(a[index] == num){
  6.         a[0] = index;
  7.         return;
  8.     }
  9.     if(x == y)
  10.         return;
  11.     if(a[index] > num)
  12.         search(x, index-1, num);
  13.     else if(a[index] < num)
  14.         search(index+1, y, num);
  15. }

  16. int main(){
  17.     search(1, 15, 60);
  18.     if(a[0] != 0){
  19.         printf("%d,在第%d个!\n", a[a[0]], a[0]);
  20.     }
  21.     else{
  22.         printf("查无此数!\n");
  23.     }
  24.     return 0;
  25. }
复制代码


这样修改后,当查找失败时会正确输出"查无此数!"。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-6 11:39:05 | 显示全部楼层
在这段代码中,查找失败时输出失败的问题出在对边界条件的处理上。当x==y时,也就是在查找范围缩小到只有一个元素时,你没有正确地处理这种情况,导致没有返回结果。

你可以修改search函数中的边界条件判断部分来解决这个问题。修改后的代码如下:
  1. #include <stdio.h>
  2. int a[16] = {0, 5, 6, 8, 10, 13, 18, 22, 26, 27, 29, 30, 36, 58, 59, 87};
  3. void search(int x, int y, int num){
  4.     int index = (x + y) / 2;
  5.    
  6.     if(a[index] == num){
  7.         a[0] = index;
  8.         return;
  9.     }
  10.    
  11.     if(x > y)
  12.         return;
  13.    
  14.     if(a[index] > num)
  15.         search(x, index - 1, num); // 修改为 index - 1
  16.     else if(a[index] < num)
  17.         search(index + 1, y, num); // 修改为 index + 1
  18. }
  19. int main(){
  20.     search(1, 15, 60);
  21.    
  22.     if(a[0] != 0){
  23.         printf("%d 在第 %d 个!\n", a[a[0]], a[0]);
  24.     }
  25.     else{
  26.         printf("查无此数!\n");
  27.     }
  28.    
  29.     return 0;
  30. }
复制代码

在这段代码中,我对边界条件判断部分进行了修改。如果x超过y时,表示已经查找完整个范围,但没有找到目标数字,此时应该返回。在递归调用search函数时,对index进行加减操作来调整查找范围。

重新运行修改后的代码后,查找失败时会正确输出"查无此数!"。希望这个解答能够帮到你!如果还有其他问题,请随时提问。

如果回答对你有帮助,请给我一个最佳答案!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 22:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表