求解一道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();
}
}