Java实现P5715 【深基3.例8】三位数排序

在这里插入图片描述

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    
    
	@SuppressWarnings({
    
     "resource"})
	public static void main(String[] args) throws IOException{
    
    
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt();
		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;
		}
		System.out.println(a+" "+b+" "+c);
	}
}

思路:由于只有三个,就不用冒泡排序等对于一堆数据进行排序的方法了。
关键点:注意顺序。先a和b排,再a和c排,确保a是最小的,然后比较b和c,这样就省时间了。

猜你喜欢

转载自blog.csdn.net/jinyeran/article/details/115570053