【每天一道编程系列-2018.2.9】(Ans)

2019-03-14 12:55:56 浏览数 (1)

【题目描述】

Implementation of a given order of 4 integers from large to small

【题目翻译】

编写程序实现对给定的 4 个整数从大到小的顺序排列

【答案】

代码语言:javascript复制
/**
 * @author: yesr
 * @date: 2018.2.9
 */

import java.util.Scanner;

public class Test0209 {
    private static int number=4;        //输入4个数存放在数组中                   
    private static int[] t1 = new int[number];
    public static void main(String[] args) {
        Test0209 obj = new Test0209();
        obj.sort();
    }
    void sort(){
        System.out.println("请输入4个数:");
        Scanner in_t1 = new Scanner(System.in);//循环输入数组
        for(int i=0;i<number;i  ){
            t1[i]=in_t1.nextInt();}
        for (int i = 0; i < t1.length; i  ) {
            int pos = i;
            for (int j = i   1; j < t1.length; j  ) {
                if (t1[pos] > t1[j])
                    pos = j;
            }
            if (pos != i) {
                t1[i] = t1[i]   t1[pos];
                t1[pos] = t1[i] - t1[pos];
                t1[i] = t1[i] - t1[pos];
            }
        }

        for (int i = t1.length - 1; i >= 0; i--)
            System.out.print(t1[i]   "t");
    }
}

0 人点赞