HDU 3777 Page Count

/*******************************************************
 * @Nstd
 * 题号:HDU - 3777
 * 类型:模拟
 * 题目:给出打印时的页号(或范围或单页)
 *           并给定总页数n
 *           忽略这样的范围:a-b (a>b)
 *           忽略超过总页数的页号
 *           求最后需要打印几页
 * 思路:1.如果是单页:
 *              单页号m超过n则忽略,否则置范围为:m-m
 *           2.如果是双页(a-b)
 *              1)如果a>n则a置为n+1,否则为a
 *              2)如果b>n则b置为n,否则为b
 *            用以上的方法在判断的时候
 *            可以把超出范围的页数简化到范围内进行判断
 * 问题:一开始的时候开了很多if进行特判,wa了很多此
 *           第二天晚上回去想到了'思路2'的归一判断法
 * 特例:
 * Input:
 * 20
 * 31
 *
 * 20
 * 20
 *
 * 20
 * 19,20-27
 * Output:
 * 0
 * 1
 * 2
 *
 *                                    —— 2012/3/19
 *******************************************************/


hdu - 3777
 1 #include <stdio.h>
2 #include <iostream>
3 #include <string.h>
4 #include <algorithm>
5 using namespace std;
6
7 struct Page
8 {
9 int s, e;
10 }p[1010];
11
12 bool cmp(const Page& a, const Page& b)
13 {
14 return (a.s == b.s) ? (a.e < b.e) : (a.s < b.s);
15 }
16
17 int main()
18 {
19 int n, i, j, cnt, len, t, f;
20 char ip[1001];
21 while(scanf("%d", &n)!=EOF && n)
22 {
23 getchar();
24 gets(ip);
25 cnt = 0;
26 len = strlen(ip);
27 for(f=t=i=0; i<=len; i++)
28 {
29 if(ip[i]>='0' && ip[i]<='9')
30 {
31 t = t * 10 + ip[i]-'0';
32 }
33 else if(ip[i] == '-')
34 {
35 f++;
36 if(t < 1) t = 1;
37 p[cnt].s = (t>n ? n+1 : t);
38 t = 0;
39 }
40 else
41 {
42 if(f == 0)
43 {
44 if(t < n)
45 {
46 p[cnt].s = p[cnt].e = t;
47 cnt ++;
48 }
49 }
50 else
51 {
52 if(t > n) t = n;
53 p[cnt].e = t;
54 if(p[cnt].s <= p[cnt].e)
55 {
56 cnt++;
57 }
58 }
59 f = t = 0;
60 }
61 }
62 sort(p, p+cnt, cmp);
63 for(i=0; i<cnt; i++)
64 printf("%d-%d\n", p[i].s, p[i].e);
65 int ans = 0;
66 if(cnt)
67 {
68 int ss=p[0].s, ee=p[0].e;
69 for(i=1; i<cnt; i++)
70 {
71 if(ee + 1 >= p[i].s)
72 {
73 if(ee < p[i].e)
74 {
75 ee = p[i].e;
76 }
77 }
78 else
79 {
80 ans += (ee - ss + 1);
81 ss = p[i].s;
82 ee = p[i].e;
83 }
84 }
85 ans += (ee - ss + 1);
86 }
87 printf("%d\n", ans);
88 }
89 return 0;
90 }



原文地址:https://www.cnblogs.com/Nstd/p/2405944.html