Contest - 第10届“新秀杯”ACM程序设计大赛现场热身赛 赛后信息(题解)

  Problem Id Title
  Problem A A+B
  Problem B 统计字数
  Problem C 生日计算
  Problem D 冬瓜的寒假之旅

Problem A(略)

Problem B

B题目就是一个统计字符的简单模拟,纵向记录横向输出就可。先for一圈遍历遍最大的个数可确定高度。

 1 /****************************************/
 2 /*****            Desgard_Duan        *****/
 3 /****************************************/
 4 //#pragma comment(linker, "/STACK:102400000,102400000")
 5 #define _CRT_SECURE_NO_WARNINGS
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstdlib>
 9 #include <cstring>
10 #include <string>
11 #include <algorithm>
12 #include <stack>
13 #include <map>
14 #include <queue>
15 #include <vector>
16 #include <set>
17 #include <functional>
18 #include <cmath>
19 #include <numeric>
20 
21 using namespace std;
22 
23 vector<string> ans;
24 int let[26];
25 
26 int main () {
27     string text = "", in;
28     for (int i = 0 ; i < 4; ++ i) {
29         getline(cin, in);
30         text += in;
31     }
32     //cout << text << endl;
33     memset (let, 0, sizeof (let));
34     int Max = 0;
35     for (int i = 0; i < text.size(); ++ i) {
36         if (isalpha(text[i])) {
37             let[text[i] - 'A'] ++;
38             Max = max (Max, let[text[i] - 'A']);
39         }
40     }
41     for (int i = 1; i <= Max; ++ i) {
42         string text_line = "";
43         for (int j = 0 ; j < 26; ++ j) {
44             if (i > Max - let[j]) {
45                 text_line += "*";
46             } else {
47                 text_line += " ";
48             }
49             if (j != 25) text_line += " ";
50         }
51         cout << text_line << endl;
52     }
53     puts("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
54     return 0;
55 }

 

Problem C

考虑闰年的2月29日生日就可,其他没有什么坑。

 1 /****************************************/
 2 /*****            Desgard_Duan        *****/
 3 /****************************************/
 4 //#pragma comment(linker, "/STACK:102400000,102400000")
 5 #define _CRT_SECURE_NO_WARNINGS
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstdlib>
 9 #include <cstring>
10 #include <string>
11 #include <algorithm>
12 #include <stack>
13 #include <map>
14 #include <queue>
15 #include <vector>
16 #include <set>
17 #include <functional>
18 #include <cmath>
19 #include <numeric>
20 
21 using namespace std;
22 
23 int judge( int x ) {
24     if( x % 400 == 0 || ( x % 4 == 0 && x % 100  != 0 ) )
25         return 1;
26     return 0;
27 }
28 int main() {
29     int y, m, d, t;
30     while(scanf( "%d", &t ) != EOF) {
31         while( t-- ) {
32             scanf( "%d-%d-%d", &y, &m, &d );
33             int sum = 0;
34             if( judge( y ) && m == 2 && d == 29 && !judge( y + 30 ) ) {
35                 puts( "-1" );
36                 continue;
37             }
38             for( int i = 0; i <= 30; ++i )
39                 if(judge( y + i ))
40                     sum ++;
41             if( judge( y + 30 ) && m <= 2 && d < 28 )
42                 --sum;
43             if( judge( y ) && m >= 3 )
44                 --sum;
45             printf( "%d\n", sum + 30 * 365 );
46         }
47     }
48     return 0;
49 }

 

Problem D

主要考察枚举法。即全排列。在数据量不大的情况下,即可将TSP问题的所有情况全部列出,寻求最佳值。

另外可以学习一下next_permutation函数的用法,白书中也有介绍。

 

 1 #include <cstring>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cctype>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 
13 using namespace std;
14 
15 int per[10];
16 
17 struct point {
18     double x, y;
19 } p[10];
20 
21 double dis(point a, point b) {
22     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
23 }
24 
25 int main() {
26     //freopen("in.txt","r",stdin);
27     //freopen("out.txt","w",stdout);
28     int T, n, cas = 1;
29     double ans;
30     bool f;
31     scanf("%d", &T);
32     while(T--) {
33         f = true;
34         scanf("%d", &n);
35         for(int i = 1; i <= n; ++i) {
36             scanf("%lf%lf", &p[i].x, &p[i].y);
37         }
38         for(int i = 1; i <= n; ++i) {
39             per[i - 1] = i;
40         }
41         do {
42             point tmp;
43             tmp.x = 0;
44             tmp.y = 0;
45             double sum = dis(tmp, p[per[0]]);
46             for(int i = 1; i < n; ++i) {
47                 sum += dis(p[per[i]], p[per[i - 1]]);
48             }
49             sum += dis(tmp, p[per[n - 1]]);
50             if(f) {
51                 ans = sum;
52                 f = false;
53             } else
54                 ans = min(ans, sum);
55         } while(next_permutation(per, per + n));
56         printf("Case #%d: %.2lf\n", cas++, ans);
57     }
58     return 0;
59 }

 

 


 

posted @ 2014-11-15 17:43  Desgard_Duan  阅读(255)  评论(0编辑  收藏  举报