感悟:一定要小心细节,循环中注意要是否需要重新赋值
#include <stdio.h>
int main()
{
int c = 0;
int b = 0;
int n = 0;
int k = 0;
int i = 0;
int j = 0;
int max = 0;
int z = 0;
int i1 = 0;
int temp = 0;
long long int arr[100] = { 0 };
scanf("%d %d",&n,&k);
b = n;
for( i = 0 ;i < n ; i++ )
{
scanf("%d",&arr[i]);
}
while( z != k )
{
max = arr[0];
i1 = 0;
for( i = 0 ; i < n ; i++ )
{
if( arr[i] > max )
{
max = arr[i];
i1 = i;
}
}
temp = arr[i1];
arr[i1] = arr[n-1];
arr[n-1] = temp;
z++;
n = n-1;
}
for( i = 0 ; i < b ; i++)
{
if( c == 0 )
{
printf("%d",arr[i]);
c++;
}
else
{
printf(" %d",arr[i]);
}
}
return 0;
}