洛谷题--三位数排序 题目描述给出三个整数 a,b,c(0≤a,b,c≤100)要求把这三位整数从小到大排序。输入格式输入三个整数 a,b,c以空格隔开。输出格式输出一行三个整数表示从小到大排序后的结果。初始版本import java.util.Scanner; public class Main{ public static void main(String[] arg){ Scanner sc new Scanner(System.in); int a sc.nextInt(); int b sc.nextInt(); int c sc.nextInt(); if(ab){ if(bc){ System.out.println(a b c); }else{ if(ac){ System.out.println(a c b); }else{ System.out.println(c a b); } } }else{ if(bc){ System.out.println(c b a); }else{ if(ac){ System.out.println(b c a); }else{ System.out.println(b a c); } } } } }存在问题虽然逻辑正确但代码繁琐可读性差优化方案方案一依然只用if,简化版本不再写多层嵌套用临时变量交换保证 a ≤ b ≤ c代码短、逻辑直白import java.util.Scanner; public class Main{ public static void main(String[] arg){ Scanner sc new Scanner(System.in); int a sc.nextInt(); int b sc.nextInt(); int c sc.nextInt(); int t; // 如果a比b大交换a,b if (a b) { t a; a b; b t; } // 如果a比c大交换a,c → a一定是最小值 if (a c) { t a; a c; c t; } // 最后保证b ≤ c if (b c) { t b; b c; c t; } System.out.println(a b c); } }✅ 优点没有地狱式嵌套 if阅读轻松改动一个数字也不容易出错完全只用 if 判断符合分支结构作业要求方案二数组 工具类排序刷题、竞赛最优解把 3 个数放进数组调用 Java自带排序函数(局限是只能从小到大排序代码最少import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String[] arg){ Scanner sc new Scanner(System.in); int[] nums {sc.nextInt(), sc.nextInt(), sc.nextInt()}; Arrays.sort(nums); System.out.println(nums[0] nums[1] nums[2]); } }✅ 优点以后如果不是 3 个数是 100 个数几乎不用改代码逻辑干净不会写错判断分支方案三Math.max/ Math.min 纯计算版趣味写法不用交换变量直接算出最小、中间、最大值import java.util.Scanner; public class Main{ public static void main(String[] arg){ Scanner sc new Scanner(System.in); int a sc.nextInt(); int b sc.nextInt(); int c sc.nextInt(); int min Math.min(Math.min(a,b),c); int max Math.max(Math.max(a,b),c); int mid a b c - min - max; System.out.println(min mid max); } }原理三个数总和减去最大值、最小值剩下的就是中间值非常巧妙

相关新闻

最新新闻

ElevenLabs Agents 平台安装与接入全指南:CLI、JS/Python SDK、REST 与 API Key 配置(OpenMontage 实战)

ElevenLabs Agents 平台安装与接入全指南:CLI、JS/Python SDK、REST 与 API Key 配置(OpenMontage 实战)

ElevenLabs Agents 平台安装与接入全指南:CLI、JS/Python SDK、REST 与 API Key 配置(OpenMontage 实战) 【免费下载链接】OpenMontage Worlds first open-source, agentic video production system. 12 production pipelines, 100 tools, 70…

2026/9/8 16:05:17
freeCodeCamp CSS Grid 课程实战:用 align-self 实现网格项的垂直对齐

freeCodeCamp CSS Grid 课程实战:用 align-self 实现网格项的垂直对齐

freeCodeCamp CSS Grid 课程实战:用 align-self 实现网格项的垂直对齐 【免费下载链接】freeCodeCamp freeCodeCamp.orgs open-source codebase and curriculum. Learn math, programming, and computer science for free. 项目地址: https://gitcode.com/GitHub_…

2026/9/8 16:05:17
7.3.2.1初始接入阶段的随机接入的功能和作用

7.3.2.1初始接入阶段的随机接入的功能和作用

本节课程视频 7.3.2.1.1 从小区驻留到随机接入:为什么还需要“第一次上行发言”? 在上一节完成小区选择之后,UE已经找到了一个满足驻留条件的小区,并获得了必要的下行同步与系统信息。此时UE能够“听见”gNB:它能够…

2026/9/8 16:05:17
无人机摄像头图传技术全解析:模拟、WiFi、数字与4G方案对比与选型指南

无人机摄像头图传技术全解析:模拟、WiFi、数字与4G方案对比与选型指南

无人机摄像头,画面是怎么传回来的?看懂市面上的玩法再选方案把无人机飞上天之后,摄像头拍的画面还能实时出现在手机屏幕上,这事儿现在看起来稀松平常,但背后的门道真不少。说实话,我自己刚开始玩图传的时候…

2026/9/8 16:05:17
迈向通用人工智能:我们处于什么位置,又将去向何方?

迈向通用人工智能:我们处于什么位置,又将去向何方?

1. 引言 人工智能自1956年达特茅斯会议正式得名以来,已经走过了七十年的历程。从最初基于规则的简单程序,到如今能够生成流畅文本、编写代码、甚至辅助科学研究的深度学习大模型,AI的发展速度在近几年尤其令人瞩目。ChatGPT、DeepSeek、Grok等…

2026/9/8 16:05:17
2026届专科生毕业论文AI严选清单:8款实测工具与全流程提效思路

2026届专科生毕业论文AI严选清单:8款实测工具与全流程提效思路

“这十几篇初稿里,至少有六七个学生用了AI。”说这话的是老周,一位每年都要带专科毕业设计的导师。他在我面前翻了翻论文,抽出两段贴给我看,我一眼就认出了那种工整到不真实的语气:既没有数据支撑,也没有论…

2026/9/8 16:00:17