XDOJ-文件排序

文件排序

要变得更强。
今天上课刚讲了结构体,就来玩玩啦。
问题描述
操作系统中在查找文件时会按照某个规则对文件排序,例如下图为按照文件修改日期逆序排
序(最后修改的排在最前面)。
但目前操作系统不支持同时按照多个字段进行排序。现在请你写一个程序能够同时按照修改
日期和文件大小对文件进行排序,排序规则为:

  1. 日期优先,最后修改的排在前面
  2. 当修改日期相同时,大的文件排在前面。
    输入说明

第一行为一个数字 n,n 表示共有 n 个待排序的文件, 1≤ n≤ 100。 接下来是 n
行,每行包含一个文件的修改日期和文件大小,这两个字段之间用空格分隔。
文件修改日期包含年、月、日,表示年、月、日的整数之间用“/”分隔,格式为“年/月/ 日”。年份的数值在 1960-2018
之间;月份的数值在 1-12 之间;日的数值在 1-31 之间。 文件大小是一个不超过 100000000 的整数。
输入数据中没有完全相同的日期和文件大小。

输出说明

将输入数据按题目描述的规则排序后输出,每行输出一个文件的修改日期和文件大小。

输入样例

8 2018/1/8 1024
2012/10/31 256
2014/10/29 300
2012/10/31 457
2014/10/27 512
2011/10/27 95
2014/11/3 1102
2017/11/24 1535

输出样例

2018/1/8 1024
2017/11/24 1535
2014/11/3 1102
2014/10/29 300
2014/10/27512
2012/10/31 457
2012/10/31 256
2011/10/27 95

我的代码

#include<stdio.h>
typedef struct{
	int memory;
	struct{
		int year,month,day;
	}date;
}PROFILE;
int main(){
	int n,i,j,temp1,temp2,temp3,temp4;
	PROFILE files[100];
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d/%d/%d %d",&files[i].date.year,&files[i].date.month,&files[i].date.day,&files[i].memory);
	
	for(i=0;i<n;i++){
		for(j=0;j<n-i-1;j++){
			if(files[j].date.year<files[j+1].date.year){
				temp1=files[j].date.year;
				files[j].date.year=files[j+1].date.year;
				files[j+1].date.year=temp1;
				
				temp2=files[j].date.month;
				files[j].date.month=files[j+1].date.month;
				files[j+1].date.month=temp2;
				
				temp3=files[j].date.day;
				files[j].date.day=files[j+1].date.day;
				files[j+1].date.day=temp3;
				
				temp4=files[j].memory;
				files[j].memory=files[j+1].memory;
				files[j+1].memory=temp4;
			}
			if(files[j].date.year==files[j+1].date.year){
				if(files[j].date.month<files[j+1].date.month){
					temp2=files[j].date.month;
					files[j].date.month=files[j+1].date.month;
					files[j+1].date.month=temp2;
				
					temp3=files[j].date.day;
					files[j].date.day=files[j+1].date.day;
					files[j+1].date.day=temp3;
				
					temp4=files[j].memory;
					files[j].memory=files[j+1].memory;
					files[j+1].memory=temp4;
				}
				if(files[j].date.month==files[j+1].date.month){
					if(files[j].date.day<files[j+1].date.day){
						temp3=files[j].date.day;
						files[j].date.day=files[j+1].date.day;
						files[j+1].date.day=temp3;
				
						temp4=files[j].memory;
						files[j].memory=files[j+1].memory;
						files[j+1].memory=temp4;
					}
					if(files[j].date.day==files[j+1].date.day){
						if(files[j].memory<files[j+1].memory){
							temp4=files[j].memory;
							files[j].memory=files[j+1].memory;
							files[j+1].memory=temp4;
						}
					}
				}
				
			}
		}
	}
	
	for(i=0;i<n;i++){
		printf("%d/%d/%d %d\n",files[i].date.year,files[i].date.month,files[i].date.day,files[i].memory);
	}
} 

不过老实说,结构体数组的调用,字母真的好多好长啊!!
复制粘贴真麻烦(滑稽

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值