我是靠谱客的博主 听话御姐,这篇文章主要介绍1187: 零起点学算法94——今年暑假不AC(Java),现在分享给大家,希望可以做个参考。

1187:零起点学算法94——今年暑假不AC
Time Limit: 1 Sec Memory Limit: 32 MB 64bit IO Format: %lld

Description

“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%…”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

Input

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

提示:Ti_s<=Ti_e

Output

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

Sample Input

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0

Sample Output

复制代码
1
2
5

代码如下

复制代码
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.Scanner; public class Main { Scanner sc; int n, i; // 存储节目开始时间和结束时间 short[][] time; // 最终可以看的节目数量 int number; public Main() { sc = new Scanner(System.in); n = sc.nextInt(); while(n > 0) { // 输入时间 input(); // 排序 sort(); // 统计可以看的节目数量 number = statistics(); System.out.println(number); n = sc.nextInt(); } sc.close(); } /** * 输入节目的开始时间和结束时间 */ private void input() { time = new short[n][2]; for(i = 0; i < n; i++) { time[i][0] = sc.nextShort(); time[i][1] = sc.nextShort(); } } /** * 节目排序。规则如下: * 1、结束时间早的放在前面 * 2、结束时间相同,则开始时间早的放在前面 * 算法:选择排序 */ private void sort() { int j, min; for(i = 0; i < n; i++) { min = i; for(j = i; j < n; j++) { if(time[min][1] > time[j][1]) { min = j; } else if(time[min][1] == time[j][1]){ if(time[min][0] > time[j][0]) { min = j; } } } if(min != i) { swap(min, i); } } } /** * 交换节目位置,即交换时间 * @param a 需要交换的第一组数据的下标 * @param b 需要交换的第二组数据的下标 */ private void swap(int a, int b) { short temp; temp = time[a][0]; time[a][0] = time[b][0]; time[b][0] = temp; temp = time[a][1]; time[a][1] = time[b][1]; time[b][1] = temp; } /** * @return 返回统计的可以看的节目数量 */ private int statistics() { int number = 1; int previous = 0; for(i = 1; i < n; i++) { if(time[i][0] >= time[previous][1]) { number = number + 1; previous = i; } } return number; } public static void main(String[] args) { new Main(); } }

转载于:https://www.cnblogs.com/wowpH/p/11060843.html

最后

以上就是听话御姐最近收集整理的关于1187: 零起点学算法94——今年暑假不AC(Java)的全部内容,更多相关1187:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部