请各位C语言高手帮忙解决下面的问题。
先从键盘上读入15个数放在一个数组a[15]中,然后再输入一个数,要求用顺序查找或折半查找法出该数是数组中第几个(从0开始计数,若有相同的数,则计首次出现的数)元素a[i...
先从键盘上读入15个数放在一个数组a[15]中,然后再输入一个数,要求用顺序查找或折半查找法出该数是数组中第几个(从0开始计数,若有相同的数,则计首次出现的数)元素a[i]的值。如果该数不在数组中,则打印出相应的提示信息。
编程可用素材:printf("please input 15 integer numbers:\n")、printf("please input the integer you want to find: ")、printf("\n%d has been found,it is a[%d]\n"...、printf("\n%d has not been found\n"...。
请各位高手将两种方法都写上吧(主函数+查找函数) 展开
编程可用素材:printf("please input 15 integer numbers:\n")、printf("please input the integer you want to find: ")、printf("\n%d has been found,it is a[%d]\n"...、printf("\n%d has not been found\n"...。
请各位高手将两种方法都写上吧(主函数+查找函数) 展开
2个回答
展开全部
#include <stdio.h>
int binary_find(int * a, int target, int low, int high)
{
if (low > high)
{
return -1;
}
else if (a[(low + high)/2] == target)
{
return (low + high)/2;
}
else if (a[(low + high)/2] > target)
{
return binary_find(a, target, low, (low + high)/2 - 1);
}
else
{
return binary_find(a, target, (low + high)/2 + 1, high);
}
}
int main()
{
int a[15] = {0};
int i = 0;
int ret = 0;
int target = 0;
printf("请输入15个数:\n");
for(i=0; i<15; i++)
{
scanf("%d", a+i);
}
printf("\n请输入要查找的数:\n");
scanf("%d", &target);
ret = binary_find(a, target, 0, 14);
if(-1 != ret)
{
printf("\n%d找到了, 它是a[%d]\n", target, ret);
}
else
{
printf("\n%d没有找到\n", target);
}
getchar();
return 0;
}
int binary_find(int * a, int target, int low, int high)
{
if (low > high)
{
return -1;
}
else if (a[(low + high)/2] == target)
{
return (low + high)/2;
}
else if (a[(low + high)/2] > target)
{
return binary_find(a, target, low, (low + high)/2 - 1);
}
else
{
return binary_find(a, target, (low + high)/2 + 1, high);
}
}
int main()
{
int a[15] = {0};
int i = 0;
int ret = 0;
int target = 0;
printf("请输入15个数:\n");
for(i=0; i<15; i++)
{
scanf("%d", a+i);
}
printf("\n请输入要查找的数:\n");
scanf("%d", &target);
ret = binary_find(a, target, 0, 14);
if(-1 != ret)
{
printf("\n%d找到了, 它是a[%d]\n", target, ret);
}
else
{
printf("\n%d没有找到\n", target);
}
getchar();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int search(int a[], int want, int len)
{
int i;
for(i = 0; i < len; i++)
{
if(a[i] == want) return(i);
}
return(-1);
}
int main
{
int i;
int want;
int index;
int a[15];
printf("please input 15 integer numbers:\n");
for(i = 0; i < 15; i++) scanf("%d", &a[i]);
printf("please input the integer you want to find: ");
scanf("%d", &want);
index = search(a, want, 15)
if(index != -1)
{
printf("\n%d has been found,it is a[%d]\n", want, index);
}
else
{
printf("\n%d has not been found\n", want);
}
}
{
int i;
for(i = 0; i < len; i++)
{
if(a[i] == want) return(i);
}
return(-1);
}
int main
{
int i;
int want;
int index;
int a[15];
printf("please input 15 integer numbers:\n");
for(i = 0; i < 15; i++) scanf("%d", &a[i]);
printf("please input the integer you want to find: ");
scanf("%d", &want);
index = search(a, want, 15)
if(index != -1)
{
printf("\n%d has been found,it is a[%d]\n", want, index);
}
else
{
printf("\n%d has not been found\n", want);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询