【杭电】[1234]开门人和关门人

文章字数:311

问题描述

问题分析

也是挺重要的一题
第一次用到的结构体
在一些地方纠结了好久

而且第一次没接触到sort
所以写的比较长

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
#include<string.h>
struct qd {
	char s[15];
	int sh;
	int sm;
	int ss;
	int eh;
	int em;
	int es;
};
int main() {
	int N,M;
	scanf("%d",&N);
	while(N--) {
		scanf("%d",&M);
		int i;
		struct qd a[M],b,e;
		for(i=0; i<M; i++)
			scanf("%s %d:%d:%d %d:%d:%d",&a[i].s,&a[i].sh,&a[i].sm,&a[i].ss,&a[i].eh,&a[i].em,&a[i].es);
		for(i=0; i<M; i++) {
			if(i==0) {
				b=a[i];
				e=a[i];
			} else {
				if(a[i].sh<b.sh)
					b=a[i];
				else if(a[i].sh==b.sh) {
					if(a[i].sm<b.sm)
						b=a[i];
					else if(a[i].sm==b.sm) {
						if(a[i].ss<b.ss)
							b=a[i];
					}
				}
				if(a[i].eh>e.eh)
					e=a[i];
				else if(a[i].eh==e.eh) {
					if(a[i].em>e.em)
						e=a[i];
					else if(a[i].em==e.em) {
						if(a[i].es>e.es)
							e=a[i];
					}
				}
			}
		}
		printf("%s %s\n",b,e);
	}
	return 0;
}

下面是后来写的sort版

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct qd {
	char s[20];
	char kai[10];
	char guan[10];
};
bool vsk(qd A,qd B) {
	return strcmp(A.kai,B.kai)<0;
}
bool vsg(qd A,qd B) {
	return strcmp(A.guan,B.guan)>0;
}
int main() {
	int N,M;
	scanf("%d",&N);
	while(N--) {
		scanf("%d",&M);
		int i;
		struct qd a[M];
		for(i=0; i<M; i++)
			scanf("%s %s %s",&a[i].s,&a[i].kai,&a[i].guan);
		sort(a,a+M,vsk);
		printf("%s ",a[0].s);
		sort(a,a+M,vsg);
		printf("%s\n",a[0].s);
	}
	return 0;
}

明显短了好多啊~
C++的用法

该内容采用 CC BY-NC-SA 4.0 许可协议。

如果对您有帮助或存在意见建议,欢迎在下方评论交流。

加载中...