磁盘的容量单位常用的有 M,G,T 这三个等级,关系为 1T = 1024G、1G = 1024M 如样例所示先输入磁盘的个数,再依次输入磁盘的容量大小,然后按照从小到大 的顺序对磁盘容量进行排序并输出。
3
1G
2G
1024M
输出
1G
1024M
2G
3
2G4M
3M2G
1T
输出
3M2G
2G4M
1T
#include <iostream>
#include <algorithm> // stable_sort(beg,end,_Pr)
#include <string>
#include <sstream> // stringstream
#include <vector>
void fun(){
// 录入数据
int n{};
std::cin >> n;
std::vector<std::pair<std::string,unsigned >> vs;
std::string s;
unsigned temp{}, sum{};
char c{};
std::stringstream ss;
for (int i{}; i < n; ++i) {
std::cin >> s;
ss.clear();
ss << s;
sum = 0u;
while (ss >> temp >> c) {
switch (c)
{
case 'M':sum += temp; break;
case 'G':sum += temp << 10; break;
case 'T':sum += temp << 20; break;
default:
break;
}
}
vs.push_back(std::make_pair(s, sum));
}
// 排序
std::stable_sort(vs.begin(), vs.end(), [](std::pair<std::string, unsigned > a, std::pair<std::string, unsigned > b) {return a.second < b.second; });
// 输出
for (auto& a : vs) {
std::cout << a.first << std::endl;
}
}