2017年第八届蓝桥杯c/c++B组

此为博主为记录刷题过程的代码,修正并且都是正确答案,如果你需要具体解析,请在下方留言,我将会在赛后修改回答你的问题!

购物单

熟练使用EXCEL,少一半的事。

  1. 复制购物单放入txt文件中,按下CTRL + H 将**** 替换成(空格)、什么什么什么折换算成小数, 数字和数字之间的空格换为*
  2. 将数据放入Excel中。
  3. 计算统计得出答案。
    在这里插入图片描述

答案:5200

等差素数列

#include
#include
using namespace std;

const int N = 1000010;
int primes[N], cnt;
bool st[N];

void get_primes(int n) {
     
	for (int i = 2; i <= n; ++i) {
     
		if (!st[i]) primes[cnt++] = i;	
		for (int j = 0; i * primes[j] <= n; ++j) {
     
			st[primes[j] * i] = true;
			if (i % primes[j] == 0) break;
		}
	}
}

int main() {
     
	get_primes(N);
	for (int d = 1; ; ++d) {
     
		for (int i = 2; i <= N; ++i) {
     
			int len = 0, t = i;
			while (!st[t]) {
     
				t += d;
				len++;
			}
			if (len == 10) {
     
				cout << i << " " << d << endl;
				return 0;
			}
		}
	}
	return 0;
}

答案:210

承压计算

答案:72665192664

代码

#include
#include
#include
using namespace std;

#define _for_(i, a, b) for (int i = (a); i <= (b); ++i)
const int N = 30;
double g[N + 5][N + 5];

int main() {
     
	ifstream ifile("in.txt");

	_for_(i, 1, N - 1) _for_(j, 1, i)
		ifile >> g[i][j];
	
	_for_(i, 2, N) _for_(j, 1, i)
		g[i][j] += (g[i - 1][j - 1] + g[i - 1][j]) / 2.0;

	sort(g[N], g[N] + N + 1);
	printf("%lf\n", g[N][N]* (((long long)2086458231) / g[N][1]));

	ifile.close();
	return 0;
}

方格分割

答案:509

代码

#include
using namespace std;

const int N = 7;
bool vis[N][N];
int ans, dx[] = {
      -1, 0, 1, 0 }, dy[] = {
     0, 1, 0, -1};

void dfs(int x, int y) {
     
	vis[x][y] = true;
	vis[6 - x][6 - y] = true;
	if (!x || !y || x == 6 || y == 6) ans++;
	else {
     
		for (int i = 0; i < 4; ++i) {
     
			int p = x + dx[i], q = y + dy[i];
			if (p >= 0 && p < 7 && q >= 0 && q < 7 && !vis[p][q]) {
     
				dfs(p, q);
				vis[p][q] = false;
				vis[6 - p][6 - q] = false;
			}
		}
	}
}

int main() {
     
	dfs(3, 3);
	cout << ans / 4 << endl;
	return 0;
}

取数位

答案:f(x / 10, k)

最大公共子串

答案:a[i - 1][j - 1] + 1

日期问题

题目提交点

点我提交

代码

#include
#include
using namespace std;

const int N = 10;
struct Date {
     
	int y, m, d;
	bool operator < (const Date& p) const {
     
		if (y != p.y) return y < p.y;
		if (m != p.m) return m < p.m;
		return d < p.d;
	}
}date[10];
int k, day[] = {
     0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void slove(int y, int m, int d) {
     
	if (y <= 59) y += 2000;
	else y += 1900;

	if ((!(y % 4) && y % 100) || !y % 400) day[2] = 29;
	else day[2] = 28;

	if (m > 12 || m < 1) return;
	if (d > day[m] || d < 1) return;

	date[k++] = {
      y, m, d };
}

int main() {
     
	int a, b, c;
	scanf("%d/%d/%d", &a, &b, &c);

	slove(a, b, c);
	slove(c, a, b);
	slove(c, b, a);
	sort(date, date + k);

	printf("%d-%02d-%02d\n", date[0].y, date[0].m, date[0].d);
	for (int i = 1; i < k; ++i)
		if (!(date[i].y == date[i - 1].y &&date[i].m == date[i - 1].m &&date[i].d == date[i - 1].d))
		printf("%d-%02d-%02d\n", date[i].y, date[i].m, date[i].d);
	return 0;
}

包子凑数

题目提交点

点我提交

代码

#include
#include
using namespace std;

#define _for_(i, a, b) for (int i = (a); i <= (b); ++i)
const int N = 10010;
int a[110];
bool f[N];

int gcd(int a, int b) {
     
	return b ? gcd(b, a % b) : a;
}

int main() {
     
	int n;
	scanf("%d", &n);
	int d = 0;
	_for_(i, 1, n) {
     
		scanf("%d", &a[i]);
		d = gcd(d, a[i]);
	}

	if (d != 1) puts("INF");
	else {
     
		f[0] = true;
		_for_(i, 1, n)
			_for_(j, a[i], N - 1)
			f[j] |= f[j - a[i]];

		int ans = 0;
		_for_(i, 1, N - 1) if (!f[i]) ans++;
		printf("%d\n", ans);
	}
	return 0;
}

分巧克力

题目提交点

点我提交

代码

#include
#include
using namespace std;

#define _for(i, a, b) for (int i = (a); i < (b); ++i)
const int N = 100010;
int h[N], w[N], n, k;

bool check(int m) {
     
	int sum = 0;
	_for(i, 0, n)  sum += (h[i] / m) * (w[i] / m);
	return sum >= k;
}

int main() {
     
	scanf("%d%d", &n, &k);
	_for(i, 0, n) scanf("%d%d", &h[i], &w[i]);

	int l = 1, r = 1e5;
	while (l < r) {
     
		int mid = l + r + 1 >> 1;
		if (check(mid)) l = mid;
		else r = mid - 1;
	}
	printf("%d\n", l);
	return 0;
}

k倍区间

题目提交点

点我提交

思路

求子序列的和 => 前缀和算法
即: ( s u m r − s u m l − 1 ) % k = 0 (sum_r - sum_{l - 1} ) \% k = 0 (sumrsuml1)%k=0
s u m r ≡ s u m l − 1 m o d ( k ) sum_r \equiv sum_{l - 1} \quad mod(k) sumrsuml1mod(k)

代码

#include
#include
using namespace std;

#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
typedef long long LL;
const int N = 100000 + 10;
LL s[N], cnt[N];

int main() {
     
	int n, k;
	scanf("%d%d", &n, &k);
	_rep(i, 1, n) {
     
		scanf("%d", &s[i]);
		s[i] += s[i - 1];
	}

	LL ans = 0;
	cnt[0] = 1;
	_rep(i, 1, n) ans += cnt[s[i] % k]++;

	printf("%lld\n", ans);
	return 0;
}

你可能感兴趣的:(蓝桥杯)