编写程序:输入三个整数,然后按由大到小的顺序输出这三个数。

方法一(直接交换)

cpp 复制代码
#include<stdio.h>
int main()
{
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c); 
	//可以这么写 想办法把a换成最大的数 ,之后是b c
	int temp;
	if(a<b)
	{
		temp=a;
		a=b;
		b=temp;
	 } 
	 if(a<c)
	 {
	 	temp=a;
		a=c;
		c=temp;
	 }
	 if(b<c)
	 {
	 	temp=b;
		b=c;
		c=temp;
	 }
	 printf("排序后的三个数为:%d %d %d\n",a,b,c);
	return 0;
}

方法二(冒泡排序思想)

cpp 复制代码
#include<stdio.h>
int main()
{
	int a[3];
	for(int i=0;i<3;i++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=0;i<3-1;i++)
	{
		for(int j=0;j<3-i-1;j++)
		{
			if(a[j]<a[j+1])
			{
				int temp=a[j];
				a[j]=a[j+1];
				a[j+1]=temp;
			}
		}
	}
	 printf("排序后的三个数为:");
	 for(int i=0;i<3;i++) printf("%d ",a[i]);
	return 0;
}

延伸到对n个数排序

这里还是使用冒泡排序的思想

cpp 复制代码
#include<stdio.h>
int main()
{
	int n,a[1005];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n-1;i++)
	{
		for(int j=0;j<n-i-1;j++)
		{
			if(a[j]<a[j+1])
			{
				int temp=a[j];
				a[j]=a[j+1];
				a[j+1]=temp;
			}
		}
	}
	 printf("排序后的%d个数为:",n);
	 for(int i=0;i<n;i++) printf("%d ",a[i]);
	return 0;
}
相关推荐
科技林总1 天前
【系统分析师】11.1 软件需求
学习
invincible_Tang1 天前
AcWing 796. 子矩阵的和 _
数据结构·算法
怪侠_岭南一只猿1 天前
爬虫学习阶段三:动态网页爬取(完整学习文档)
爬虫·python·学习
米粒11 天前
力扣算法刷题 Day 8
算法·leetcode·职场和发展
keep intensify1 天前
康复训练 2
c++
失败才是人生常态1 天前
大数据基础学习
大数据·学习
Sakinol#1 天前
Leetcode Hot 100 —— 普通数组
算法·leetcode
@Mike@1 天前
【算法】高精度
算法
leo__5201 天前
MHT多假设跟踪算法(Multiple Hypothesis Tracking)MATLAB实现
开发语言·算法·matlab
燃于AC之乐1 天前
深入解剖STL RB-tree(红黑树):用图解带入相关复杂操作实现
开发语言·c++·stl·红黑树·大厂面试·图解·插入操作