当前位置:首页 » 《随便一记》 » 正文

[C语言]实训考前这几个函数得会_某新手的博客

16 人参与  2022年04月09日 10:01  分类 : 《随便一记》  评论

点击全文阅读


#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("%3d\n",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;
}

注:排序修改成从小到大和大到小排序只用修改一个大于小于号就可以了


点击全文阅读


本文链接:http://zhangshiyu.com/post/37584.html

排序  素数  修改  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1