思路:题目理解了很久。。。。(调用了两个快排,只是为了熟悉下快排怎么用)
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void swp(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
return;
}
void quick_sort(int *a,int low,int high)
{
int i;
int j;
int base;
i=low;
j=high;
if(low>high)
return;
base=a[low];
while(i<j)
{
while(i<j)
{
if(a[j]<base)
break;
j--;
}
while(i<j)
{
if(a[i]>base)
break;
i++;
}
if(i<j)
swp(&a[i],&a[j]);
if(i==j)
swp(&a[low],&a[i]);
}
quick_sort(a,low,i-1);
quick_sort(a,j+1,high);
return;
}
void sort(float *A,int left,int right)
{
if(left>=right) return;
float x=A[(left+right)>>1];
int low=left;
int high=right;
while(low<high)
{
while(A[low]>x)
low++;
while(A[high]<x)
high--;
if(low<=high)
{
float Temp=A[low];
A[low]=A[high];
A[high]=Temp;
low++;
high--;
}
}
sort(A,left,high);
sort(A,low,right);
}
int main(int argc, char *argv[]) {
float a[500][500];
int n,m,k;
int i,j;
float b[500];
float c[500];
int x;
int end[500];
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
a[i][j]=0.0;
memset(b,0.0,sizeof(b));
memset(b,0.0,sizeof(c));
x=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%f",&a[i][j]);
}
}
for(j=1;j<=m;j++)
{
for(i=1;i<=n;i++)
{
b[j]+=a[i][j];
}
}
for(j=1;j<=m;j++)
c[j]=b[j];
sort(b,1,m);
for(i=1;i<=k;i++)
{
for(j=1;j<=m;j++)
{
if(b[i]==c[j])
{
end[x]=j;
x++;
c[j]=-1;
break;
}
}
}
quick_sort(end,0,x-1);
for(i=x-1;i>=0;i--)
{
if(i!=0)
printf("%d ",end[i]);
else
printf("%d",end[i]);
}
printf("\n");
}
return 0;
}