Tasks and code:
/********************************************************** File NAME:MIN.C
Author: Lijie date:2016-12-15
Description: Find the smallest k element
Title: Enter n integers, output the smallest K.
For example, enter 1,2,3,4,5,6,7 and 8 of these 8 digits,
The smallest 4 digits are 1,2,3 and 4.
************************************************************/
#include <stdio.h>
int main ()
{
int a[10] = {0};
int i = 0;
int j = 0;
int n;
printf ("Please input the values of n:\n");
scanf ("%d", &n);
int k;
printf ("Please input the values of k:\n");
scanf ("%d", &k);
int m = n;
printf ("Please input n number: \ n");
while (m>0)
{
scanf ("%d", &a[i]);
m--;
i++;
}
int temp;
for (i=0; i<n-1; i++)
{
For (j=0 j<n-1-i; j + +)
{
if (a[j]>a[j+1])
{
temp = A[j];
A[J] = a[j+1];
A[J+1] = temp;
}
}
}
printf ("The smallest number of K: \ n");
for (i=0; i<k; i++)
{
printf ("%4d", A[i]);
}
printf ("\ n");
return 0;
}
Run Result:
Summary: In fact, it is a bubble sort, and then output the smallest number of K