Day60:求5个数的最值和ASCII码排序

135 阅读1分钟

Day09 2023/03/08

难度:简单

题目1

设计一个从5个整数中取最小数和最大数的程序

输入 :
输入只有一组测试数据,为五个不大于1万的正整数

输出 :
输出两个数,第一个为这五个数中的最小值,第二个为这五个数中的最大值,两个数字以空格格开。

static int[] max(int[] text){
    int ans[] = new int[2];
    ans[0] = text[0];
    ans[1] = text[0];
    for (int i = 0;i < text.length;i++){
        ans[0] = ans[0] > text[i] ? ans[0] : text[i];
        ans[1] = ans[1] < text[i] ? ans[1] : text[i];
    }
    return ans;
}

题目2

输入三个字符(可以重复)后,按各字符的ASC码从小到大的顺序输出这三个字符。

输入:
第一行输入一个数N,表示有N组测试数据。后面的行输入多组数据,每组输入数据都是占一行,由三个字符组成,之间无空格。

输出:
对于每组输入数据,输出一行,字符中间用一个空格分开。

static String sortByAsii(char[] txt){
    if (txt.length != 3)return "字符不符合要求";
    String ans = "";
   if(txt[0] - txt[1] < 0 & txt[0] - txt[2] < 0){
        ans += txt[0];
        if (txt[1] - txt[2] < 0){
        ans += txt[1];
        ans += txt[2];
        }
        else {
        ans += txt[2];
        ans += txt[1];
        }
   }
   else if (txt[1] - txt[0] < 0 & txt[1] - txt[2] < 0) {
       ans += txt[1];
       if (txt[0] - txt[2] < 0){
           ans += txt[0];
           ans += txt[2];
       }
       else {
           ans += txt[2];
           ans += txt[0];
       }
   }else {
       ans += txt[2];
       if (txt[1] - txt[0] < 0){
           ans += txt[1];
           ans += txt[0];
       }
       else {
           ans += txt[0];
           ans += txt[1];
       }
   }
   return ans;
}