18年蓝桥杯省赛C/C++B组

本文涵盖了多种信息技术问题,包括日期计算,耐摔测试的最坏情况测试次数,16点阵汉字编码的解密,多位数乘积尾零的计算,快速排序算法的理解,递增三元组的计数,螺旋折线路径的求解以及日志统计中点赞查询的高效方法。涉及算法、编码解析和数据处理策略。
摘要由CSDN通过智能技术生成

1:第几天

2000年的1月1日,是那一年的第1天。
那么,2000年的5月4日,是那一年的第几天?
注意:需要提交的是一个整数,不要填写任何多余内容。

//31+29+31+30+4=125
//闰年判断
if(x%4==0&&x%100!=0 || x%400==0)
//润年2月29天,平年28天。

2:测试次数

x星球的居民脾气不太好,但好在他们生气的时候唯一的异常举动是:摔手机。
各大厂商也就纷纷推出各种耐摔型手机。x星球的质监局规定了手机必须经过耐摔测试,并且评定出一个耐摔指数来,之后才允许上市流通。
x星球有很多高耸入云的高塔,刚好可以用来做耐摔测试。塔的每一层高度都是一样的,与地球上稍有不同的是,他们的第一层不是地面,而是相当于我们的2楼。
如果手机从第7层扔下去没摔坏,但第8层摔坏了,则手机耐摔指数=7。
特别地,如果手机从第1层扔下去就坏了,则耐摔指数=0。
如果到了塔的最高层第n层扔没摔坏,则耐摔指数=n
为了减少测试次数,从每个厂家抽样3部手机参加测试。
某次测试的塔高为1000层,如果我们总是采用最佳策略,在最坏的运气下最多需要测试多少次才能确定手机的耐摔指数呢?
请填写这个最多测试次数。
注意:需要填写的是一个整数,不要填写任何多余内容。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <set>
#include <map>
#define ll long long
const int mod = 1e9+7;
const ll ds = 1e15+7;
using namespace std;


int dp[5][1005] = {0};
int main()
{
	int n,m,ans = mod;
	cin >> n >> m;
	for(int i = 1; i <= m; i++) dp[1][i] = i;
	dp[1][1] = 1;
	dp[2][1] = 1;
	dp[3][1] = 1;
	for(int i = 2; i <= n; i++){
		for(int j = 2; j <= m; j++){
			ans = 100;
			for(int k = 1; k < j; k++){
				int l = max(dp[i-1][k-1],dp[i][j-k]);
				ans = min(ans,l);
			}
			dp[i][j] = ans+1;
		}
	}
	cout << dp[n][m] << endl;
	return 0;
}
//答案为19

3:明码

汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛。
16点阵的字库把每个汉字看成是16x16个像素信息。并把这些信息记录在字节中。
一个字节可以存储8位信息,用32个字节就可以存一个汉字的字形了。
把每个字节转为2进制表示,1表示墨迹,0表示底色。每行2个字节,
一共16行,布局是:
第1字节,第2字节
第3字节,第4字节
….
第31字节, 第32字节
这道题目是给你一段多个汉字组成的信息,每个汉字用32个字节表示,这里给出了字节作为有符号整数的值。
题目的要求隐藏在这些信息中。你的任务是复原这些汉字的字形,从中看出题目的要求,并根据要求填写答案。
这段信息是(一共10个汉字):
4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0
16 64 16 64 34 68 127 126 66 -124 67 4 66 4 66 -124 126 100 66 36 66 4 66 4 66 4 126 4 66 40 0 16
4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0
0 -128 64 -128 48 -128 17 8 1 -4 2 8 8 80 16 64 32 64 -32 64 32 -96 32 -96 33 16 34 8 36 14 40 4
4 0 3 0 1 0 0 4 -1 -2 4 0 4 16 7 -8 4 16 4 16 4 16 8 16 8 16 16 16 32 -96 64 64
16 64 20 72 62 -4 73 32 5 16 1 0 63 -8 1 0 -1 -2 0 64 0 80 63 -8 8 64 4 64 1 64 0 -128
0 16 63 -8 1 0 1 0 1 0 1 4 -1 -2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 5 0 2 0
2 0 2 0 7 -16 8 32 24 64 37 -128 2 -128 12 -128 113 -4 2 8 12 16 18 32 33 -64 1 0 14 0 112 0
1 0 1 0 1 0 9 32 9 16 17 12 17 4 33 16 65 16 1 32 1 64 0 -128 1 0 2 0 12 0 112 0
0 0 0 0 7 -16 24 24 48 12 56 12 0 56 0 -32 0 -64 0 -128 0 0 0 0 1 -128 3 -64 1 -128 0 0

注意:需要提交的是一个整数,不要填写任何多余内容。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <set>
#include <map>
#define ll long long
const int mod = 1e9+7;

using namespace std;

void print(int x)
{
	if(x < 0){
		x = -x;
		x = ~x;
		x += 1;
		//x += 256;
	}
	for(int i = 7; ~i ; i--){
		if(x & (1 << i)) printf("*");
		else printf(" ");
	}
}

int main()
{
    int n,m;
    int a[11][35]; 
    n = 10,m = 32;
    for(int i = 1; i <= 10; i++){
    	for(int j = 1; j <= 32; j++){
    		int x;
    		cin >> a[i][j];
		}
	}
	for(int i = 1; i <= 10; i++){
		for(int j = 1;  j <= 32; j++){
			print(a[i][j]);
			if(j % 2 == 0) printf("\n");
		}
	}
//	ll ans = 9;
//	for(int i = 1; i < 9; i++){
//		ans *= 9;
//	}
    ll ans = pow(9,9);
	printf("%lld",ans);
	return 0;
}
//输出问题为九的九次方等于多少?
//答案:387420489

4:乘积尾零

如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零?
5650 4542 3554 473 946 4114 3871 9073 90 4329
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899
1486 5722 3135 1170 4014 5510 5120 729 2880 9019
2049 698 4582 4346 4427 646 9742 7340 1230 7683
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649
6701 6645 1671 5978 2704 9926 295 3125 3878 6785
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074
689 5510 8243 6114 337 4096 8199 7313 3685 211
思路一: 任何一个数中有零都是直接或间接通过2*5得来的,所以我们只需要判断每个数的可以分解出多少个2或5,然后取两者中小的那个就是答案。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <set>
#include <map>
#define ll long long
const int mod = 1e9+7;
const ll ds = 1e15+7;
using namespace std;

int main()
{
	int x,cnt5 = 0,cnt2 = 0;
	for(int i = 1; i <= 100; i++){
		cin >> x;
		int k = x;
		while(x % 2 == 0){
			cnt2++;
			x /= 2;
		}
		while(k % 5 == 0){
			cnt5++;
			k /= 5;
		}
	}
	cout << min(cnt2,cnt5);
	return 0;
}
//答案为31。

5:快速排序

以下代码可以从数组a[]中找出第k小的元素。
它使用了类似快速排序中的分治算法,期望时间复杂度是O(N)的。
请仔细阅读分析源码,填写划线部分缺失的内容。

#include <stdio.h>

int quick_select(int a[], int l, int r, int k) {
	int p = rand() % (r - l + 1) + l;
	int x = a[p];
	{int t = a[p]; a[p] = a[r]; a[r] = t;}
	int i = l, j = r;
	while(i < j) {
		while(i < j && a[i] < x) i++;
		if(i < j) {
			a[j] = a[i];
			j--;
		}
		while(i < j && a[j] > x) j--;
		if(i < j) {
			a[i] = a[j];
			i++;
		}
	}
	a[i] = x;
	p = i;
	if(i - l + 1 == k) return a[i];
	if(i - l + 1 < k) return quick_select( _____________________________ ); //填空
	else return quick_select(a, l, i - 1, k);
}
	
int main()
{
	int a[] = {1, 4, 2, 8, 5, 7, 23, 58, 16, 27, 55, 13, 26, 24, 12};
	printf("%d\n", quick_select(a, 0, 14, 5));
	return 0;
}

答案:a,i+1,r,k-(i-l+1)

6:递增三元组

标题:递增三元组
给定三个整数数组
A = [A1, A2, … AN],
B = [B1, B2, … BN],
C = [C1, C2, … CN],
请你统计有多少个三元组(i, j, k) 满足:

  1. 1 <= i, j, k <= N
  2. Ai < Bj < Ck

【输入格式】
第一行包含一个整数N。
第二行包含N个整数A1, A2, … AN。
第三行包含N个整数B1, B2, … BN。
第四行包含N个整数C1, C2, … CN。

对于30%的数据,1 <= N <= 100
对于60%的数据,1 <= N <= 1000
对于100%的数据,1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000

【输出格式】
一个整数表示答案
【样例输入】
3
1 1 1
2 2 2
3 3 3
【样例输出】
27
方法一:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

vector<int>a,b,c;
int main() {
	int n,t;
	while(~scanf("%d",&n)){
	for(int i = 1; i <= n; i++) {
		scanf("%d",&t);
		a.push_back(t);
	}
	for(int i = 1; i <= n; i++) {
		scanf("%d",&t);
		b.push_back(t);
	} 
	for(int i = 1; i <= n; i++) {
	    scanf("%d",&t);
		c.push_back(t);	
	} 
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	sort(c.begin(),c.end());
	long long sum = 0;
	for(int i = 0; i < n; i++){
		int k,p;
		k = lower_bound(a.begin(),a.end(),b[i]) - a.begin();
		p = upper_bound(c.begin(),c.end(),b[i]) - c.begin();
		p = n - p;
		sum += 1ll*k*p;
	}
	printf("%lld\n",sum);
	a.clear();
	b.clear();
	c.clear();
}
	return 0;
}

方法二:

#include <bits/stdc++.h>
#define ll long long
const int mod = 1e9+7;
const ll ds = 1e15+7;
using namespace std;

int a[1000005],b[1000005],c[1000005];
int cnt1[1000005] = {0},cnt2[1000005] = {0};
int main()
{
    int n;
    while(cin >> n){
	int mmax = -1,mmin = 1000000,m = -1;
	memset(cnt1,0,sizeof(cnt1));
	memset(cnt2,0,sizeof(cnt2));
    for(int i = 1; i <= n; i++) cin >> a[i],cnt1[a[i]]++;
	for(int i = 1; i <= n; i++){
		cin >> b[i];
		mmax = max(mmax,b[i]);
		mmin = min(mmin,b[i]);
	} 
	for(int i = 1; i <= n; i++) cin >> c[i],cnt2[c[i]]++,m = max(m,c[i]);
	for(int i = 1; i <= mmax+10; i++){
		cnt1[i] += cnt1[i-1];
	}
	for(int i = m; i >= mmin; i--){
		cnt2[i] += cnt2[i+1];
	}
	ll ans = 0;
	//cout << cnt1[1] << cnt2[3] << endl;
	for(int i = 1; i <= n; i++){
		ans += cnt1[b[i]-1] * cnt2[b[i]+1];
	}
	cout << ans << endl; 
}
	return 0;
}

7:螺旋折线

题目链接:https://www.dotcpp.com/oj/problem2285.html
思路:
1)由图可以看出把坐标为(-1,-1)(-2,-2) …(-n,-n)这些点连的直线往下旋转90便可连接成一个正方形,然后找出这个点在第几个正方形。
2)怎么找呢,很简单,横坐标的绝对值和纵坐标绝对值最大的那个t就是,然后求出前面几个正方形的长度和,不包括这个坐标所在的正方形。正方形的长度满足等差数列8,16,24…利用公式求出和,之后算出这个点距离刚才说的那个(-t,-t)点的距离,加上就是了。
3)分情况去求,假设左上右下边标号为1,2,3,4,分这四种情况去求即可。

#include <bits/stdc++.h>
#define ll long long
const int mod = 1e9+7;
const ll ds = 1e15+7;
using namespace std;


int main()
{
	ll x,y,t;
	ll sum = 0;
	cin >> x >> y;
	if(x == 0 && y == 0) cout << 0;
	else{
    	t = max(abs(x),abs(y));
    	if(t == 1){
    		if(x == 1) sum = 4+abs(y-1);
    		if(x == -1) sum = abs(y+1);
    		if(y == 1) sum = 2+abs(x+1);
    		if(y == -1) sum = 6+abs(1-x);
    		cout << sum;
    	}
    	else{
	    	sum = (8+(8+8*(t-2)))*(t-1)/2;
    		if(x == t) sum += 2*2*t+abs(y-t);
	    	if(x == -t) sum += abs(y+t);
	    	if(y == t) sum += 2*t+abs(x+t);
	    	if(y == -t) sum += 3*2*t+abs(t-x); 
	        cout << sum; 
    	}
    }
    return 0;	
} 

8:日志统计

题目链接:https://www.dotcpp.com/oj/problem2279.html
思路:以id为下标,用vector保存这个id获得赞的时刻,在查询之前先对时刻排序,然后利用尺取法查询是否在d时间内获得赞数大于等k。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <set>
#include <map>
#define ll long long
const int mod = 1e9+7;

using namespace std;

vector<int>a[100005]; 
int main()
{
    int n,d,k,ts,td,id = 0;
    scanf("%d%d%d",&n,&d,&k);
    for(int i = 1; i <= n; i++){
    	scanf("%d%d",&ts,&td);
    	a[td].push_back(ts);
    	id = max(td,id); 
    	
	}
	for(int i = 0; i <= id; i++){
		int l = a[i].size();
		if(l < k) continue;
		sort(a[i].begin(),a[i].end());
		int le = 0,ri = 0,flag = 0,t = 0;
		while(le <= ri && ri < l){
			t++;
			if(t >= k){
				if(a[i][ri] - a[i][le] < d){
					flag = 1;
					printf("%d\n",i);
					break;
				}
				else le++,t--;
			}
			ri++;
		}
	}
	return 0;
}

参考:https://blog.csdn.net/qq_34202873/article/details/79784728

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值