做网站比较好的软件,网站报备查询,网络营销的四大要素,购物网站的首页是静态■ 题目描述
【磁盘容量排序】
磁盘的容量单位常用的有M#xff0c;G#xff0c;T这三个等级#xff0c;它们之间的换算关系为1T 1024G#xff0c;1G 1024M#xff0c;现在给定n块磁盘的容量#xff0c;
请对它们按从小到大的顺序进行稳定排序#xff0c;例如给定5…■ 题目描述
【磁盘容量排序】
磁盘的容量单位常用的有MGT这三个等级它们之间的换算关系为1T 1024G1G 1024M现在给定n块磁盘的容量
请对它们按从小到大的顺序进行稳定排序例如给定5块盘的容量1T20M3G10G6T3M12G9M排序后的结果为20M3G3M12G9M1T10G6T。
注意单位可以重复出现上述3M12G9M表示的容量即为3M12G9M和12M12G相等。
输入描述
输入第一行包含一个整数n(2 n 100)表示磁盘的个数接下的n行每行一个字符串(长度大于2小于30)表示磁盘的容量
由一个或多个格式为mv的子串组成其中m表示容量大小v表示容量单位例如20M1T30G10G6T3M12G9M。
磁盘容量m的范围为1到1024的正整数容量单位v的范围只包含题目中提到的MGT三种换算关系如题目描述。
输出描述
输出n行表示n块磁盘容量排序后的结果。
C语言
#include bits/stdc.h#define CEILING_POS(X) ((X-(int)(X)) 0 ? (int)(X1) : (int)(X))
#define CEILING_NEG(X) ((X-(int)(X)) 0 ? (int)(X-1) : (int)(X))
#define CEILING(X) ( ((X) 0) ? CEILING_POS(X) : CEILING_NEG(X) )#define MIN(a, b) ((a) (b)) ? (a) : (b)
#define MAX(a, b) ((a) (b)) ? (a) : (b)int cmpfunc(const void *a, const void *b) {return (*(int *) a - *(int *) b);
}int convert(char *capacity) {int size 0;char *upper strdup(capacity);for (int i 0; i strlen(upper); i) {upper[i] toupper(upper[i]);}char *token strtok(upper, MGTP);while (token ! NULL) {int num atoi(token);char unit upper[strlen(token)];switch (unit) {case M:size num;break;case G:size num * 1024;break;case T:size num * 1024 * 1024;break;default:break;}token strtok(NULL, MGTP);}return size;
}int main() {int n;scanf(%d, n);char **capacities (char **) malloc(n * sizeof(char *));for (int i 0; i n; i) {capacities[i] (char *) malloc((20 1) * sizeof(char));scanf(%s, capacities[i]);}for (int i 0; i n; i) {for (int j i 1; j n; j) {if (convert(capacities[i]) convert(capacities[j])) {char *temp capacities[i];capacities[i] capacities[j];capacities[j] temp;}}}for (int i 0; i n; i) {printf(%s\n, capacities[i]);}return 0;
}
C
示例1 输入输出示例仅供调试后台判题数据一般不包含示例
输入
3
1G
2G
1024M
输出
1G
1024M
2G
说明
1G和1024M容量相等稳定排序要求保留它们原来的相对位置故1G在1024M之前。
示例2 输入输出示例仅供调试后台判题数据一般不包含示例
输入
3
2G4M
3M2G
1T
输出
3M2G
2G4M
1T
说明
1T的容量大于2G4M2G4M的容量大于3M2G。
#include bits/stdc.husing namespace std;int getSize(string s)
{int ans 0;int curNum 0;for (char c: s) {if (isdigit(c)) {curNum curNum * 10 (c - 0);} else {if (c M) {ans curNum;} else if (c G) {ans curNum * 1024;} else if (c T) {ans curNum * 1024 * 1024;}curNum 0;}}return ans;
}bool cmp(const string s1, const string s2)
{return getSize(s1) getSize(s2);
}int main(int argc, const char *argv[])
{string s;int n;cin n;vectorstring size;while (n--) {cin s;size.push_back(s);}stable_sort(size.begin(), size.end(), cmp);for (string s: size) {cout s endl;}return 0;
}
文章转载自: http://www.morning.nqwz.cn.gov.cn.nqwz.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.lmctj.cn.gov.cn.lmctj.cn http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.pdmsj.cn.gov.cn.pdmsj.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.qbfs.cn.gov.cn.qbfs.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.ychrn.cn.gov.cn.ychrn.cn http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn http://www.morning.hncrc.cn.gov.cn.hncrc.cn http://www.morning.zfcfx.cn.gov.cn.zfcfx.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.bpmtx.cn.gov.cn.bpmtx.cn http://www.morning.nqgff.cn.gov.cn.nqgff.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.ggxbyhk.cn.gov.cn.ggxbyhk.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.fylsz.cn.gov.cn.fylsz.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.ktsth.cn.gov.cn.ktsth.cn http://www.morning.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.kmlmf.cn.gov.cn.kmlmf.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.zpqlf.cn.gov.cn.zpqlf.cn http://www.morning.pqqzd.cn.gov.cn.pqqzd.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.wdhhz.cn.gov.cn.wdhhz.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.rwlsr.cn.gov.cn.rwlsr.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.wnxqf.cn.gov.cn.wnxqf.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.wtcd.cn.gov.cn.wtcd.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn