【华为OD-E卷 - 磁盘容量排序 100分(python、java、c++、js、c)】
题目
磁盘的容量单位常用的有M,G,T这三个等级,
它们之间的换算关系为1T = 1024G,1G = 1024M,
现在给定n块磁盘的容量,请对它们按从小到大的顺序进行稳定排序,
例如给定5块盘的容量,1T,20M,3G,10G6T,3M12G9M
排序后的结果为20M,3G,3M12G9M,1T,10G6T。
注意单位可以重复出现,上述3M12G9M表示的容量即为3M+12G+9M,和12M12G相等
输入描述
- 输入第一行包含一个整数n(2 <= n <= 100),表示磁盘的个数,
接下的n行,每行一个字符串(长度大于2,小于30),
表示磁盘的容量,由一个或多个格式为mv的子串组成,
其中m表示容量大小,v表示容量单位,例如20M,1T,30G,10G6T,3M12G9M。
磁盘容量m的范围为1到1024的正整数,
容量单位v的范围只包含题目中提到的M,G,T三种,换算关系如题目描述
输出描述
- 输出n行,表示n块磁盘容量排序后的结果
用例
用例一:
输入:
3
1G
2G
1024M
输出:
1G
1024M
2G
用例二:
输入:
3
2G4M
3M2G
1T
输出:
3M2G
2G4M
1T
python解法
- 解题思路:
- 本程序的目标是对存储容量进行排序,输入的存储容量包含 M(MB),G(GB),T(TB) 等单位,排序时需按照实际大小进行比较。
解题步骤
读取输入
n:表示存储设备的数量。
disks:存储容量列表(例如 [“512M”, “2G”, “1T”])。
计算存储单位的实际数值 calculate_value(cap)
遍历字符串 cap 提取数值部分 num_str 和单位部分 M/G/T:
M (MB):保持数值不变。
G (GB):转换为 MB,1G = 1024M。
T (TB):转换为 MB,1T = 1024 × 1024M。
返回统一转换后的 MB 值,作为排序依据。
对 disks 进行排序
使用 sort(),按 calculate_value() 计算的数值排序。
输出排序后的存储容量
逐行打印排序后的 disks
# 读取存储设备数量
n = int(input())
# 读取存储容量列表
disks = [input() for _ in range(n)]
# 计算存储容量的数值(统一换算为 MB)
def calculate_value(cap):
value = 0 # 存储最终的 MB 数值
num_str = '' # 临时存储数值部分
# 遍历字符串,提取数值和单位
for ch in cap:
if ch.isdigit():
num_str += ch # 累积数值部分
else:
if ch == 'M': # MB 直接加
value += int(num_str)
elif ch == 'G': # GB 转换为 MB (1G = 1024M)
value += int(num_str) * 1024
elif ch == 'T': # TB 转换为 MB (1T = 1024 * 1024M)
value += int(num_str) * 1024 * 1024
num_str = '' # 重置数值存储
return value # 返回统一换算的 MB 值
# 按照转换后的数值进行排序
disks.sort(key=calculate_value)
# 输出排序后的存储容量
for disk in disks:
print(disk)
java解法
- 解题思路
- 本程序的目标是对存储容量进行排序,输入的存储容量包含 M(MB),G(GB),T(TB) 等单位,排序时需按照实际大小进行比较。
解题步骤
读取输入
读取整数 n,表示存储设备的数量。
读取 n 行存储容量信息,并存入 List disks。
计算存储单位的实际数值 computeValue(String capacity)
遍历 capacity 字符串,提取数值部分 number 和单位部分 M/G/T:
M (MB):保持数值不变,乘 1。
G (GB):转换为 MB,1G = 1024M。
T (TB):转换为 MB,1T = 1024 × 1024M。
计算统一转换后的 MB 值,并返回。
对 disks 进行排序
使用 sort() 方法,自定义 Comparator 进行排序,比较 computeValue(a) 和 computeValue(b)。
输出排序后的存储容量
遍历 disks,逐行打印排序后的结果
import java.util.*;
public class Main {
// 计算存储容量的数值(统一换算为 MB)
private static long computeValue(String capacity) {
long total = 0; // 存储最终的 MB 数值
int multiplier = 0; // 存储当前单位的换算值
StringBuilder number = new StringBuilder(); // 存储数值部分
// 遍历存储容量字符串,解析数值和单位
for (char c : capacity.toCharArray()) {
if (Character.isDigit(c)) {
number.append(c); // 累积数值部分
} else {
int num = Integer.parseInt(number.toString()); // 转换数值
switch (c) {
case 'M': multiplier = 1; break; // MB 直接使用
case 'G': multiplier = 1024; break; // GB 转换为 MB (1G = 1024M)
case 'T': multiplier = 1024 * 1024; break; // TB 转换为 MB (1T = 1024 * 1024M)
}
total += num * multiplier; // 计算总值
number.setLength(0); // 清空 number,准备解析下一个数值
}
}
return total; // 返回最终的 MB 数值
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // 读取存储设备数量
sc.nextLine(); // 读取换行符,防止干扰输入
List<String> disks = new ArrayList<>();
for (int i = 0; i < n; i++) {
disks.add(sc.nextLine()); // 读取存储容量
}
// 按照转换后的数值进行排序
disks.sort((a, b) -> Long.compare(computeValue(a), computeValue(b)));
// 输出排序后的存储容量
for (String disk : disks) {
System.out.println(disk);
}
}
}
C++解法
- 解题思路
- 本程序的目标是对存储容量进行排序,输入的存储容量包含 M(MB),G(GB),T(TB) 等单位,排序时需按照实际大小进行比较。
解题步骤
读取输入
读取整数 n,表示存储设备的数量。
读取 n 行存储容量信息,并存入 vector capacitys。
计算存储单位的实际数值 calc(const string& cap)
遍历 cap 字符串,提取数值部分 num 和单位部分 M/G/T:
M (MB):保持数值不变,乘 1。
G (GB):转换为 MB,1G = 1024M。
T (TB):转换为 MB,1T = 1024 × 1024M。
计算统一转换后的 MB 值,并返回。
对 capacitys 进行排序
使用 sort() 方法,调用 compare() 进行排序,比较 calc(a) 和 calc(b)。
输出排序后的存储容量
遍历 capacitys,逐行打印排序后的结果
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
// 数字转换函数,替代 C++11 的 stoi
int stringToInt(const string &s) {
stringstream ss(s);
int num;
ss >> num;
return num;
}
// 计算存储容量的数值(统一换算为 MB)
int calc(const string& cap) {
int ans = 0; // 存储最终的 MB 数值
string num; // 临时存储数值部分
// 遍历存储容量字符串,解析数值和单位
for (size_t i = 0; i < cap.size(); ++i) {
char c = cap[i];
if (isdigit(c)) {
num += c; // 累积数值部分
} else {
if (c == 'M') {
ans += stringToInt(num); // MB 直接使用
} else if (c == 'G') {
ans += stringToInt(num) * 1024; // GB 转换为 MB (1G = 1024M)
} else if (c == 'T') {
ans += stringToInt(num) * 1024 * 1024; // TB 转换为 MB (1T = 1024 * 1024M)
}
num.clear(); // 清空 num,准备解析下一个数值
}
}
return ans; // 返回最终的 MB 数值
}
// 比较函数,按照存储容量大小排序
bool compare(const string &a, const string &b) {
return calc(a) < calc(b);
}
// 执行排序并输出结果
void getResult(vector<string> &capacitys) {
sort(capacitys.begin(), capacitys.end(), compare); // 按照容量大小排序
// 逐行输出排序后的存储容量
for (size_t i = 0; i < capacitys.size(); ++i) {
cout << capacitys[i] << endl;
}
}
int main() {
int n;
cin >> n; // 读取存储设备数量
vector<string> capacitys(n);
// 读取存储容量
for (int i = 0; i < n; ++i) {
cin >> capacitys[i];
}
// 进行排序并输出结果
getResult(capacitys);
return 0;
}
C解法
更新中
JS解法
解题步骤
读取输入
监听标准输入,每次读取一行并存入 lines 数组。
第一行输入 n,表示存储设备的数量。
读取 n 行存储容量信息,并存入 lines。
当 lines.length === n + 1 时,调用 processDisks(lines) 进行处理。
计算存储单位的实际数值 getCapacity(disk)
使用正则表达式 (\d+)([MGT]) 解析存储容量,提取数值和单位:
M (MB):保持数值不变。
G (GB):转换为 MB,1G = 1024M。
T (TB):转换为 MB,1T = 1024 × 1024M。
计算统一转换后的 MB 值,并返回。
对 disks 进行排序
使用 sort() 方法,按照 getCapacity() 计算的数值排序。
输出排序后的存储容量
遍历 disks,逐行打印排序后的结果
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const lines = [];
// 监听输入,每次读取一行
rl.on("line", (line) => {
lines.push(line);
// 判断是否读取完所有输入行(第一行为 n,后续 n 行为存储容量)
if (lines.length === parseInt(lines[0], 10) + 1) {
lines.shift(); // 移除第一行(n),保留存储容量数据
processDisks(lines); // 调用处理函数
lines.length = 0; // 清空 lines,准备下一次输入
}
});
// 处理并排序存储容量
function processDisks(disks) {
disks
.sort((a, b) => getCapacity(a) - getCapacity(b)) // 按存储容量大小排序
.forEach((disk) => console.log(disk)); // 输出排序后的存储容量
}
// 计算存储容量的数值(统一换算为 MB)
function getCapacity(disk) {
let regex = /(\d+)([MGT])/g; // 正则匹配数值+单位(M、G、T)
let result;
let total = 0;
// 解析存储容量字符串
while ((result = regex.exec(disk)) !== null) {
let num = parseInt(result[1], 10); // 提取数值部分
let unit = result[2]; // 提取单位部分
// 根据单位转换为 MB
switch (unit) {
case "M":
total += num; // MB 直接加
break;
case "G":
total += num * 1024; // GB 转换为 MB (1G = 1024M)
break;
case "T":
total += num * 1024 * 1024; // TB 转换为 MB (1T = 1024 * 1024M)
break;
}
}
return total; // 返回最终计算出的 MB 值
}
注意:
如果发现代码有用例覆盖不到的情况,欢迎反馈!会在第一时间修正,更新。
解题不易,如对您有帮助,欢迎点赞/收藏