华为OD 笔试题
题目大意
磁盘的容量单位有M、G、T,其关系为 1T = 1000G、1G = 1000M,如样例所示先输入磁盘的个数,再依次输入磁盘的容量大小,然后按照从小到大的顺序对磁盘容量进行排序并输出。
例如
输入
3
20M
1T
300G
输出
20M
300G
1T
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = Integer.parseInt(scanner.nextLine());
String[] list = new String[num];
for (int i = 0; i < num; i++) {
list[i] = scanner.nextLine();
}
// 冒泡排序
for (int i = 0; i < list.length - 1; i++) {
for (int j = 0; j < list.length - 1 -i; j++) {
if (unitCovert(list[j]) > unitCovert(list[j+1])) {
String tem = list[j];
list[j] = list[j+1];
list[j+1] = tem;
}
}
}
for (String s : list) {
System.out.println(s);
}
}
// 单位换算
private static int unitCovert(String string) {
if (string.length() < 1) {
return 0;
}
int end = string.length() -1;
int value = Integer.parseInt(string.substring(0, end));
if (string.endsWith("T")) {
return value * 1000000;
} else if (string.endsWith("G")) {
return value * 1000;
} else {
return value;
}
}
}