原题地址
1189: 【绝对值排序】
AC代码
package train20190503;
/**
输入
3 3 -4 2
4 0 1 2 -3
0
输出
-4 3 2
-3 2 1 0
*/
import java.util.Scanner;
public class Main_1189_绝对值排序 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 输入套路
while (sc.hasNext()) {
// 第一个数为n
int n = sc.nextInt();
// 输入0时结束
if(n == 0)
break;
// 接下来输入n个数
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
// 绝对值比较,冒泡排序
for(int i=0; i<n; i++) {
for(int j=i; j<n; j++) {
if(Math.abs(a[i]) < Math.abs(a[j])) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
// 打印,不能多打印换行
for (int i = 0; i < n; i++) {
System.out.print(a[i]);
if (i == n - 1)
System.out.println();
else
System.out.print(" ");
}
}
}
}
AC截图


博客主要介绍了如何使用Java解决1189题目的绝对值排序问题,包括AC代码及通过截图。
最低0.47元/天 解锁文章
2875

被折叠的 条评论
为什么被折叠?



