2016第七届蓝桥杯大赛软件类B组C/C++省赛题解

试题A:煤球数目(结果填空)

题意
在这里插入图片描述

做法:前缀前缀和

代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 210;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int main() {
    
     
  LL mid = 0, res = 0;
  _rep(1, 100, i) mid += i, res += mid;
  cout << res;
  return 0;
}

答案:171700


试题B:生日蜡烛(结果填空)

题意
在这里插入图片描述

做法:前缀和累加,再双重循环判断区间差是否为236。

代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 210;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int a[100];
int main() {
    
     
  int res = 0;
  _for(1, 100, i) a[i] = a[i-1] + i;
  _for(1, 100, i) {
    
    
    _for(0, i, j) if(a[i] - a[j] == 236) {
    
    
      cout << j << " " << i << endl;
    } 
  }
  return 0;
}

答案:26


试题 C:凑算式(结果填空)

题意
在这里插入图片描述
在这里插入图片描述
做法:全排列函数next_permutation
代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 210;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int a[10] = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9};
int main() {
    
     
  int fz, fm, res = 0;
  do {
    
    
    fz = (a[6]*100+a[7]*10+a[8]) * a[1] + (a[3]*100+a[4]*10+a[5])*a[2];
    fm = (a[6]*100+a[7]*10+a[8]) * a[2];
    if(fz % fm) continue;
    if(fz / fm + a[0] == 10) ++res;
  } while(next_permutation(a, a+9));
  cout << res;
  return 0;
}

答案:29


试题 D:快速排序(结果填空)

题意
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

做法:凑

答案:swap(a, j, p);


试题 E:抽签(结果填空)

题意
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

做法:凑,m-j也是可以的,i==j
答案:f(a, k+1, m-i, b);


试题 F:方格填数(结果填空)

题意
在这里插入图片描述

做法:全排列函数next_permutation
代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 210;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int a[11] = {
    
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int tu[5][5];
int main() {
    
     
  int tx, ty;
  LL res = 0;
  do{
    
    
    tu[1][2] = a[0]; tu[1][3] = a[1]; tu[1][4] = a[2];
    tu[2][1] = a[3]; tu[2][2] = a[4]; tu[2][3] = a[5]; tu[2][4] = a[6];
    tu[3][1] = a[7]; tu[3][2] = a[8]; tu[3][3] = a[9];
    int flag = 1;
    _for(1, 4, i) _for(1, 5, j) {
    
    
      if((i == 1 && j == 1) || (i == 3 && j == 4)) continue;
      _for(0, 8, k) {
    
    
        tx = i + ne[k][0]; ty = j + ne[k][1];
        if(tx <= 0 || ty <= 0 || tx > 3 || ty > 4) continue;
        if((tx == 1 && ty == 1) || (tx == 3 && ty == 4)) continue;
        if(abs(tu[i][j]-tu[tx][ty]) == 1) flag = 0;
      }
    }
    if(flag) ++res;
  } while(next_permutation(a, a+10));
  cout << res;
  return 0;
}

答案:1580


试题 G:剪邮票(结果填空)

题意
在这里插入图片描述
在这里插入图片描述

做法:先给每个格子标号,共 C 12 5 C_{12}^{5} C125种方法,五重循环选取五个格子,再判断这个方案是否正确即可,这篇题解很nice。
代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 210;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int a[20] = {
    
    0, 1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14};
int nex[4] = {
    
    -1, 1, -5, 5}; //四个方向 
int vis[20], bv[20];
int istr(int s) {
    
    
  queue<int> q;
  q.push(s); bv[s] = 1;
  int tx, cnt = 1;
  while(!q.empty()) {
    
    
    s = q.front(); q.pop();
    _for(0, 4, i) {
    
    
      tx = s + nex[i];
      if(tx == 5 || tx == 10) continue;
      if(tx >= 1 && tx <= 14 && vis[tx] && !bv[tx]) {
    
    
        q.push(tx); bv[tx] = 1; ++cnt;
        if(cnt == 5) return 1;
      }
    }
  }
  return 0;
}
int main() {
    
    
  int res = 0;
  _rep(1, 8, b) _rep(b+1, 9, c) _rep(c+1, 10, d)
  _rep(d+1, 11, e) _rep(e+1, 12, f) {
    
    
    _rep(1, 14, h) vis[h] = bv[h] = 0;
    vis[a[b]] = vis[a[c]] = vis[a[d]] = vis[a[e]] = vis[a[f]] = 1;
    if(istr(a[b])) ++res; 
  }
  cout << res;
  return 0;
}

答案:116


试题 H:四平方和(程序设计)

题意

在这里插入图片描述
在这里插入图片描述
做法:三重循环暴力三个数字,最后判断第四个数字是否是一个平方数,时间复杂度表面看上去很高,但是有四平方定理证明了四个数字中有一个数字很小,只有一组数据,一旦找到直接终止,复杂度近似 O ( n 2 ) O(n^2) O(n2)
代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 2500;
const LL Mod = 1e9+7;
const int M = 1e6+10;
unordered_map<int, int> mp;
int main() {
    
    
  int n; scanf("%d", &n);
  int m = sqrt(n), p, mid;
  _rep(0, m, i) _rep(i, m, j) _rep(j, m, k) {
    
    
    if(i*i+j*j+k*k <= n) {
    
    
      mid = n-i*i-j*j-k*k;
      p = sqrt(mid);
      if(p*p == mid) {
    
    
        printf("%d %d %d %d\n", i, j, k, p);
        return 0;
      }
    }
  }
  return 0;
}


试题 I:交换瓶子(程序设计)

题意
在这里插入图片描述
在这里插入图片描述

做法:模拟,每一次都将最小的那个未归位的数字归位,模拟过程中记录下标的数组更新很容易跟数组中两数字交换搞混,故用了四个变量记录。

代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 10010;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int a[N], p[N];
int main() {
    
    
  int n; scanf("%d", &n);
  _rep(1, n, i) scanf("%d", &a[i]), p[a[i]] = i;
  int k = 0, res = 0, flag;
  int p1, p2, a1, a2;
  while(1) {
    
     
    if(k > n) break; 
    if(a[k] == k) {
    
    
      ++k; continue;
    }
    p1 = k; p2 = p[k]; a1 = a[k]; a2 = a[p[k]]; //这里容易弄混
    swap(a[p1], a[p2]);
    p[a1] = p[p1]; p[p1] = p1;
    flag = 1; ++res; ++k;
    
    /*_rep(1, n, i) cout << a[i] <<  " ";
    cout << endl; 
    _rep(1, n, i) cout << p[i] <<  " ";
    cout << endl << endl;*/
    
    _rep(1, n, i) if(a[i] != i) {
    
    
      flag = 0; break;
    }
  }
  printf("%d\n", res);
  return 0;
}


试题 J:最大比例(程序设计)

题意
在这里插入图片描述

做法:暂咕


猜你喜欢

转载自blog.csdn.net/qq_43408978/article/details/108308892
今日推荐