[C语言]实训考前这几个函数得会

``````#include <stdio.h>

int isPrime(int x)
{
int i;
for(i = 2 ; i <= x/2 ; i++)
if(x %i == 0)
return 0;
return 1;
}

void buble_sort(int a[],int n)
{
int i,j,temp;
for(i = 0 ; i < n - 1; i++)
for(j = 0 ; j < n - i - 1; j++)
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}

}

void Selection_sort(int a[],int n)
{
int i,j,min,temp;
for(i = 0 ; i < n - 1; i++)
{
min = i;
for(j = i + 1; j < n ; j++)
if(a[j] < a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}

int run(int n)
{
if(n % 4 == 0 && n % 100 !=0 || n % 400 == 0)
return 1;
else
return 0;
}

int main()
{
int i,count = 1;
int a[10] = {2,4,3,6,3,7,4,8,5,3};
int b[10] = {2,4,3,6,3,7,4,8,5,3};
int n = sizeof(a)/sizeof(a[0]);

//100以内的素数 10个一排
for(i = 2; i < 100 ; i++)
if(isPrime(i))
{
if(count % 10 != 0)
printf("%3d ",i);
else
printf("%3dn",i);
count++;
}
putchar('n');

//冒泡和选择排序
buble_sort(a,n);

for(i = 0 ; i < n; i++)
printf("%d ",a[i]);
putchar('n');

Selection_sort(b,n);

for(i = 0 ; i < n; i++)
printf("%d ",b[i]);
putchar('n');

//判断闰年  10个一组输出
count = 0;
for(i = 1 ; i < 2021 ; i++)
{
if(run(i))
{
printf("%5d",i);
count++;
if(count % 10 == 0)
printf("n");
}
}
putchar('n');
return 0;
}
``````

THE END