版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014186096/article/details/49532097
第一题:查找
有一个数组a[n],数组中的数据是一个无规律的递增数,请写出一个函数用最高效的方式判段关键字num是否在这个数组中。如果存在则返回该数所在数组中的位置,如果不存在则返回0XFFFFFFFF(假定数组长度小于这个值)。
例如:
Int a[]={1,2,3,4,555,666,888,......,444444};
FindNumber(a,sizeof(a)/sizeof(a[0]),3);返回值为2.
解析:
/*斐波那契查找*/
int F(int i)
{
if(i<2)
return i==0? 0:1;
return F(i-1)+F(i-2);
}
int Fibonacci_Search(int *a,int n,int key)
{
int low=1,high=n,mid,i,k=0;
while(n>F(k)-1) /*计算n位于斐波那契数列的位置*/
k++;
for(i=n;i<F(k)-1;i++) /*将不满的数值补全*/
a[i]=a[n];
while(low<=high)
{
mid=low+F(k-1)-1;
if (key<a[mid])
{
high=mid-1;
k=k-1;
}
else if (key>a[mid])
{
low=mid+1;
k=k-2;
}
else
{
if(mid<=n)
return mid;
else
return n;
}
}
return -1;
}
int main(void)
{
int a[100]={0,1,16,24,35,47,59,62,73,88,99};
int fibonacci_i=Fibonacci_Search(a,11,59);
printf("fibonacci_i=%d\n",fibonacci_i);
return 0;
}
第二题:查找
写一段程序,找出数组中第K大大小的数,输出数所在的位置。例如{2,4,3,4,7}中,第一大的数是7,位置在4.第二大、第三大的数都是4,位置在1、3随便输出哪一个均可。
#include<stdio.h>
int find_orderk(int *narry,const int n,const int k)
{
int s[10];
int t=0;
for(int i=0;i<k;i++)
{
for(int j=0;j<n;j++)
{
if(narry[i]<narry[j])
{
int temp=narry[i];
narry[i]=narry[j];
narry[j]=temp;
s[t]=j;
}
}
t++;
}
return s[k-1];
}
int main()
{
int a[]={2,4,3,4,7};
int t =find_orderk(a,5,4);
printf("%d\n",t);
}
(第二种方法:)
利用构建大顶堆的方法,先定义一个结构体,包括当前数据值data,及数组中的位置place,之后开始构建堆。
typedef struct Node
{
int data;
int place;
}Node;
void createNode(Node n[],const int *a,const int size)
{
for(int i=0;i<size;i++)
{
n[i].data = a[i];
n[i].place = i+1;
}
}
void HeapAdjust(Node a[],int i,int size)
{
int lchild = i*2;
int rchild = i*2+1;
int max = i;
if(i<=size/2)
{
if(a[max].data <a[lchild].data && lchild<=size)
max = lchild;
if(a[max].data <a[rchild].data && rchild<=size)
max = rchild;
if(max!=i)
{
swap(a[i],a[max]);
HeapAdjust(a,max,size);
}
}
}
int find_orderk(const int* narry,const int n,const int k)
{
Node a[n];
createNode(a,narry,n);
int i=0;
for(i=n/2;i>=0;--i)
HeapAdjust(a,i,n);
int j=1;
for(i=n;i>=0;--i,++j)
{
swap(a[0],a[i]);
if(j==k)
{
cout<<a[i].place<<endl;
return a[i];
}
HeapAdjust(a,0,i-1);
}
}
第3种方法:
//快速排序
#include <iostream>
using namespace std;
int Partition (int*L,int low,int high)
{
int temp = L[low];
while (low < high)//完成一趟排序
{
while (low<high&&L[high]>=temp)
--high;
L[low] = L[high];//比括支点小的移到低台端
while (low < high && L[low] <= temp)
++low;
L[high] = L[low];//比括支点大的移到高端
}//终止循环之后low和high一定相等
L[low] = temp;//支点
return low;
}
void QSort (int*L,int low,int high)
{
if (low < high)
{
int pl = Partition (L,low,high);
QSort (L,low,pl - 1);
QSort (L,pl + 1,high);
}
}
int main ()
{
int narry[100],addr[100];
int sum = 1,t;
cout << "Input number:" << endl;
cin >> t;
while (t != -1)//输入-1则停止输入数组元素
{
narry[sum] = t;
addr[sum - 1] = t;
sum++;
cin >> t;
}
sum -= 1;
QSort (narry,1,sum);
for (int i = 1; i <= sum;i++)
cout << narry[i] << '\t';
cout << endl;
int k;
cout << "Please input place you want:" << endl;
cin >> k;
int aa = 1;
int kk = 0;
for (;;)
{
if (aa == k)
break;
if (narry[kk] != narry[kk + 1])//找到第一个开始重复的数字的位置
{
aa += 1;
kk++;
}
}
cout <<"The NO." << k << "number is:" << narry[sum - kk] << endl;
cout << "And it's place is:" ;
for (int i = 0;i < sum;i++)
{
if (addr[i] == narry[sum - kk])
cout << i << '\t';
}
return 0;
}
第三题:查找
写一个函数,它的原型是:
Int find_alphabet_string(char *inputstr,char *outputstr)
功能:在字符串中找出连续最长的英文字母串(26个英文字母,不区分大小写),把这个字母串的长度作为函数值返回,并把这个字母串拷贝给函数参数outputstr所指的内存。
#include<stdio.h>
#include<string.h>
int find_alphabet_string(char *inputstr,char *outputstr)
{
char *head=NULL;
int maxnum=0,count=0;
while(*inputstr!=NULL)
{
if(((*inputstr>'a')&&(*inputstr<'z'))||((*inputstr>'A')&&(*inputstr<'Z')))
{
count++;
}
else if(count>maxnum)
{
{
maxnum=count;
head=inputstr-maxnum;
}
count=0;
}
inputstr++;
}
outputstr[maxnum]=0;
for(int i=0;i<maxnum;i++)
*(outputstr++)=*(head++);
return maxnum;
}
int main()
{
int result;
char intputstr[100],outputstr[100];
while (printf("Please input a string: "),fgets(intputstr,sizeof(intputstr),stdin)!=NULL) {
result=find_alphabet_string(intputstr,outputstr);
printf("The result is : %d %s\n\n",result,outputstr);
}
return 0;
}
第四题:递归
用递归判断数组a[N]是否为一个递增数组
Bool fun(int *a,int n)
{
if(n==1)
return true;
If(n==2)
return a[n-2]<=a[n-1];
return fun(a,n-1)&&(a[n-1]>=a[n-2]);
}
第五题:递归
请实现递归函数将一个字符串反转(字符串中最大长度为128)。
#include<stdio.h>
#include<string.h>
char *reverse(char *p)
{
if(!p)
return NULL;
int len=strlen(p);
if(len>1)
{
char temp = p[0];
p[0]=p[len-1];
p[len-1]='\0';
reverse(p+1);
p[len-1]=temp;
}
return p;
}
int main()
{
char src[] = {"abcdef"};
char *pdest = reverse(src);
// puts(pdest);
printf("%s\n",pdest);
return 0;
}
第六题:递归
递归函数sum(int a[],int n)的返回值是数组a[]的前n个元素之和。
#include<stdio.h>
int sum(int a[],int n)
{
if(n>0)
return sum(a,n-1)+a[n-1];
else return 0;
}
int main()
{
int a[]={1};
int b[]={1,2,3,4,5};
//int c[0]={};
printf("%d,%d\n",sum(a,1),sum(b,5));
}
第七题:排序
用指针实现冒泡法。
#include<stdio.h>
#include<string.h>
void Bubblesort_poniter(int *s,int n)
{
int *p;
for(int i=0;i<n;i++)
for(p=s;p<s+n-i-1;p++)
{
if(*p<*(p+1))
{
int temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
}
}
int main()
{
int s[]={1,33,5,7,2,3,45,21,6,0,10};
int len=sizeof(s)/sizeof(s[0]);
Bubblesort_poniter(s,len);
for(int j=0;j<len;j++)
printf("%5d",s[j]);
printf("\n");
}
第八题:排序
将两个有序数组归并为一个有序数组。
#include<stdio.h>
#include<string.h>
void MergeSort(int *s,int n,int *p,int m,int *c)
{
int i,j;
for(i=0,j=0;i<n&&j<m;)
*(c++)=((s[i]<p[j])?s[i++]:p[j++]);
while(i<n)
*(c++)=s[i++];
while(j<m)
*(c++)=p[j++];
}
int main()
{
int s[]={1,3,4,7,9,13,44};
int p[]={5,12,13,21,45};
int n=sizeof(s)/sizeof(s[0]);
int m=sizeof(p)/sizeof(p[0]);
int *c=new int[n+m];
MergeSort(s,n,p,m,c);
for(int i=0;i<n+m;i++)
printf("%4d",c[i]);
printf("\n");
}