[CCF2015.12]题解

201512-1 数位之和

水题一个,取模除以10胡搞即可(不知道字符串为什么不行

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19 
20 using namespace std;
21 
22 int n;
23 
24 int main() {
25     while(~scanf("%d", &n)) {
26         int ans = 0;
27         while(n) {
28             ans += n % 10;
29             n /= 10;
30         }
31         printf("%d
", ans);
32     }
33     return 0;
34 }
1

201512-2 消除类游戏

按行按列枚举三个相邻的中点,看看左右是否和它相同颜色,如果相同就打标记,最后根据标记处理所有点。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19 
20 using namespace std;
21 
22 const int maxn = 33;
23 const int dx[4] = {0, 0, -1, 1};
24 const int dy[4] = {1, -1, 0, 0};
25 int n, m;
26 int G[maxn][maxn];
27 bool dis[maxn][maxn];
28 
29 bool ok(int i, int j) {
30     return i >= 0 && j >= 0 && i < n && j < m;
31 }
32 
33 int main() {
34     // freopen("in", "r", stdin);
35     while(~scanf("%d %d", &n, &m)) {
36         memset(dis, 0, sizeof(dis));
37         for(int i = 0; i < n; i++) {
38             for(int j = 0; j < m; j++) {
39                 scanf("%d", &G[i][j]);
40             }
41         }
42         for(int i = 0; i < n; i++) {
43             for(int j = 0; j < m; j++) {
44                 if(ok(i-1, j) && ok(i+1, j)) {
45                     if(G[i-1][j] == G[i+1][j] && G[i-1][j] == G[i][j] && G[i+1][j] == G[i][j]) {
46                         dis[i-1][j] = dis[i+1][j] = dis[i][j] = 1;
47                     }
48                 }
49             }
50         }
51         for(int i = 0; i < n; i++) {
52             for(int j = 0; j < m; j++) {
53                 if(ok(i, j-1) && ok(i, j+1)) {
54                     if(G[i][j-1] == G[i][j+1] && G[i][j-1] == G[i][j] && G[i][j+1] == G[i][j]) {
55                         dis[i][j-1] = dis[i][j+1] = dis[i][j] = 1;
56                     }
57                 }
58             }
59         }
60         for(int i = 0; i < n; i++) {
61             for(int j = 0; j < m; j++) {
62                 if(dis[i][j]) G[i][j] = 0;
63                 printf("%d ", G[i][j]);
64             }
65             printf("
");
66         }
67     }
68     return 0;
69 }
2

201512-3 画图

小模拟,注意好行和列即可,第一个样例提供了一个trick,那就是覆盖后的颜色还可以被继续覆盖,并且覆盖后可以在其上画线段。判断线段相交在每画一段的时候完成,假如画之前存在与它不一样方向的线段就变+(注意原本就是+的情况)。填充操作一遍dfs就行,和POJ的一个题一样。

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <iomanip>
  4 #include <cstring>
  5 #include <climits>
  6 #include <complex>
  7 #include <fstream>
  8 #include <cassert>
  9 #include <cstdio>
 10 #include <bitset>
 11 #include <vector>
 12 #include <deque>
 13 #include <queue>
 14 #include <stack>
 15 #include <ctime>
 16 #include <set>
 17 #include <map>
 18 #include <cmath>
 19 
 20 using namespace std;
 21 
 22 typedef struct Point {
 23     int x, y;
 24     Point() {}
 25     Point(int xx, int yy) : x(xx), y(yy) {}
 26 }Point;
 27 
 28 const int maxn = 111;
 29 const int dx[4] = {0, 0, 1, -1};
 30 const int dy[4] = {1, -1, 0, 0};
 31 
 32 char G[maxn][maxn];
 33 int n, m, q;
 34 Point a, b;
 35 int cmd;
 36 
 37 void init() {
 38     memset(G, 0, sizeof(G));
 39     for(int i = 0; i < n; i++) {
 40         for(int j = 0; j < m; j++) {
 41             G[i][j] = '.';
 42         }
 43     }
 44 }
 45 
 46 void line(Point a, Point b) {
 47     if(a.y == b.y) {
 48         if(a.x > b.x) {
 49             Point tmp = a;
 50             a = b;
 51             b = tmp;
 52         }
 53         for(int i = a.x; i <= b.x; i++) {
 54             if(G[i][a.y] == '-' || G[i][a.y] == '+') G[i][a.y] = '+';
 55             else G[i][a.y] = '|';
 56         }
 57     }
 58     else if(a.x == b.x) {    
 59         if(a.y > b.y) {
 60             Point tmp = a;
 61             a = b;
 62             b = tmp;
 63         }
 64         for(int i = a.y; i <= b.y; i++) {
 65             if(G[a.x][i] == '|' || G[a.x][i] == '+') G[a.x][i] = '+';
 66             else G[a.x][i] = '-';
 67         }
 68     }
 69 }
 70 bool ok(int i, int j) {
 71     return i >= 0 && j >= 0 && i < n && j < m;
 72 }
 73 
 74 void dfs(int x, int y, char s) {
 75     for(int i = 0; i < 4; i++) {
 76         int xx = x + dx[i];
 77         int yy = y + dy[i];
 78         if(ok(xx, yy) && !(G[xx][yy] == '|' || G[xx][yy] == '+' || G[xx][yy] == '-' ) && G[xx][yy] != s) {
 79             G[xx][yy] = s;
 80             dfs(xx, yy, s);
 81         }
 82     }
 83 }
 84 
 85 int main() {
 86     // freopen("in", "r", stdin);
 87     while(~scanf("%d %d", &m, &n)) {
 88         scanf("%d", &q);
 89         init();
 90         while(q--) {
 91             scanf("%d", &cmd);
 92             if(cmd == 0) {
 93                 scanf("%d %d %d %d", &a.y, &a.x, &b.y, &b.x);
 94                 line(a, b);
 95             }
 96             else {
 97                 char s[3];
 98                 memset(s, 0, sizeof(s));
 99                 scanf("%d %d", &a.y, &a.x);
100                 scanf("%s", s);
101                 dfs(a.x, a.y, s[0]);
102             }
103         }
104         for(int i = n - 1; i >= 0; i--) {
105             for(int j = 0; j < m; j++) {
106                 printf("%c", G[i][j]);
107             }
108             printf("
");
109         }
110     }
111     return 0;
112 }
3

201512-4 送货

图论小题,求一条不用回到原点的欧拉路,trick在图不连通的情况,因此要提前做一步连通性的判断。还有就是记录点的度,假如是奇数度的点为0个或者2个的时候是存在这样一条路的,假如奇数度点为1或者大于2则不存在。dfs的时候打好标记就可以了。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19 
20 using namespace std;
21 
22 const int maxn = 10005;
23 const int maxm = 100005;
24 vector<int>::iterator it;
25 vector<int> G[maxn];
26 bool vis[maxn][maxn];
27 int dig[maxn];
28 int n, m, top;
29 int st[maxm];
30 int pre[maxn];
31 
32 int find(int x) {
33     return x == pre[x] ? x : pre[x] = find(pre[x]);
34 }
35 
36 void unite(int x, int y) {
37     x = find(x);
38     y = find(y);
39     if(x != y) {
40         pre[y] = x;
41     }
42 }
43 inline void init() {
44     for(int i = 0; i < maxn; i++) {
45         pre[i] = i;
46     }
47 }
48 
49 void dfs(int u) {
50     for(int i = 0; i < G[u].size(); i++) {
51         if(!vis[u][G[u][i]]) {
52             vis[u][G[u][i]] = vis[G[u][i]][u] = 1;
53             dfs(G[u][i]);
54             st[top++] = G[u][i];
55         }
56     }
57 }
58 
59 int main() {
60     // freopen("in", "r", stdin);
61     int a, b;
62     while(~scanf("%d %d", &n, &m)) {
63         init();
64         memset(dig, 0, sizeof(dig));
65         memset(vis, 0, sizeof(vis));
66         for(int i = 0; i < n + 5; i++) G[i].clear();
67         for(int i = 0; i < m; i++) {
68             scanf("%d %d", &a, &b);
69             G[a].push_back(b);
70             G[b].push_back(a);
71             unite(a, b);
72             dig[a]++; dig[b]++;
73         }
74         int odd = 0;
75         bool exflag = 0;
76         int father = find(1);
77         for(int i = 1; i <= n; i++) {
78             if(father != find(i)) exflag = 1;
79             sort(G[i].begin(), G[i].end());
80             if(dig[i] & 1) odd++;
81         }
82         if(odd == 1 || odd > 2 || exflag) {
83             puts("-1");
84             continue;
85         }
86         top = 0;
87         dfs(1);
88         printf("1");
89         while(top--) printf(" %d", st[top]);
90         printf("
");
91     }
92     return 0;
93 }
4

201512-5 矩阵

题目好长QAQ,不想做QAQ

看了一眼题应该是矩阵快速幂,怎么看怎么是个烂题。。

原文地址:https://www.cnblogs.com/kirai/p/5354174.html