(请忽略我工地散装英语的注释)(lll¬ω¬)
问题描述
操作系统中在查找文件时会按照某个规则对文件排序,例如下图为按照文件修改日期逆序排
序(最后修改的排在最前面)。
但目前操作系统不支持同时按照多个字段进行排序。现在请你写一个程序能够同时按照修改
日期和文件大小对文件进行排序,排序规则为:
1.
日期优先,最后修改的排在前面
2.
当修改日期相同时,大的文件排在前面。
输入说明
第一行为一个数字
n
,
n
表示共有
n
个待排序的文件,
1
≤
n
≤
100
。
接下来是
n
行,每行包含一个文件的修改日期和文件大小,这两个字段之间用空格分隔。
文件修改日期包含年、月、日,表示年、月、日的整数之间用“
/
”分隔,格式为“年
/
月
/
日”。年份的数值在
1960-2018
之间;月份的数值在
1-12
之间;日的数值在
1-31
之间。
文件大小是一个不超过
100000000
的整数。
输入数据中没有完全相同的日期和文件大小。
输出说明将输入数据按题目描述的规则排序后输出,每行输出一个文件的修改日期和文件大小。 输入样例82018/1/8 10242012/10/31 2562014/10/29 3002012/10/31 4572014/10/27 5122011/10/27 952014/11/3 11022017/11/24 1535
输出样例2018/1/8 10242017/11/24 15352014/11/3 11022014/10/29 3002014/10/27 5122012/10/31 4572012/10/31 2562011/10/27 95
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
typedef struct file1
{
int year;
int month;
int day;
int size;
}file[], t;
int main()
{
int n, i, j;
scanf("%d", &n);
file1 file[n], t;
for(i=0;i<n;i++){
scanf("%d/%d/%d %d", &file[i].year, &file[i].month, &file[i].day, &file[i].size);
}//tips:输入直接加上/
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(file[j].year<file[j+1].year){
t = file[j];
file[j] = file[j+1];
file[j+1] = t;
}else if(file[j].year==file[j+1].year){//年份相等
if(file[j].month<file[j+1].month){//year same,but month not same
t = file[j];
file[j] = file[j+1];
file[j+1] = t;
}else if(file[j].month==file[j+1].month){//year and month same
if(file[j].day<file[j+1].day){//day not same
t = file[j];
file[j] = file[j+1];
file[j+1] = t;
}else if(file[j].day==file[j+1].day)//year,month and day same,but size not same
if(file[j].size<file[j+1].size){
t = file[j];
file[j] = file[j+1];
file[j+1] = t;
}
}
}
}
}
for(i=0;i<n;i++){
printf("%d/%d/%d %d\n", file[i].year, file[i].month, file[i].day, file[i].size);
}
return 0;
}