问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

求解一道Java数组排序方面的题目

发布网友 发布时间:2022-09-25 03:28

我来回答

3个回答

热心网友 时间:2023-09-15 06:19

import java.util.Scanner;

public class Test {


    public static void main(String[] args) {
    int[] array = new int[1000];
        for (int i = 0; i < 1000; i++) {
            array[i] = i;
        }
        boolean loopFlag = true;

        while(loopFlag) {
            System.out.println("请输入字符:");
            Scanner sc = new Scanner(System.in);
            String result = sc.nextLine();
            if ("A".equalsIgnoreCase(result)) {
                array = sort(array, result);
                print(array);
            } else if ("B".equalsIgnoreCase(result)) {
                array = sort(array, result);
                print(array);
            } else if ("C".equalsIgnoreCase(result)) {
                array = sort(array, result);
                print(array);
            } else {
                System.out.println("你输入的不合法,请重新输入...");
            }
            System.out.println("按Y/y继续,按N/n退出?");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            if ("N".equalsIgnoreCase(input)) {
                loopFlag = false;
                System.out.println("退出成功!");
            }
        }
    }

    /**
     * @param a
     * @param b
     * @param type 比较类型
     * @return 若a>b 返回true
     */
    public static boolean compare(int a, int b, String type) {
        int hundredsDigitA = a / 100;
        int tenDigitA = a % 100 / 10;
        int singleDigitA = a % 100 % 10;

        int hundredsDigitB = b / 100;
        int tenDigitB = b % 100 / 10;
        int singleDigitB = b % 100 % 10;

        if("B".equalsIgnoreCase(type)) {
            // 十位>个位>百位
            if (tenDigitA > tenDigitB) {
                return true;
            } else if(tenDigitA < tenDigitB){
                return false;
            } else {
                if (singleDigitA > singleDigitB) {
                    return true;
                } else if(singleDigitA < singleDigitB){
                    return false;
                } else {
                    if (hundredsDigitA > hundredsDigitB) {
                        return true;
                    } else if(hundredsDigitA < hundredsDigitB){
                        return false;
                    } else {
                        // a,b相等,返回true/false都可以,基本上不会走到这一步
                        return true;
                    }
                }
            }
        } else if ("C".equalsIgnoreCase(type)) {
            // 个位>百位>十位
            if (singleDigitA > singleDigitB) {
                return true;
            } else if(singleDigitA < singleDigitB){
                return false;
            } else {
                if (hundredsDigitA > hundredsDigitB) {
                    return true;
                } else if(hundredsDigitA < hundredsDigitB){
                    return false;
                } else {
                    if (tenDigitA > tenDigitB) {
                        return true;
                    } else if(tenDigitA < tenDigitB){
                        return false;
                    } else {
                        // 相等,返回true/false都可以,基本上不会走到这一步
                        return true;
                    }
                }
            }
        } else {
            // 百位>十位>个位
            if (hundredsDigitA > hundredsDigitB) {
                return true;
            } else if(hundredsDigitA < hundredsDigitB){
                return false;
            } else {
                if (tenDigitA > tenDigitB) {
                    return true;
                } else if(tenDigitA < tenDigitB){
                    return false;
                } else {
                    if (singleDigitA > singleDigitB) {
                        return true;
                    } else if(singleDigitA < singleDigitB){
                        return false;
                    } else {
                        // 相等,返回true/false都可以,基本上不会走到这一步
                        return true;
                    }
                }
            }
        }
    }

    /**
     *
     * @param array 排序数组
     * @param type  排序类型,传入字符串"A"、"B"、"C"
     * @return
     */
    public static int[] sort(int[] array, String type) {
        int length = array.length;
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length - 1; j++) {
                if (!compare(array[j], array[j+1], type)) {
                    // 如果前面的数小于后面的数,就换位
                    int temp;
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
        return array;
    }
    public static void print(int[] array) {
        for (int number : array) {
            System.out.print(number + ">");
        }
        System.out.println();
    }
}

热心网友 时间:2023-09-15 06:20

import java.util.*;
import java.util.stream.*;
/**
 * @author hardneedl
 */
public class SortNumsDemo {
    public static void main(String... args) {
        //初始化整数的集合体
        List<Integer> nums = new ArrayList<>();
        for (int i = 0; i < 1000; i++) nums.add(i);

        //A方案的排序
        Comparator<Integer> a_comparator = Integer::compareTo;

        //B 方案的排序
        Comparator<Integer> b_comparator = (o1, o2) -> {
            //取百位上的值
            int h1 = o1 / 100, h2 = o2 / 100;

            //取 “十位” 上的值
            int i1 = (o1 - h1 * 100) / 10;
            int i2 = (o2 - h2 * 100) / 10;

            if (i1 > i2) return 1;
            if (i1 < i2) return -1;

            // 取个位
            int l1 = o1 - 100 * h1 - 10 * i1;
            int l2 = o2 - 100 * h2 - 10 * i2;

            if (l1 > l2) return 1;
            if (l1 < l2) return -1;

            if (h1 > h2) return 1;
            if (h1 < h2) return -1;

            return 0;
        };

        //c 方案的排序
        Comparator<Integer> c_comparator = (o1, o2) -> {
            //取百位上的值
            int h1 = o1 / 100, h2 = o2 / 100;

            //取 “十位” 上的值
            int i1 = (o1 - h1 * 100) / 10;
            int i2 = (o2 - h2 * 100) / 10;

            // 取个位
            int l1 = o1 - 100 * h1 - 10 * i1;
            int l2 = o2 - 100 * h2 - 10 * i2;

            if (l1 > l2) return 1;
            if (l1 < l2) return -1;

            if (h1 > h2) return 1;
            if (h1 < h2) return -1;

            if (i1 > i2) return 1;
            if (i1 < i2) return -1;

            return 0;
        };

        Stream<Integer> a_stream = nums.stream().sorted(a_comparator.reversed());
        System.out.println("A 方案排序:");
        a_stream.forEach(i -> System.out.printf("%d,", i));

        System.out.println("\r\nB方案排序:");
        Stream<Integer> b_stream = nums.stream().sorted(b_comparator.reversed());
        b_stream.forEach(i -> System.out.printf("%d,", i));

        System.out.println("\r\nC方案排序:");
        Stream<Integer> c_stream = nums.stream().sorted(c_comparator.reversed());
        c_stream.forEach(i -> System.out.printf("%d,", i));
    }
}

热心网友 时间:2023-09-15 06:20

用的冒泡排序法

package sort;

import java.util.Random;
import java.util.Scanner;

public class Sort {
public static void main(String[] args){

Scanner scanner=new Scanner(System.in);
System.out.println("输入ABC");
String input=scanner.nextLine();

//默认A排序
char type='A';
if(input.equals("B")){
type='B';
}else if(input.equals("C")){
type='C';
}

//定义、初始化要排序的数组,并输出数组
int[] array=new int[10];
Random random=new Random();
System.out.print("排序前数组:");
for(int i=0;i<array.length;i++){
array[i]=random.nextInt(1000);
System.out.print(array[i]+" ");
}
System.out.println();

//从大到小冒泡排序
if(type=='A'){
for(int x=array.length-1;x>0;x--){
for(int y=0;y<x;y++){
if(array[y]<array[y+1]){
int temp=array[y];
array[y]=array[y+1];
array[y+1]=temp;
}
}
}
//十位→个位→百位冒泡排序
}else if(type=='B'){
for(int x=array.length-1;x>0;x--){
for(int y=0;y<x;y++){
//取出百位h,十位t,个位g
int h1=array[y]/100;
int t1=(array[y]/10)%10;
int g1=array[y]%10;

int h2=array[y+1]/100;
int t2=(array[y+1]/10)%10;
int g2=array[y+1]%10;

if(t1<t2||(t1==t2 && g1<g2)||(t1==t2 && g1==g2 && h1<h2)){
int temp=array[y];
array[y]=array[y+1];
array[y+1]=temp;
}
}
}
//个位→百位→十位冒泡排序
}else{
for(int x=array.length-1;x>0;x--){
for(int y=0;y<x;y++){
//取出百位h,十位t,个位g
int h1=array[y]/100;
int t1=(array[y]/10)%10;
int g1=array[y]%10;

int h2=array[y+1]/100;
int t2=(array[y+1]/10)%10;
int g2=array[y+1]%10;

if(g1<g2||(g1==g2 && h1<h2)||(g1==g2 && h1==h2 && t1<t2)){
int temp=array[y];
array[y]=array[y+1];
array[y+1]=temp;
}
}
}
}

//输出结果
System.out.print("排序后数组:");
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}

scanner.close();
}
}

热心网友 时间:2023-09-15 06:19

import java.util.Scanner;

public class Test {


    public static void main(String[] args) {
    int[] array = new int[1000];
        for (int i = 0; i < 1000; i++) {
            array[i] = i;
        }
        boolean loopFlag = true;

        while(loopFlag) {
            System.out.println("请输入字符:");
            Scanner sc = new Scanner(System.in);
            String result = sc.nextLine();
            if ("A".equalsIgnoreCase(result)) {
                array = sort(array, result);
                print(array);
            } else if ("B".equalsIgnoreCase(result)) {
                array = sort(array, result);
                print(array);
            } else if ("C".equalsIgnoreCase(result)) {
                array = sort(array, result);
                print(array);
            } else {
                System.out.println("你输入的不合法,请重新输入...");
            }
            System.out.println("按Y/y继续,按N/n退出?");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            if ("N".equalsIgnoreCase(input)) {
                loopFlag = false;
                System.out.println("退出成功!");
            }
        }
    }

    /**
     * @param a
     * @param b
     * @param type 比较类型
     * @return 若a>b 返回true
     */
    public static boolean compare(int a, int b, String type) {
        int hundredsDigitA = a / 100;
        int tenDigitA = a % 100 / 10;
        int singleDigitA = a % 100 % 10;

        int hundredsDigitB = b / 100;
        int tenDigitB = b % 100 / 10;
        int singleDigitB = b % 100 % 10;

        if("B".equalsIgnoreCase(type)) {
            // 十位>个位>百位
            if (tenDigitA > tenDigitB) {
                return true;
            } else if(tenDigitA < tenDigitB){
                return false;
            } else {
                if (singleDigitA > singleDigitB) {
                    return true;
                } else if(singleDigitA < singleDigitB){
                    return false;
                } else {
                    if (hundredsDigitA > hundredsDigitB) {
                        return true;
                    } else if(hundredsDigitA < hundredsDigitB){
                        return false;
                    } else {
                        // a,b相等,返回true/false都可以,基本上不会走到这一步
                        return true;
                    }
                }
            }
        } else if ("C".equalsIgnoreCase(type)) {
            // 个位>百位>十位
            if (singleDigitA > singleDigitB) {
                return true;
            } else if(singleDigitA < singleDigitB){
                return false;
            } else {
                if (hundredsDigitA > hundredsDigitB) {
                    return true;
                } else if(hundredsDigitA < hundredsDigitB){
                    return false;
                } else {
                    if (tenDigitA > tenDigitB) {
                        return true;
                    } else if(tenDigitA < tenDigitB){
                        return false;
                    } else {
                        // 相等,返回true/false都可以,基本上不会走到这一步
                        return true;
                    }
                }
            }
        } else {
            // 百位>十位>个位
            if (hundredsDigitA > hundredsDigitB) {
                return true;
            } else if(hundredsDigitA < hundredsDigitB){
                return false;
            } else {
                if (tenDigitA > tenDigitB) {
                    return true;
                } else if(tenDigitA < tenDigitB){
                    return false;
                } else {
                    if (singleDigitA > singleDigitB) {
                        return true;
                    } else if(singleDigitA < singleDigitB){
                        return false;
                    } else {
                        // 相等,返回true/false都可以,基本上不会走到这一步
                        return true;
                    }
                }
            }
        }
    }

    /**
     *
     * @param array 排序数组
     * @param type  排序类型,传入字符串"A"、"B"、"C"
     * @return
     */
    public static int[] sort(int[] array, String type) {
        int length = array.length;
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length - 1; j++) {
                if (!compare(array[j], array[j+1], type)) {
                    // 如果前面的数小于后面的数,就换位
                    int temp;
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
        return array;
    }
    public static void print(int[] array) {
        for (int number : array) {
            System.out.print(number + ">");
        }
        System.out.println();
    }
}

热心网友 时间:2023-09-15 06:20

import java.util.*;
import java.util.stream.*;
/**
 * @author hardneedl
 */
public class SortNumsDemo {
    public static void main(String... args) {
        //初始化整数的集合体
        List<Integer> nums = new ArrayList<>();
        for (int i = 0; i < 1000; i++) nums.add(i);

        //A方案的排序
        Comparator<Integer> a_comparator = Integer::compareTo;

        //B 方案的排序
        Comparator<Integer> b_comparator = (o1, o2) -> {
            //取百位上的值
            int h1 = o1 / 100, h2 = o2 / 100;

            //取 “十位” 上的值
            int i1 = (o1 - h1 * 100) / 10;
            int i2 = (o2 - h2 * 100) / 10;

            if (i1 > i2) return 1;
            if (i1 < i2) return -1;

            // 取个位
            int l1 = o1 - 100 * h1 - 10 * i1;
            int l2 = o2 - 100 * h2 - 10 * i2;

            if (l1 > l2) return 1;
            if (l1 < l2) return -1;

            if (h1 > h2) return 1;
            if (h1 < h2) return -1;

            return 0;
        };

        //c 方案的排序
        Comparator<Integer> c_comparator = (o1, o2) -> {
            //取百位上的值
            int h1 = o1 / 100, h2 = o2 / 100;

            //取 “十位” 上的值
            int i1 = (o1 - h1 * 100) / 10;
            int i2 = (o2 - h2 * 100) / 10;

            // 取个位
            int l1 = o1 - 100 * h1 - 10 * i1;
            int l2 = o2 - 100 * h2 - 10 * i2;

            if (l1 > l2) return 1;
            if (l1 < l2) return -1;

            if (h1 > h2) return 1;
            if (h1 < h2) return -1;

            if (i1 > i2) return 1;
            if (i1 < i2) return -1;

            return 0;
        };

        Stream<Integer> a_stream = nums.stream().sorted(a_comparator.reversed());
        System.out.println("A 方案排序:");
        a_stream.forEach(i -> System.out.printf("%d,", i));

        System.out.println("\r\nB方案排序:");
        Stream<Integer> b_stream = nums.stream().sorted(b_comparator.reversed());
        b_stream.forEach(i -> System.out.printf("%d,", i));

        System.out.println("\r\nC方案排序:");
        Stream<Integer> c_stream = nums.stream().sorted(c_comparator.reversed());
        c_stream.forEach(i -> System.out.printf("%d,", i));
    }
}

热心网友 时间:2023-09-15 06:20

用的冒泡排序法

package sort;

import java.util.Random;
import java.util.Scanner;

public class Sort {
public static void main(String[] args){

Scanner scanner=new Scanner(System.in);
System.out.println("输入ABC");
String input=scanner.nextLine();

//默认A排序
char type='A';
if(input.equals("B")){
type='B';
}else if(input.equals("C")){
type='C';
}

//定义、初始化要排序的数组,并输出数组
int[] array=new int[10];
Random random=new Random();
System.out.print("排序前数组:");
for(int i=0;i<array.length;i++){
array[i]=random.nextInt(1000);
System.out.print(array[i]+" ");
}
System.out.println();

//从大到小冒泡排序
if(type=='A'){
for(int x=array.length-1;x>0;x--){
for(int y=0;y<x;y++){
if(array[y]<array[y+1]){
int temp=array[y];
array[y]=array[y+1];
array[y+1]=temp;
}
}
}
//十位→个位→百位冒泡排序
}else if(type=='B'){
for(int x=array.length-1;x>0;x--){
for(int y=0;y<x;y++){
//取出百位h,十位t,个位g
int h1=array[y]/100;
int t1=(array[y]/10)%10;
int g1=array[y]%10;

int h2=array[y+1]/100;
int t2=(array[y+1]/10)%10;
int g2=array[y+1]%10;

if(t1<t2||(t1==t2 && g1<g2)||(t1==t2 && g1==g2 && h1<h2)){
int temp=array[y];
array[y]=array[y+1];
array[y+1]=temp;
}
}
}
//个位→百位→十位冒泡排序
}else{
for(int x=array.length-1;x>0;x--){
for(int y=0;y<x;y++){
//取出百位h,十位t,个位g
int h1=array[y]/100;
int t1=(array[y]/10)%10;
int g1=array[y]%10;

int h2=array[y+1]/100;
int t2=(array[y+1]/10)%10;
int g2=array[y+1]%10;

if(g1<g2||(g1==g2 && h1<h2)||(g1==g2 && h1==h2 && t1<t2)){
int temp=array[y];
array[y]=array[y+1];
array[y+1]=temp;
}
}
}
}

//输出结果
System.out.print("排序后数组:");
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}

scanner.close();
}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
东京猿物语 歌词 经常手淫会腰痛阿。要紧吗 男科高玩痛是什么情况? 莱西市区周围有没有能拿回家做的手工活,要简单点的不费眼睛的活,老人快... 2024年江西450分能考上什么大学? 江西高考450分左右能上什么大学 多少分能考上江西农业大学南昌商学院 江西高考449分左右能上什么大学 中国热菜艺术造型基本信息 热菜造型的做法 用java产生10个随机数,并将10个随机数按产生的顺序显示出来然后将它们按从小大的顺序排序并显示 如何导入java.util.stream.jar 如何让Eclipse自动 import java.util.stream.Stream java中 在键盘中输入十个数判断哪些是偶数 并把偶数写在文件中 并从文件中读出来 电影《美人鱼》中有哪些值得大家留意的小细节? 上班心情说说和句子大全 在调查小岛的过程中鲁滨孙发现了什么水果 在合肥什么地方开瑜伽馆好?在生态公园附近怎么样?这边的消费水平怎么样?帮帮忙,谢谢啦。 柴油燃点和汽油燃点 《影 (2018)》在线免费观看百度云资源,求下载 我在微信被人骗了,只知道可以利用查到他的手机号码吗? 拉伸运动如何做 被骗走了,绑定了别人的手机号码? 拉伸运动应该如何做 怎么查到对方手机号 如坐针毡是指什么意思? 支气管炎有什么症状如何治疗? 支气管炎症状及治疗方法介绍? 塔罗牌.命运轮的解释 有独字,有心字,成语是什么 java ping ip ,求大神帮忙 java循环换行的问题 康佳电视看了100天坏了,能换新的吗? 三星手机的熄屏显示很完美,为什么苹果iphone就做不出来呢? 公益林法律法规 非农业户口能领公益林? 修水县可以申请公益林办家庭农场吗? 我家林地被划为公益林有补贴吗? 拔完牙多久可以喝冷泡茶吗 隔夜茶到底能不能喝 《百科知识》 2009农历11月15是什么星座 农历19999年11月15号什么星座 200911月15日是什么星座? 20091115出生是什么星座 坚果手机怎么把自己照片当桌面 怎样关闭花呗 关闭花呗的步骤 夏季养生就吃姜 夏季吃姜有什么好处 新配的电脑,主机里面时不时会有滋~这种很长一声,有点类似于电流声音,声音很小? 如何在EXCEL中制作瀑布图 龟山的介绍