藍橋杯 三個整數的排序

  • 問題描述
  • 輸入三個數,比較其大小,並從大到小輸出。
    -輸入格式
    一行三個整數。

  • 輸出格式

  • 一行三個整數,從大到小排序。
    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]+" ");
      }  
    }

}
發佈了50 篇原創文章 · 獲贊 18 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章