#include <stdio.h>
typedef int a[];
int kp(int *a,int begin,int end,int k)
{
int i=begin;
int j=end;
int temp=a[begin];
while(i<j)
{
while(i<j&&temp>a[j])
{
j--;
}
a[i]=a[j];
while(i<j&&temp<a[i])
{
i++;
}
a[j]=a[i];
}
temp=a[i];
if(i==k-1)
{
return a[i];
}
else if(i>k-1)
{
kp(a,begin,i-1,k);
}
else if(i<k-1)
{
kp(a,i+1,end,k-i);
}
}
int main(void)
{
int a[10]={12,31,21,17,14,6,8,4,15,19};
int i=0,j=9,x;
for(i=0;i<=j;i++)
{
printf("%d\t",a[i]);
}
printf("请输入你想找的第k大的数\n");
scanf("%d",&x);
printf("%d",kp(a,i,j,x));
}
printf("%d",kp(a,i,j,x));运行到这里的时候,i和j的值都是9,因为上面经过了for循环,所以你起始位置和终止位置都是9,出的结果肯定是错误的。
如有帮助,请采纳一下,谢谢。