gpt4 book ai didi

c++ - 使用指针和线性搜索在数组中找到更大和最小的数字?

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

我有两个关于指针的问题。我试图自己解决这些问题,但我做不到。首先是

Find the bigger and smaller number in an array using pointers.

这是我的第一次尝试:

#include<stdio.h>
main()
{
int arr[]={8,9,5,23,12};
int min;
int i;
int *arr_pt;
int big;
min = arr[];
for(i=0;i<=5;i++)
{
if(arr[i]<min)
min = arr[i];
}
}

第二个是

Find a given number in an integer array using linear search algorithm with pointers.

我不知道应该把指针放在哪里

#include<stdio.h>
main()
{
int i;
int y;
int arr[];
int found;
for(i=0;i<5;i++){
if(arr[]==y)
found=1;
if(found==1)
printf("%d is found",y);
}
}

最佳答案

#include <algorithm>

auto minmax = std::minmax_element(std::begin(values), std::end(values));

std::cout << "min element " << *(minmax.first) << "\n";
std::cout << "max element " << *(minmax.second) << "\n";

复杂性:

最多 max(floor(3/2(N−1)), 0) 个谓词应用,其中 N = std::distance(first, last)。

C++ 很漂亮;)

关于c++ - 使用指针和线性搜索在数组中找到更大和最小的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32398968/

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