2019年第十屆藍橋杯c++A組java/c++組題解


 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 vector <int > vec;
 5 long long sum;
 6 int main(){
 7     for(int i=1;i<=2019;i++){
 8         int cnt=0;
 9         int a[4]={0,0,0,0};
10         int t=i; 
11         while(t){
12             a[cnt]=t%10;
13             t/=10;
14             cnt++;
15         }
16         for(int j=0;j<cnt;j++){
17             if(a[j]==0||a[j]==2||a[j]==1||a[j]==9){
18                 vec.push_back(i);
19                 break;
20             }
21         }
22     } 
23     for(vector<int>::iterator it = vec.begin(); it != vec.end(); it++){
24         sum+=(*it)*(*it);
25     } 
26     cout<<sum;
27     return 0;
28 }

 思路是將1到2019的所有數字的每一位存到a數組里,然后比較它的每一位看有沒有2,0,1,9;如果有就插入到隊列里面。

然后將隊列中每個元素的平方存到sum里再輸出。

第二題

 1 #include<iostream>
 2 using namespace std; 
 3 int main(){
 4     int d;
 5     int a=1,b=1,c=1;
 6     for(int i=4;i<=20190324;i++){
 7         d=(a+b+c)%10000;
 8         a=b%10000;
 9         b=c%10000;
10         c=d%10000;
11     }
12     cout<<d;
13 }

這個就是要注意模10000來避免超范圍。

第3題

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

 

 

解法1

 1 #include <iostream>
 2 #include <string>
 3 #include <queue>
 4 
 5 using namespace std;
 6 
 7 string ss[35];
 8 int maze[35][55];
 9 int dir[4][2] = { { 1, 0 }, { 0, -1 }, { 0, 1 }, { -1, 0 } };
10 char letter[4] = { 'D', 'L', 'R', 'U' };
11 int cnt = 10000;
12 bool vis[35][55];
13  
14 struct node
15 {
16     int x;
17     int y;
18     string s;
19     int step;
20     node(int xx, int yy, string ss, int st)
21     {
22         x = xx;
23         y = yy;
24         s = ss;
25         step = st;
26     }
27 };
28 
29 bool in(int x, int y)
30 {
31     if (x < 30 && x >= 0 && y < 50 && y >= 0)
32     {
33         return true;
34     }
35     return false;
36 }
37 
38 void bfs(int x, int y, string s, int step)
39 {
40     queue<node> q;
41     q.push(node(x, y, s, step));
42     while (!q.empty())
43     {
44         node now = q.front();
45         q.pop();
46 
47         vis[now.x][now.y] = true;
48 
49         if (now.x == 29 && now.y == 49)
50         {
51             if (now.step < cnt)
52             {
53                 cnt = now.step;
54                 cout << now.step << " : " << now.s << endl;
55                 
56             }
57             continue;
58         }
59 
60         for (int i = 0; i < 4; i++)
61         {
62             int tx = now.x + dir[i][0];
63             int ty = now.y + dir[i][1];
64 
65             if (maze[tx][ty] != 1 && !vis[tx][ty] && in(tx, ty))
66             {
67                 q.push(node(tx, ty, now.s + letter[i], now.step + 1));
68             }
69         }
70     }
71 }
72 
73 int main()
74 {
75     for (int i = 0; i < 30; i++)
76     {
77         cin >> ss[i];
78     }
79 
80     for (int i = 0; i < 30; i++)
81     {
82         for (int j = 0; j < 50; j++)
83         {
84             maze[i][j] = (ss[i][j] - '0');
85         }
86     }
87 
88     int step = 0;
89     string s = "";
90     bfs(0, 0, s, step);
91 
92     system("pause");
93     return 0;
94 }

解法2:yxc做的

 1 #include <cstring>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <set>
 5 #include <queue>
 6 
 7 using namespace std;
 8 const int N = 55;
 9 int n, m;
10 string g[N];
11 int dist[N][N];
12 int dx[4] = {1, 0, 0, -1}, dy[4] = {0, -1, 1, 0};
13 char dir[4] = {'D', 'L', 'R', 'U'};
14 
15 void bfs()
16 {
17     queue<pair<int,int>> q;
18     memset(dist, -1, sizeof dist);
19     dist[n - 1][m - 1] = 0;
20     q.push({n - 1, m - 1});
21     while (q.size())
22     {
23         auto t = q.front();
24         q.pop();
25         for (int i = 0; i < 4; i ++ )
26         {
27             int x = t.first + dx[i], y = t.second + dy[i];
28             if (x >= 0 && x < n && y >= 0 && y < m && dist[x][y] == -1 && g[x][y] == '0')
29             {
30                 dist[x][y] = dist[t.first][t.second] + 1;
31                 q.push({x, y});
32             }
33         }
34     }
35 }
36 int main()
37 {
38     cin >> n >> m;
39     for (int i = 0; i < n; i ++ ) cin >> g[i];
40     bfs();
41     cout << dist[0][0] << endl;
42     int x = 0, y = 0;
43     string res;
44     while (x != n - 1 || y != m - 1)
45     {
46         for (int i = 0; i < 4; i ++ )
47         {
48             int nx = x + dx[i], ny = y + dy[i];
49             if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] == '0')
50             {
51                 if (dist[x][y] == 1 + dist[nx][ny])
52                 {
53                     x = nx, y = ny;
54                     res += dir[i];
55                     break;
56                 }
57             }
58         }
59     }
60     cout << res << endl;
61     return 0;
62 }

解法3,先輸入30,50,行號列號
 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 char ma[501][501];
 5 bool visit[501][501];
 6 int dx[4] = {1, 0, 0, -1};
 7 int dy[4] = {0, -1, 1, 0};
 8 char d[4] = {'D', 'L', 'R', 'U'};
 9 int main() {
10     int n, m;
11     cin >> n >> m;
12     for (int i = 1; i <= n; i++) {
13         for (int j = 1; j <= m; j++) {
14             cin >> ma[i][j];
15         }
16     }
17     queue<pair<int,int> > que;
18     queue<string> step;  //記錄路徑
19     visit[1][1] = true;
20     que.push(make_pair(1, 1));
21     step.push("");
22     while(!que.empty()) {
23         pair<int, int> top = que.front();
24         int x = top.first;
25         int y = top.second;
26         string s = step.front();
27         que.pop();
28         step.pop();
29         if (x == n && y == m) {
30             cout << s.length() << endl;
31             cout << s;
32             break;
33         }
34         for (int i = 0; i < 4; i++) {
35             int tx = x + dx[i];
36             int ty = y + dy[i];
37             string tem = s;
38             if (visit[tx][ty] == true || ma[tx][ty] == '1' || tx < 1 || tx > n || ty < 1 || ty > m) {
39                 continue;
40             }
41             tem = tem + d[i];
42             visit[tx][ty] = true;
43             step.push(tem);
44             que.push(make_pair(tx,ty));
45         }
46     }
47     return 0;
48 }
第四題

這題就是每周都找最大的。

第一周49 48 47 46 3個數   就是46

第二周45 44 43 42 3個數   就是42

第三周41 40 39 38 3個數   就是38

所以每周最大的都是-4;4,5,6,7周最大的中位數分別為34,30,26,22;

然后7周的就是22,26,30,34,38,42,46;

所以答案就是34


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM