当前位置:网站首页>[深入浅出]三位数排序
[深入浅出]三位数排序
2022-08-03 11:36:00 【ceshyong】
题目链接【深基3.例8】三位数排序 - 洛谷
https://www.luogu.com.cn/problem/P5715
题目描述
给出三个整数a,b,c(0≤a,b,c≤100),要求把这三位整数从小到大排序。
输入
输入三个整数 a,b,ca,b,c,以空格隔开。
输出格式
输出一行,三个整数,表示从小到大排序后的结果。
样例组
样例1
输入 1 14 5
输出 1 5 14
样例2
输入 2 2 2
输出 2 2 2题目解析
这道题目有三种做法。
第一种做法是将每个数都存入A数组,然后用sort()将每个数排序就可以了。(呃......)
第二种做法则借助于max和min。读入三个数,然后输出三个数中的最小值。然后用if逐个判断那个数是不是三个数中的最小值,并将其改为三个数中任意两个数中的一个数。最后输出它们中的最大值和最小值。
第三种做法则需要使用swap()函数。如果A比B小,交换A,B;如果B比C小,交换B,C;如果A比B小,交换A,B;最后输出A,B,C就行了。
AC代码
第一种做法:
#include<bits/stdc++.h>
using namespace std;
int a[4];
int main()
{
cin>>a[1]>>a[2]>>a[3];
sort(a+1,a+4);
cout<<a[1]<<' '<<a[2]<<' '<<a[3];
return 0;
}第二种做法:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c,d;
cin>>a>>b>>c;d=min(min(a,b),c);
cout<<d<<' ';
if(d==a) a=b;
else if(d==b) b=c;
else c=a;
cout<<min(min(a,b),c)<<' '<<max(max(a,b),c);
return 0;
}第三种做法:
#include<bits/stdc++.h>
using namespace std;
int a,b,c;
int main()
{
cin>>a>>b>>c;
if(a<b) swap(a,b);
if(b<c) swap(b,c);
if(a<b) swap(a,b);
cout<<c<<' '<<b<<' '<<a;
return 0;
}这道题目就这么多。涨粉一个,更新一篇!
边栏推荐
- LeetCode刷题笔记:622.设计循环队列
- hystrix 服务熔断和服务降级
- QGIS绘制演习区域示意图
- 当前页面的脚本发生错误如何解决_电脑出现当前页面脚本错误怎么办
- 【HCIP持续更新】STP协议相关保护机制
- The effects of the background and the Activiti
- 【MySQL功法】第4话 · 和kiko一起探索MySQL中的运算符
- For invoice processing DocuWare, cast off the yoke of the paper and data input, automatic processing all the invoice received
- 【一起学Rust】Rust的Hello Rust详细解析
- OFDM 十六讲 4 -What is a Cyclic Prefix in OFDM
猜你喜欢

MySQL - 2059 - Authentication plugin ‘caching_sha2_password‘ cannot be loaded

Babbitt | Metaverse daily must-read: Players leave, platforms are shut down, and the digital collection market is gradually cooling down. Where is the future of the industry?...

性能优化|从ping延时看CPU电源管理

OFDM 十六讲 4 -What is a Cyclic Prefix in OFDM
![[Star Project] Little Hat Plane Battle (9)](/img/e3/c7d2728080bcdccc181a7e5c50ee6f.png)
[Star Project] Little Hat Plane Battle (9)

笔试题:金额拆分

第四周学习 HybridSN,MobileNet V1,V2,V3,SENet

劝退背后。

【冒泡排序以及奇数偶数排列】

深度学习跟踪DLT (deep learning tracker)
随机推荐
ThreadLocal源码解析及使用场景
【MySQL】数据库进阶之索引内容详解(上篇 索引分类与操作)
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
899. 有序队列 : 最小表示法模板题
云原生 Dev0ps 实践
智能日报脚本
字节最爱问的智力题,你会几道?
一个扛住 100 亿次请求的红包系统,写得太好了!!
「全球数字经济大会」登陆 N 世界,融云提供通信云服务支持
深度学习跟踪DLT (deep learning tracker)
Generate interface documentation online
[LeetCode—Question 2 Sum of Two Numbers Detailed Code Explanation ] The source code is attached, which can be copied directly
ETL data cleaning case in MapReduce
bash case用法
html+css+php+mysql实现注册+登录+修改密码(附完整代码)
The way of programmer architecture practice: how to design a sustainable evolution system architecture?
距LiveVideoStackCon 2022 上海站开幕还有3天!
The effects of the background and the Activiti
缓存--伪共享问题
【冒泡排序以及奇数偶数排列】