idkhts 2017-11-01 10:25 采纳率: 0%
浏览 1105

c++新手问题。。。。。

#include"stdafx.h"
#include
using namespace std;
#define NUM 10
int search(int value[], int key, int low, int high);
void main()
{
int value[NUM], result = 1, key = 2;
int i;
cout << "input ten number which are from big to small:" << endl;
for (i = 0; i < NUM; i++)
{
cin >> value[i];/* 输入从大到小的有序数列*/
}
cout << "input one of the ten number:" << endl;
cin >> key; /* 输入待查找的数值*/
result = search(value, key, 0, NUM - 1); /*调用二分查找函数进行查找*/
if (result != -1) cout << "Number." << result << "is :" << key << endl;
else cout << "Can't find the key" << key << endl;
}
int search(int value[], int key, int low, int high)
{
int mid;
if (low > high) return -1; /*查找区间为空*/
else mid = ((low + high) / 2); /*求中间位置*/
if (value[mid] == key) return mid;/*得到查找的数据位置*/
if (key < value[mid]) return search(value, key, low, mid - 1); else return search(value, key, mid + 1, high);/*在下半区查找*/
}

错在哪里?为什么?

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-11-02 01:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作