蓝桥杯2016年省赛C/C++大学B组

  1. 煤球数目

有一堆煤球,堆成三角棱锥形。具体:
第一层放1个,
第二层3个(排列成三角形),
第三层6个(排列成三角形),
第四层10个(排列成三角形),

如果一共有100层,共有多少个煤球?

思路
注意到每一层的数目其实是一个1为首项,1为公差的等差数列,求一共多少个煤球,就直接相加每层的数量就可以了

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
	int ans = 0;
	int sum = 0;
	for(int i = 1; i <= 100; i++){
		ans += i;
		sum += ans;
	}
	cout << sum << endl;
	return 0;
}
//171700
  1. 生日蜡烛

某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。

现在算起来,他一共吹熄了236根蜡烛。

请问,他从多少岁开始过生日party的?

请填写他开始过生日party的年龄数。

思路
简单搜索

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
	for(int i = 1; i <= 100; i++){
		int sum = 0;
		for(int j = i; sum < 236; j++){
			sum += j;
			if(sum == 236){
				cout << i << endl;
			}
		}
	}
	return 0;
}
//  26
  1. 凑算式
    【图1.jpg】

这个算式中A- I 代表1~9的数字,不同的字母代表不同的数字。

比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。

这个算式一共有多少种解法?

思路
求全排列, 注意精度问题

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int aa[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int ans = 0;
int main(){
	do{
		int a = aa[1], b = aa[2], c = aa[3], d = aa[4];
		int e = aa[5], f = aa[6], g = aa[7], h = aa[8];
		int i = aa[9];
		int x = d * 100 + e * 10 + f;
		int y = g * 100 + h * 10 + i;
		x = a * c * y + b * y + c * x;
		y = c * y;
		if(x == y * 10) ans++;
	}while(next_permutation(aa + 1, aa + 10));
	cout << ans << endl;
	return 0;
}
//  362880

  1. 快速排序

排序在各种场合经常被用到。
快速排序是十分常用的高效率的算法。

其思想是:先选一个“标尺”,
用它把整个队列过一遍筛子,
以保证:其左边的元素都不大于它,其右边的元素都不小于它。

这样,排序问题就被分割为两个子区间。
再分别对子区间排序就可以了。

下面的代码是一种实现,请分析并填写划线部分缺少的代码。

#include <stdio.h>

void swap(int a[], int i, int j)
{
	int t = a[i];
	a[i] = a[j];
	a[j] = t;
}

int partition(int a[], int p, int r)
{
    int i = p;
    int j = r + 1;
    int x = a[p];
    while(1){
        while(i<r && a[++i]<x);
        while(a[--j]>x);
        if(i>=j) break;
        swap(a,i,j);
    }
	______________________;
	//swap(a, p, j);
    return j;
}

void quicksort(int a[], int p, int r)
{
    if(p<r){
        int q = partition(a,p,r);
        quicksort(a,p,q-1);
        quicksort(a,q+1,r);
    }
}
    
int main()
{
	int i;
	int a[] = {5,13,6,24,2,8,19,27,6,12,1,17};
	int N = 12;
	
	quicksort(a, 0, N-1);
	
	for(i=0; i<N; i++) printf("%d ", a[i]);
	printf("\n");
	
	return 0;
}

注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。

  1. 抽签

X星球要派出一个5人组成的观察团前往W星。
其中:
A国最多可以派出4人。
B国最多可以派出2人。
C国最多可以派出2人。

那么最终派往W星的观察团会有多少种国别的不同组合呢?

下面的程序解决了这个问题。
数组a[] 中既是每个国家可以派出的最多的名额。
程序执行结果为:
DEFFF
CEFFF
CDFFF
CDEFF
CCFFF
CCEFF
CCDFF
CCDEF
BEFFF
BDFFF
BDEFF
BCFFF
BCEFF
BCDFF
BCDEF

(以下省略,总共101行)

#include <stdio.h>
#define N 6
#define M 5
#define BUF 1024

void f(int a[], int k, int m, char b[])
{
	int i,j;
	
	if(k==N){ 
		b[M] = 0;
		if(m==0) printf("%s\n",b);
		return;
	}
	
	for(i=0; i<=a[k]; i++){
		for(j=0; j<i; j++) b[M-m+j] = k+'A';
		______________________;  //填空位置
		//	f(a, k+1, m - i, b);
	}
}
int main()
{	
	int  a[N] = {4,2,2,1,1,3};
	char b[BUF];
	f(a,0,M,b);
	return 0;
}

仔细阅读代码,填写划线部分缺少的内容。

注意:不要填写任何已有内容或说明性文字。

思路
搜索每一种可能的值,m代表还可以选几个, k代表选第几个国家

  1. 方格填数

如下的10个格子

   +--+--+--+
   |  |  |  |
+--+--+--+--+
|  |  |  |  |
+--+--+--+--+
|  |  |  |
+--+--+--+

(如果显示有问题,也可以参看【图1.jpg】

填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)

一共有多少种可能的填数方案?

请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

思路
也是求全排列, 为方格打下下标, 用数组存数字,选出符合条件的结果
在这里插入图片描述

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int map[5][6] = {100};
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
bool check(){
	for(int i = 1; i <= 3; i++)
		for(int j = 1; j <= 4; j++)
			for(int a = -1; a <= 1; a++)
				for(int b = -1; b <= 1; b++){
					if(abs(map[i][j] - map[a + i][b + j]) == 1) return false;
				}
	return true;
}
int main()
{	memset(map, 0x3f, sizeof map);
	int ans = 0;
	do{
		int cnt = 0;
		for(int i = 1; i <= 3; i++)
			for(int j = 1; j <= 4; j++){
				if(i == 1 && j == 1) continue;
				if(i == 3 && j == 4) break;;
				map[i][j] = a[cnt++];
			}
		if(check()) ans++;
	}while(next_permutation(a, a + 10));
	cout << ans << endl;
	return 0;
}
//1580
  1. 剪邮票

如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。

请你计算,一共有多少种不同的剪取方法。

请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

思路
类似于二进制枚举, 这里用01的全排列模拟选择的全部情况,然后判断连通图,如果当前选择的是连通图,就结果加一

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[12] = {0, 0, 0 ,0 , 0, 0, 0, 1, 1, 1, 1, 1};
int map[10][10];
int ans = 0;
void dfs(int x, int y){
	if(x < 1 || x > 3 || y < 1 || y > 4 || map[x][y] != 1) return;
	map[x][y] = 0;
	dfs(x + 1, y);
	dfs(x - 1, y);
	dfs(x, y + 1);
	dfs(x, y - 1);
}
bool check(){
	int x, y;
	for(int i = 1; i <= 3; i++)
		for(int j = 1; j <= 4; j++){
			if(map[i][j] == 1 ){
				x = i, y = j;
			}
		}
	dfs(x, y);
	bool flag = 1;
	for(int i = 1; i <= 3; i++)
		for(int j = 1; j <= 4; j++){
			if(map[i][j] == 1 ){
				flag = 0;
			}
		}
	if(flag) return true;
	else return false;
}
int main()
{	
	do{
		for(int i = 0; i < 12; i++){
			if(a[i] == 1){
				map[i / 4 + 1][i - (i / 4 * 4) + 1] = 1;
			}
		}
		if(check()) ans++;
		memset(map, 0, sizeof map);
	}while(next_permutation(a, a + 12)); 
	cout << ans << endl;
	return 0;
}
// 116
  1. 四平方和

四平方和定理,又称为拉格朗日定理:
每个正整数都可以表示为至多4个正整数的平方和。
如果把0包括进去,就正好可以表示为4个数的平方和。

比如:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
(^符号表示乘方的意思)

对于一个给定的正整数,可能存在多种平方和的表示法。
要求你对4个数排序:
0 <= a <= b <= c <= d
并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法

程序输入为一个正整数N (N<5000000)
要求输出4个非负整数,按从小到大排序,中间用空格分开

例如,输入:
5
则程序应该输出:
0 0 1 2

再例如,输入:
12
则程序应该输出:
0 2 2 2

再例如,输入:
773535
则程序应该输出:
1 1 267 838

资源约定:
峰值内存消耗 < 256M
CPU消耗  < 3000ms

思路
利用HASH表优化循环

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
map<int, int> ca;
int n;
int main(){
	scanf("%d", &n);
	for(int c = 0; c * c <= n / 2; c++){
		for(int d = c; c * c + d * d <= n; d++){
			if(ca.find(c * c + d * d) == ca.end())
				ca[c * c + d * d] = c;
		}
	}
	for(int a = 0; a * a <= n / 4; a++){
		for(int b = a; b * b + a * a <= n / 2; b++){
			int x = a * a + b * b;
			x = n - x;
			if(ca.find(x) != ca.end()){ 
				int c = ca[x];
				int d = n - a * a - b * b - c * c;
				d = (int)sqrt(d);
				printf("%d %d %d %d\n", a, b, c, d);
				goto flag;
			}
		}
	}
	flag : return 0;
}
  1. 交换瓶子

有N个瓶子,编号 1 ~ N,放在架子上。

比如有5个瓶子:
2 1 3 5 4

要求每次拿起2个瓶子,交换它们的位置。
经过若干次后,使得瓶子的序号为:
1 2 3 4 5

对于这么简单的情况,显然,至少需要交换2次就可以复位。

如果瓶子更多呢?你可以通过编程来解决。

输入格式为两行:
第一行: 一个正整数N(N<10000), 表示瓶子的数目
第二行:N个正整数,用空格分开,表示瓶子目前的排列情况。

输出数据为一行一个正整数,表示至少交换多少次,才能完成排序。

例如,输入:
5
3 1 2 5 4

程序应该输出:
3

再例如,输入:
5
5 4 3 2 1

程序应该输出:
2

资源约定:
峰值内存消耗 < 256M
CPU消耗  < 1000ms

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int n, ans;
int a[10010];
int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);
	for(int i = 1; i <= n; i++){
		if(a[i] != i){
			swap(a[i], a[a[i]]);
			ans++;
		}
	}
	cout << ans << endl;
}
  1. 最大比例

X星球的某个大奖赛设了M级奖励。每个级别的奖金是一个正整数。
并且,相邻的两个级别间的比例是个固定值。
也就是说:所有级别的奖金数构成了一个等比数列。比如:
16,24,36,54
其等比值为:3/2

现在,我们随机调查了一些获奖者的奖金数。
请你据此推算可能的最大的等比值。

输入格式:
第一行为数字 N (0<N<100),表示接下的一行包含N个正整数
第二行N个正整数Xi(Xi<1 000 000 000 000),用空格分开。每个整数表示调查到的某人的奖金数额

要求输出:
一个形如A/B的分数,要求A、B互质。表示可能的最大比例系数

测试数据保证了输入格式正确,并且最大比例是存在的。


例如,输入:
3
1250 200 32

程序应该输出:
25/4

再例如,输入:
4
3125 32 32 200

程序应该输出:
5/2

再例如,输入:
3
549755813888 524288 2

程序应该输出:
4/1

资源约定:
峰值内存消耗 < 256M
CPU消耗  < 3000ms

思路
枚举法, 找出合法的结果。
代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
struct node{
	ll x, y;
	node(ll a, ll b): x(a), y(b){
		ll ans = gcd(x, y);
		x /= ans;
		y /= ans;
	}
	ll gcd(ll a, ll b){
		if(b == 0) return a;
		return gcd(b, a % b);
	}
};
vector<node> ra;
int n;
ll a[110];
map<ll, map<ll, ll> > Pow;
map<ll, map<ll, ll> > Log;
void init(){
    for (int i = 2; i < 1e6; i++) {
        ll ans = (ll)i * i;
        int pow = 2;
        while(ans < 1e12){
            Pow[ans][pow] = i;
            Log[ans][i] = pow;
            pow++;
            ans *= i;
        }
    }
}
ll extract(ll x,ll pow){
    if(pow == 1) return x;
    if(x == 1) return 1;
    if(Pow[x].find(pow) != Pow[x].end())
        return Pow[x][pow];
    return -1;
}
ll getpow(ll ans, int i){
	if(ans == i) return 1;
	if(Log[ans].find(i) != Log[ans].end()) 
		return Log[ans][i];
	return -1;
}
int main(){
	init();
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%lld", a + i);
	sort(a + 1, a + 1 + n);
	int cnt = 0;
	for(int i = 1; i < n; i++){
		if(a[i + 1] != a[i]){
			ra.push_back(node(a[i + 1], a[i]));
			node ans = ra[cnt++];
		}		
	}
	for(int i = 1; i <= 40; i++){
		node ans = ra[0];
		ll x = ans.x;
		ll y = ans.y;	
		x = extract(x, i);
		y = extract(y, i);
		if(x == -1 || y == -1) continue;	
		bool flag = true;
		for(int j = 1; j < ra.size(); j++){
			ll a = ra[j].x;
			ll b = ra[j].y;			
			ll a1 = getpow(a, x);
			ll a2 = getpow(b, y);
			if(a2 == 1 && y == 1) a2 = a1;
			if(a1 != a2 || a1 == -1 || a2 == -1){
				flag = false;
			}
		}
		if(flag){
			node ans = node(x, y);
			printf("%lld/%lld", ans.x, ans.y);
			return 0;
		}
	}
	return 0;
}
发布了103 篇原创文章 · 获赞 203 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_45432665/article/details/104262976
今日推荐