c语言快速查找函数 c语言中查找函数

C语言查找的用法#include stdio.h
#include stdlib.h
#include time.h
#define N 20
void Sort(int a[],int n){
int i,j,k,t;
for(i = 0; in - 1;i) {
k = i;
for(j = i1; jn;j) {
if(a[k]a[j]) k = j;
}
if(i != k) {
t = a[k];
a[k] = a[i];
a[i] = t;
}
}
}
int Find(int a[],int n,int x) {
int low = 0,high = n - 1,mid;
while(low = high) {
mid = (lowhigh)/2;
if(x == a[mid]) return mid;
else if(xa[mid]) low = mid1;
else high = mid - 1;
}
return -1;
}
void Show(int a[],int n) {
int i;
for(i = 0; in;i) {
printf("%d ",a[i]);
}
printf("\n");
}
int main() {
【c语言快速查找函数 c语言中查找函数】int a[20],i,x,res;
srand((unsigned)time(NULL));
for(i = 0; iN;i)
a[i] = rand()%N; // 每个数都在1 -- 100之间
Show(a,N);
Sort(a,N);
Show(a,N);
x = rand()01;
res = Find(a,N,x);
if(res = 0) printf("数值苡镅钥焖俨檎液南卤晡?d 。\n",a[res],res);
else printf("数列中没有找到数值%d 。\n",x);
return 0;
}
C语言查找函数首先,指针默认只有一个地址的长度的空间,存不了那么多的字符 。
其次 , 传值和传地址一塌糊涂 , 完全就是乱写么 。
scanf的第二个参数是地址 , 如果本身不是指针则需要取址符 。
我给你重写了算了
#includestdio.h
int Retrieve(int array[],int length,int x);/*形参x直接传值就可以了*/
int main()
{
int *a,x;
int length,i;
scanf("%d",length);
a = (int)malloc(sizeof(int)*length); /*申请一个内存空间用来存放数据,或者直接用一个大数组也可以*/
求c语言大神 题目:编写查找函数search(),实现如下功能 。C代码和运行结果如下:
输入5,成功输出了其在给定数组中的下标为4 , 结果正确,望采纳~
附源码:
#include stdio.h
int search(int a[], int n, int x) { // 返回数组a[]中x的下标
int i;
for (i = 0; in; i) {
if (a[i] == x)
return i;
}
return -1; // 数组中没有x则返回-1
}
int main() {
int x, a[10] = {7,9,3,4,5,2,1,6,10,8};
scanf("%d", x); // 用户输入数x
printf("%d ", search(a, 10, x)); // 输出x在数组中的下标
return 0;
}
C语言数组的查找函数#includestdio.h
int main()
{
int a[5];
int i,max,min;
printf("input number:\n");
for(i=0;i5;i)
scanf("%d",a[i]);
max=a[0];
min=a[0];
for(i=0;i5;i){
if(a[i]max)
max=a[i];
}
for(i=0;i5;i){
if(a[i]min)
min=a[i];
}
for(i=0;i5;i){
printf("%d",a[i]);
printf("");
}
printf("\n");
printf("最大值为%d\n",max);
printf("最小值为%d\n",min);
return 0;
}
c语言函数find的使用方法c语言find函数c语言快速查找函数的用法详解
C语言之find()函数
find函数用于查找数组中c语言快速查找函数的某一个指定元素的位置 。
比如c语言快速查找函数:有一个数组[0, 0, 5, 4, 4];
问:元素5的在什么位置,find函数 返回值 为 2;
find (数组名起始查找元素的位置,数组名结束查找的元素位置, 想要查找的元素)
直接上代码:
#include iostream
#include vector
#include algorithm//注意要包含该头文件
using namespace std;
int main()
{
int nums[] = { 3, 1, 4, 1, 5, 9 };
int num_to_find = 5;
int start = 0;
int end = 5;
int* result = find( numsstart, numsend, num_to_find );
if( result == numsend )
{
cout "Did not find any number matching "num_to_findendl;
}
else
{
cout "Found a matching number: "*resultendl;
}
return 0;
}
C语言如何用函数来实现搜索#include stdio.h
int search(int a[],int b,int c,int i)
{
int x,y,z;
x=i 1;
z=b-1;
y=(x z)/2;
while(x=z)
{
if(a[y]c)
{
z=y-1;
y=(x z)/2;
continue;
}
if(a[y]c)
{
x=y 1;
y=(x z)/2;
continue;
}
return y 1;
}
return -1;
}
int main()
{
int i,m,pos;
scanf("%d",m);
int a[m];
for(i=0;im;i)
{
scanf("%d",a[i]);
}
for(i=0;im;i)
{
pos=search(a,m,a[i],i);
if(pos!=-1)
{
printf("FOUND a[%d]=%d, position is %d\n",i,a[i],i 1);
return 0;
}
}
if(pos==-1)
{
printf("NOT FOUND\n");
}
return 0;
}
这种查找方法的数组必须是从小到大的,用遍历的话就没这个问题了 。
c语言快速查找函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言中查找函数、c语言快速查找函数的信息别忘了在本站进行查找喔 。

    推荐阅读