快捷搜索:

2015年蓝桥杯(C/C++)省赛B组

题目:https://wenku.baidu.com/view/fe752820240c844768eaee06.html

第一题 奖券数目

代码:

#include<cstdio> #include <algorithm> using namespace std; int num[7] = {}, cnt = 0; bool book[23] = {}; int rec[485903] = {}; void dfs(int step) { if(step == 5) { cnt++; return; } for(int i = step == 0 ? 1 : 0; i <= 9; i++) { if(step == 0 && i == 0) continue; if(i == 4) continue; dfs(step + 1); } } int main(void) { dfs(0); printf("%d ", cnt); return 0; }

答案:52488

第二题 星系炸弹

代码:

#include<cstdio> using namespace std; int days[3][23] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; int main(void) { int yy = 2014, mm = 11, dd = 9; int add = 1000; //while(scanf("%d %d %d %d", &yy, &mm, &dd, &add)) { int cnt = 0; add -= (days[cnt][mm] - dd); mm++; while(add > days[cnt][mm]) { add -= days[cnt][mm++]; if(mm > 12) { mm = 1; yy++; if((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) { cnt++; } else cnt = 0; } } printf("%d-%d-%d ", yy, mm, add); } return 0; }

答案:2017-08-05

第三题 三羊献瑞

代码:

#include<cstdio> #include <algorithm> using namespace std; int arr[23] = {}, cnt = 0; int main(void) { for(int i = 0; i <= 9; i++) { arr[i] = i; } do { if(arr[0] != 0 && arr[4] != 0) { int sum1 = 0, ten = 1; for(int i = 3; i >= 0; i--) { sum1 += arr[i] * ten; ten *= 10; } int sum2 = arr[6] + arr[5] * 10 + arr[3] * 100 + arr[4] * 1000; int sum3 = arr[7] + arr[3] * 10 + arr[5] * 100 + arr[1] * 1000 + arr[0] * 10000; //printf("%d + %d == %d ( %d ) ", sum1, sum2, sum1 + sum2, sum3); if(sum1 + sum2 == sum3) { printf("%d ", sum1); } } } while(next_permutation(arr + 0, arr + 10)); return 0; }

答案:1085

第四题

代码:

#include <stdio.h> #include <string.h> void StringInGrid(int width, int height, const char *s) { int i, k; char buf[1000]; strcpy(buf, s); if(strlen(s) > width - 2) buf[width - 2] = 0; printf("+"); for(int i = 0; i < width-2; i++) printf("-"); printf("+ "); for(k = 1; k < (height - 1) / 2; k++) { printf("|"); for(i = 0; i < width - 2; i++) printf(" "); printf("| "); } printf("|"); printf("%*s%s%*s", ——————); //填空 printf("| "); for(k = (height - 1) / 2 + 1; k < height - 1; k++) { printf("|"); for(int i = 0; i < width - 2; i++) printf(" "); printf("| "); } printf("+"); for(i = 0; i < width - 2; i++) printf("-"); printf("+"); } int main() { StringInGrid(20, 6, "abcd1234"); return 0; }

填空:(width - strlen(s) - 2) / 2, " ", s, (width - strlen(s) - 1) / 2, "

第五题

代码:

#include <stdio.h> void test(int x[]) { int a = x[0] * 1000 + x[1] * 100 + x[2] * 10 + x[3]; int b = x[4] * 10000 + x[5] * 1000 + x[6] * 100 + x[7] * 10 + x[8]; if(a * 3 == b) printf("%d / %d ", a, b); } void f(int x[], int k) { int i, t; if(k >= 9) { test(x); return; } for(i = k; i < 9; i++) { t = x[k]; x[k] = x[i]; x[i] = t; f(x, k + 1); ————————//填空 } } int main() { int x[] = {1, 2, 3, 4, 5, 6,7,8,9}; f(x, 0); return 0; }

填空:{t = x[k]; x[k] = x[i]; x[i] = t}

第六题 加法变乘法

代码:

#include <cstdio> #include <queue> #include <cstring> using namespace std; bool book[54] = {}; int sum[65] = {}; void dfs(int step) { if(step == 2) { memset(sum, 0, sizeof(sum)); sum[1] = 1; for(int i = 2; i <= 49; i++) { if(book[i]) { sum[i] = sum[i - 2] + i * (i - 1); } else sum[i] = sum[i - 1] + i; } if(sum[49] == 2015) { for(int i = 1; i <= 49; i++) { if(book[i]) printf("%d ", i - 1); } printf(" "); } return; } for(int i = 2; i <= 49; i++) { if(!book[i] && !book[i + 1] && !book[i - 1]) { book[i] = true; dfs(step + 1); book[i] = false; } } } int main() { dfs(0); return 0; }

答案:16

第七题 牌型种类

代码:

#include <cstdio> #include <algorithm> #include <cstring> using namespace std; int num[34] = {}, cnt = 0, ans = 1; int book[324354][23] = {}; void dfs(int step) { if(step == 13) { int sum = 0; for(int i = 0; i < 13; i++) { sum += num[i]; } if(sum == 13) { cnt++; } int i; /*for(i = 1; i <= cnt; i++) { int j; for(j = 1; j <= 13; j++) { if(book[i][j] != num[j]) break; } if(j > 13) break; } if(i > cnt) { cnt++; for(int i = 1; i <= 13; i++) { book[cnt][i] = num[i]; } }*/ return; } for(int i = 0; i <= 4; i++) { num[step] = i; dfs(step + 1); num[step] = -1; } } int main(void) { dfs(0); printf("%d ", cnt); return 0; }

答案:3598180

第八题 移动距离

代码:

#include <cstdio> #include <cmath> #include <algorithm> #include <cstring> using namespace std; int getw(int hm, int m, int w) { if(hm % 2 == 0) return w - (m - (hm - 1) * w) + 1; else return m - (hm - 1) * w; } int main(void) { int w, m, n; while(scanf("%d %d %d", &w, &n, &m) != EOF) { if(m < n) swap(m, n); int hm = (m - 1) / w + 1; int hn = (n - 1) / w + 1; int wm = getw(hm, m, w); int wn = getw(hn, n, w); printf("%d %d %d %d ", hm, wm, hn, wn); printf("%d ", abs(hm - hn) + abs(wm - wn)); } return 0;

}

小结:这次可以说是非常大意

第一题没有申请题,以为数字不能重复,结果算错了,,,

第二题没有认真算,以为日期的第一天也算,结果加1了,然后就错了,,

第八题没有多考虑几组数据,结果也错了,,扎心了

经验分享 程序员 职场和发展