蓝桥杯 三个整数的排序

  • 问题描述
  • 输入三个数,比较其大小,并从大到小输出。
    -输入格式
    一行三个整数。

  • 输出格式

  • 一行三个整数,从大到小排序。
    33 88 77
  • 样例输出
  • 88 77 33
  • 程序代码:

  • 方法一:最基本的程序

import java.util.Scanner;

public class ADV_175 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();

        int t;
        if(a>b) {//找出a,b之间的最大值,赋给b
            t = a;
            a = b;
            b = a;
        }

        if(a>c) {//找出a,c之间的最大值,赋给c
            t = a;
            a = c;
            c = a;
        }

        if(b>c) {//找出b,c之间的最大值,赋给c
            t = b;
            b = c;
            c = t;
        }

        System.out.print(c+" ");
        System.out.print(b+" ");
        System.out.print(a);

    }

}
  • 方法二:用到java函数
import java.util.Arrays;
import java.util.Scanner;

public class ADVV_175 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] a = new int[3];
        for(int i = 0;i<3;i++) {
            a[i] = sc.nextInt();
        }
        Arrays.sort(a);//将数组降序排列,输出的时候逆序就可以了

      for(int i = 2;i>=0;i--) {//逆序输出数组,使其按照从大到小输出
          System.out.print(a[i]+" ");
      }  
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_41862755/article/details/79762696