当前位置:网站首页>三个整数从大到小排序(详细介绍多种方法)
三个整数从大到小排序(详细介绍多种方法)
2022-07-26 22:49:00 【光光光仔】
例题:输入三个整数,按照从大到小的顺序表示出
输入:4 3 5
输出:5 4 3
(1)最简单的方法(两两进行比较)
原理是:数学的排序(两两握手的问题,n个人有(n-1+n-2+....+1)中方法)

#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int tmp = 0; //作为交换的媒介
printf("请输入三个数字:\n");
scanf("%d %d %d", &a, &b, &c);
if (a < b)
{
tmp = a;
a = b;
b = tmp;
}
if (a < c)
{
tmp = a;
a = c;
c = tmp;
}
if (b < c)
{
tmp = b;
b = c;
c = tmp;
}
printf("输出排序后的三个数字:\n");
printf("%d %d %d", a, b, c);
return 0;
}(2)使用函数的方法(原理比较交换cmp,三个数字比较)
#include<stdio.h>
void cmp(int* _a, int* _b, int* _c)//函数里对应参数为主函数实参的形参,形参是实参的临时拷贝
{
int tmp = 0;
if (*_a < *_b) //int* 是指针变量的类型 相当于 int 是 整数的类型
{
tmp = *_a; //_a =&a *_a = a *&a=a *这里的作用是解引用操作可以找到实参
*_a = *_b;
*_b = tmp;
}
if (*_a < *_c)
{
tmp = *_a;
*_a = *_c;
*_c = tmp;
}
if (*_b < *_c)
{
tmp = *_b;
*_b = *_c;
*_c = tmp;
}
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
printf("请输入三个数字:\n");
scanf("%d %d %d", &a, &b, &c);
cmp(&a, &b, &c);
printf("输出排序后的三个数字:\n");
printf("%d %d %d", a, b, c);
return 0;
}(3)函数方法(两个数字之间比较)
#include<stdio.h>
void cmp(int* px, int* py) //px,py是存储两个数的地址指针,是指针变量,指针变量就是地址
{
int z = 0; //z为暂时存储数据的场所,交换媒介
if (*px < *py) //*px 解指针操作 *px=a(a为地址存储的数字)
{
z = *px;
*px = *py;
*py = z;
}
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
printf("请输入三个数字:\n");
scanf("%d %d %d", &a, &b, &c);
cmp(&a, &b);
cmp(&a, &c);
cmp(&b, &c);
printf("输出排序后的三个数字:\n");
printf("%d %d %d", a, b, c);
return 0;
}(4)运用冒泡排序(可以但没必要)
#include<stdio.h>
int main()
{
int a[3] = { 0 };//设置数组存储三个数字,打算完成从大到小的顺序存放
int i = 0;
int j = 0;
printf("请输入三个数字:\n");
for (i = 0; i < 3; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < 2; i++) //3个数字,第一个数字需要和剩下两个数字比较,只需进行两次
{
int tmp = 0;
for (j = 0; j < 2 - i; j++)//前面已经比较了,减去前面比较的次数
{
if (a[j] < a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
printf("输出排序后的三个数字:\n");
for (i = 0; i < 3; i++)
{
printf("%d ", a[i]);
}
return 0;
}边栏推荐
- 广域网技术实验
- Test and open basic daily question brushing (continuous updating...)
- Ospf基础配置应用( 综合实验: 干涉选举 缺省路由 区域汇总 认证--接口认证)
- Experiment exercise of two-layer packaging technology (HDLC, ppp--pap\chap, GRE)
- FID index reproduction step on the pit to avoid the pit text generation image FID quantitative experiment whole process reproduction (FR é Chet inception distance) quantitative evaluation experiment s
- Golang — 解析 yaml 文件
- 通过对射式红外传感器计次实验讲解EXTI中断
- 静态路由基础配置(IP地址的规划、静态路由的配置),实现全网可达。
- (史上最详细)Codeforces Round #805 (Div. 3)E. Split Into Two Sets
- lvs+keepalived项目实战
猜你喜欢
![[volatile principle] volatile principle](/img/d4/adb4b43aaccecd506065ce838c569e.png)
[volatile principle] volatile principle

OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)
![[Database Course Design] SQLSERVER database course design (student dormitory management), course design report + source code + database diagram](/img/24/fe01d656a54086adf8a3702b82cc26.png)
[Database Course Design] SQLSERVER database course design (student dormitory management), course design report + source code + database diagram

RS-485 bus communication application

记录HandsomeBlog的star用户
![[MySQL] MySQL startup and shutdown commands and some error reports to solve problems](/img/23/b4c90604eba796692fbe049d2679d1.png)
[MySQL] MySQL startup and shutdown commands and some error reports to solve problems

HCIA (network elementary comprehensive experimental exercise)

OSPF协议知识汇总

STM32入门教程第二讲

Nb-iot access to cloud platform
随机推荐
HCIA基础知识(1)
ESP8266Wi-Fi数据通讯
Experiment of OSPF in mGRE environment
定时器中断实验
Experiment of total connection and star topology of mGRE
FID index reproduction step on the pit to avoid the pit text generation image FID quantitative experiment whole process reproduction (FR é Chet inception distance) quantitative evaluation experiment s
OSPF静态大实验
第四讲—讲解GPIO_Write函数以及相关例程
7.7 sheen Xiyin written test
Dynamic routing rip protocol experiment
(prefix and / thinking) codeforces round 806 (Div. 4) F Yet Another Problem About Pairs Satisfying an Inequality
(史上最详细)Codeforces Round #805 (Div. 3)E. Split Into Two Sets
C语言——赋值运算符、复合的赋值运算符、自增自减运算符、逗号运算符、条件运算符、goto语句、注释
C语言——while语句、dowhile语句、for循环和循环结构、break语句和continue语句
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
Tim output comparison - PWM
动态路由ofps协议配置
mgre的全连和星型拓扑实验
最新C语言入门与进阶 -史上最全最详细的C语言教程!! 第一节-总览C语言概括