首页 > 代码库 > BUCT蓝桥杯热身赛II题解
BUCT蓝桥杯热身赛II题解
A题 编码(decode)
签到题,没有可说的。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <set> 5 6 using namespace std; 7 8 int main () { 9 int mark[26];10 memset(mark,0,sizeof(mark));11 string child,goal;int N;12 cin >> child >> N >> goal;13 set<int>s;14 for (int i = 0;i < child.length();i++) {15 mark[(N - 1 + i) % 26] = child[i];16 s.insert(child[i]);17 }18 int pos = N - 1 + child.length();19 for (int i = ‘A‘;i <= ‘Z‘;i++) {20 if (s.count(i)) continue;21 pos %= 26;22 s.insert(i);23 mark[pos] = i;24 pos++;25 }26 for (int i = 0;i < goal.length();i++) {27 for (int j = 0;j < 26;j++) {28 if (goal[i] == mark[j]) cout << char(j + ‘A‘);29 }30 }31 }
B题 阶乘问题(fact)
水题,不多说。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 int a[4] = {6,2,4,8}; 8 9 int get_five(int n) {10 if (n == 0) return 0;11 return n / 5 + get_five(n / 5);12 }13 14 int jie(int n) {15 if (n == 0) return 1;16 else return n * jie(n - 1);17 }18 19 int f(int n) {20 if (n == 0) return 1;21 int a0 = n % 5;22 int j = (n - a0) / 5;23 return a[j % 4] * (jie(a0) % 10) * f(j) % 10;24 }25 26 int main () {27 int n;28 while (cin >> n)29 if (n == 1) cout << 1 << " " << 0 << endl;30 else cout << f(n) << " " << get_five(n) << endl;31 }
C题 集合(subset)
水题,没说的,set
用法
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <set> 5 #include <algorithm> 6 using namespace std; 7 8 #define ALL(x) x.begin() , x.end() 9 #define INS(x) inserter(x , x.begin())10 11 int main () {12 set<int>s1;13 set<int>s2;14 int n;15 int x;16 cin >> n;17 for (int i = 0;i < n;i++) {18 cin >> x;19 s1.insert(x);20 }21 cin >> n;22 for (int i = 0;i < n;i++) {23 cin >> x;24 s2.insert(x);25 }26 if (s1 == s2) {27 cout << "A equals B" << endl;28 }29 else {30 set<int>xx;31 set_intersection(ALL(s1),ALL(s2),INS(xx));32 if (xx == s1) {33 cout << "A is a proper subset of B" << endl;34 }35 else if (xx == s2) {36 cout << "B is a proper subset of A" << endl;37 }38 else if (xx.size() == 0) cout << "A and B are disjoint" << endl;39 else cout << "I‘m confused!" << endl;40 }41 }
D题 加工生产调度(prod)
简单贪心题,保证 x1 + max(x2,y1) + y2 < x2 + max(x1,y2) + y1
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 class Node { 9 public:10 int x,y;11 bool operator < (const Node rhs) const {12 return x + max(rhs.x,y) + rhs.y < rhs.x + max(x,rhs.y) + y;13 }14 };15 16 int main () {17 int N;18 // freopen("1.in","r",stdin);19 cin >> N;20 Node a[1010];21 for (int i = 0;i < N;i++) cin >> a[i].x;22 for (int i = 0;i < N;i++) cin >> a[i].y;23 sort(a,a + N);24 int start[1010];25 int end[1010];26 end[0] = a[0].x;27 for (int i = 1;i < N;i++) {28 end[i] = a[i].x + end[i - 1];29 start[i] = end[i - 1];30 }31 int maxn = end[N - 1];32 int ee = 0;33 for (int i = 0;i < N;i++) {34 ee = max(end[i],ee) + a[i].y;35 }36 cout << ee << endl;37 }
E题 帕斯卡的旅行(tour)
简单DP题 dp[i][j] 表示从起点到点(i,j)的路径数,所以结果是dp[n - 1][m - 1];
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 long long dp[35][35]; 8 int a[35][35]; 9 10 int main () {11 int n;12 // freopen("1.in","r",stdin);13 cin >> n;14 char ch;15 for (int i = 0;i < n;i++) {16 for (int j = 0;j < n;j++) {17 cin >> ch;18 a[i][j] = ch - ‘0‘;19 }20 }21 memset(dp,0,sizeof(dp));22 dp[0][0] = 1;23 for (int i = 0;i < n;i++) {24 for (int j = 0;j < n;j++) {25 if (a[i][j] == 0) continue;26 if (i + a[i][j] < n) {27 dp[i + a[i][j]][j] += dp[i][j];28 }29 if (j + a[i][j] < n) {30 dp[i][j + a[i][j]] += dp[i][j];31 }32 }33 }34 cout << dp[n - 1][n - 1] << endl;35 }
F题 泡泡龙(pop)
水题,简单模拟,广搜一下。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 6 using namespace std; 7 8 int dir[4][2] = {-1,0,0,1,1,0,0,-1}; 9 int mark[110][110];10 int a[110][110];11 int m,n;12 13 void BFS(int x,int y) {14 int ans = a[x][y];15 a[x][y] = 0;16 queue<pair<int,int> > Q;17 Q.push(make_pair(x,y));18 while (!Q.empty()) {19 pair<int,int> now = Q.front();Q.pop();20 for(int i = 0;i < 4;i++) {21 int xx = now.first + dir[i][0];22 int yy = now.second + dir[i][1];23 if (xx >= 0 && xx <= n && yy >= 0 && yy < n && a[xx][yy] == ans) {24 a[xx][yy] = 0;25 Q.push(make_pair(xx,yy));26 }27 }28 }29 }30 31 int main () {32 cin >> n >> m;33 for (int i = 0;i < n;i++) {34 for (int j = 0;j < n;j++) {35 cin >> a[i][j];36 }37 }38 BFS(n - 1,m - 1);39 for (int j = 0;j < n;j++) {40 int pos = n - 1;41 for (int i = n - 1;i >= 0;i--) {42 if (a[i][j] != 0) {43 a[pos][j] = a[i][j];44 pos--;45 }46 }47 for (int i = 0;i <= pos;i++) {48 a[i][j] = 0;49 }50 }51 for (int i = 0;i < n;i++) {52 for (int j = 0;j < n;j++) {53 cout << a[i][j] << " ";54 }55 cout << endl;56 }57 }
G题卫星照片(satel)
水题,跟某年的NOIP统计细胞很像,随便广搜一下就搞定了。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 6 using namespace std; 7 8 string str[80]; 9 int dir[4][2] = {-1,0,0,1,1,0,0,-1};10 int visit[80][80];11 int row,col;12 int oxnum = 0;13 int roomnum = 0;14 15 class Node {16 public:17 int x,y;18 Node (int xx,int yy) {19 x = xx;20 y = yy;21 }22 };23 24 void bfs(int x,int y) {25 queue<Node> Q;26 Q.push(Node(x,y));27 while (!Q.empty()) {28 Node now = Q.front();Q.pop();29 visit[now.x][now.y] = 1;30 for (int i = 0;i < 4;i++) {31 int xx = now.x + dir[i][0];32 int yy = now.y + dir[i][1];33 if (xx >= 0 && xx < row && yy >= 0 && yy < col && str[xx][yy] == ‘#‘) {34 str[xx][yy] = ‘!‘;35 Q.push(Node(xx,yy));36 }37 }38 }39 }40 41 int cover(int x,int y) {42 int rowbegin = x,colbegin = y;43 int i;44 for (i = x;i < row;i++) {45 if (str[i][y] != ‘!‘) break;46 }47 int rowend = i;48 for (i = y;i < col;i++) {49 if (str[x][i] != ‘!‘) break;50 }51 int colend = i;52 int ok = 1;53 for (i = rowbegin;i < rowend;i++) {54 for (int j = colbegin;j < colend;j++) {55 if (str[i][j] != ‘!‘) {56 return 0;57 }58 }59 }60 for (i = 0;i < row;i++) {61 for (int j = 0;j < col;j++) {62 if (visit[i][j]) {63 if (i < rowbegin || j < colbegin || i >= rowend || j >= colend) return 0;64 }65 }66 }67 return 1;68 }69 70 int main () {71 // freopen("1.in","r",stdin);72 cin >> row >> col;73 getchar();74 for (int i = 0;i < row;i++) {75 cin >> str[i];76 }77 for (int i = 0;i < row;i++) {78 for (int j = 0;j < col;j++) {79 if (str[i][j] == ‘#‘) {80 str[i][j] = ‘!‘;81 bfs(i,j);82 if (cover(i,j)) roomnum ++;83 else oxnum ++;84 memset(visit,0,sizeof(visit));85 }86 }87 }88 cout << roomnum << endl << oxnum << endl;89 }
BUCT蓝桥杯热身赛II题解
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。