用C++语言编写:输入n值(1<=n<=1000)、n个非降序排列的整数以及要查找的数x,使用二分查找算法查找x,输出x所在的下标(0~n-1)及比较次数。若x不存在,输出-1和比较次数。 输入格式: 输入共三行: 第一行是n值; 第二行是n个整数; 第三行是x值。 输出格式: 输出x所在的下标(0~n-1)及比较次数。若x不存在,输出-1和比较次数。 输入样例: 4 1 2 3 4 1 输出样例: 0 2
时间: 2024-02-09 10:07:04 浏览: 37
以下是使用C++实现的二分查找算法,可以直接复制运行:
#include <iostream>
using namespace std;
int main() {
int n, x;
cin >> n;
int nums[n]; // 用数组存储n个整数
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
cin >> x;
int left = 0, right = n - 1, count = 0;
while (left <= right) {
int mid = (left + right) / 2;
count++;
if (nums[mid] == x) {
cout << mid << " " << count << endl;
return 0;
} else if (nums[mid] < x) {
left = mid + 1;
} else {
right = mid - 1;
}
}
cout << -1 << " " << count << endl;
return 0;
}
相关问题
输入n值(1<=n<=1000)、n个非降序排列的整数以及要查找的数x,使用二分查找算法查找x,输出x所在的下标(0~n-1)及比较次数。若x不存在,输出-1和比较次数。并用c++写出其代码
好的,以下是二分查找算法的 C++ 代码实现:
```c++
#include <iostream>
using namespace std;
pair<int, int> binary_search(int arr[], int n, int x) {
int low = 0;
int high = n - 1;
int count = 0;
while (low <= high) {
int mid = (low + high) / 2;
count++;
if (arr[mid] == x) {
return make_pair(mid, count);
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return make_pair(-1, count);
}
int main() {
int n, x;
int arr[1000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cin >> x;
auto result = binary_search(arr, n, x);
if (result.first == -1) {
cout << -1 << " " << result.second << endl;
} else {
cout << result.first << " " << result.second << endl;
}
return 0;
}
```
其中,`arr` 表示非降序排列的整数数组,`n` 表示数组的长度,`x` 表示要查找的数。函数返回一个 `pair` 类型的结果,第一个元素为 `x` 在数组中的下标(从 0 开始),第二个元素为比较次数。
你可以将上述代码复制到你的 C++ 环境中运行,并输入相应的参数进行测试。
利用指针找数组最大值:输入n(n<=10)个整数并存入数组中,利用指针操作数组元素找出最大值,
好的,可以使用以下代码实现:
```c++
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int *p = &arr[0];
int max_val = *p;
for (int i = 1; i < n; i++) {
if (*(p + i) > max_val) {
max_val = *(p + i);
}
}
cout << "最大值为:" << max_val << endl;
return 0;
}
```
首先输入n个整数并存入数组中,然后定义一个指针p指向数组的第一个元素。接着定义变量max_val存储最大值,初始值为数组的第一个元素。然后遍历数组,如果当前元素比max_val大,则更新max_val的值。最后输出最大值即可。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)