2019届蓝桥杯大学B组题解

试题 B: 年号字串 本题总分:5 分
【问题描述】 小明用字母 A 对应数字 1,B 对应 2,以此类推,用 Z 对应 26。对于 27 以上的数字,小明用两位或更长位的字符串来对应,例如 AA 对应 27,AB 对 应 28,AZ 对应 52,LQ 对应 329。 请问 2019 对应的字符串是什么?
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一 个大写英文字符串,在提交答案时只填写这个字符串,注意全部大写,填写多 余的内容将无法得分。
试题 B: 年号字串

题意:考察26进制,用短除法自己做做吧

答案:BYQ

试题 C: 数列求值 本题总分:10 分
【问题描述】 给定数列 1, 1, 1, 3, 5, 9, 17, …,从第 4 项开始,每项都是前 3 项的和。求 第 20190324 项的最后 4 位数字。
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一 个 4 位整数(提示:答案的千位不为 0),在提交答案时只填写这个整数,填写 多余的内容将无法得分。

题解:用数组来处理,for循环打表,千万别用递归!

#include <iostream>
#include <bitset>
#include <algorithm>
#include <vector>
#include <map>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <queue> 
using namespace std;
int a[21190323];
void form()
{
	a[0]=a[1]=a[2]=1;
	for(int i=3;i<21190323;i++)
	{
		a[i]=a[i-1]+a[i-2]+a[i-3];
		if(a[i]>10000)
		{
			a[i]=a[i]%10000;
		}
	}
}
int main()
{
	form();
	cout<<a[20190323]<<endl;
	return 0;
}

试题 D: 数的分解 本题总分:10 分
【问题描述】 把 2019 分解成 3 个各不相同的正整数之和,并且要求每个正整数都不包 含数字 2 和 4,一共有多少种不同的分解方法? 注意交换 3 个整数的顺序被视为同一种方法,例如 1000+1001+18 和 1001+1000+18 被视为同一种。
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一 个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

思路:for循环暴力吧,反正是填空。

#include <iostream>
#include <bitset>
#include <algorithm>
#include <vector>
#include <map>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <queue> 
using namespace std;
int paichu(int n)
{
	int a=n;
	while(a>0)
	{
		if(a%10==2||a%10==4)
		{
			return 0;
		}
		a/=10;
	}
	return 1;	
}
int main()
{
	int count=0;
	for(int i=1;i<2020;i++)
		for(int j=i+1;j<2020;j++)
			for(int k=j+1;k<2020;k++)
			{
				if(i+j+k==2019)
				{
					if(paichu(i)&&paichu(j)&&paichu(k))
					{
						count++;
					}
				}
			}
	cout<<count<<endl;
	return 0;
}

试题 E: 迷宫 本题总分:15 分
【问题描述】 下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方。
010000 000100 001001 110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。 对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式, 其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。 请注意在字典序中D<L<R<U。(如果你把以下文字复制到文本文件中,请务 必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 maze.txt, 内容与下面的文本相同)
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101 11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001 00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010 00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000 10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一 个字符串,包含四种字母 D、U、L、R,在提交答案时只填写这个字符串,填 写多余的内容将无法得分。

试题 H: 等差数列 时间限制: 1.0s 内存限制: 256.0MB 本题总分:20 分
【问题描述】
数学老师给小明出了一道等差数列求和的题目。但是粗心的小明忘记了一 部分的数列,只记得其中 N 个整数。 现在给出这 N 个整数,小明想知道包含这 N 个整数的最短的等差数列有 几项?
【输入格式】
输入的第一行包含一个整数 N。 第二行包含 N 个整数 A1,A2,··· ,AN。(注意 A1 ∼ AN 并不一定是按等差数 列中的顺序给出)
【输出格式】
输出一个整数表示答案。
【样例输入】 5 2 6 4 10 20
【样例输出】 10
【样例说明】 包含 2、6、4、10、20 的最短的等差数列是 2、4、6、8、10、12、14、16、 18、20。
【评测用例规模与约定】 对于所有评测用例,2≤ N ≤100000,0≤ Ai ≤109。

题解:当时没考率那么多,直接做差,选出最小差,然后带入公式,然后就凉了,正解应该是求差的最大公约数

/* 辗转相除法(下面还有其他算法) */
#include <iostream>
#include <bitset>
#include <algorithm>
#include <vector>
#include <map>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <queue>
#include <string.h> 
using namespace std;
int u(int a,int b)
{
	int temp;
	if(a<b)
	{
		int t;
		t=a;
		a=b;
		b=t;
	}
	temp=a%b;
	while(temp==0)
	return b;
	while(temp!=0)
	{
		a=b;
		b=temp;
		temp=a%b; 
	}
	return b;
} 
int main()
{
	int sum=0,n,temp1,temp2,k;
	int a[100001];
	int b[100001];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	for(int i=0;i<n-1;i++)
	{
		sum=a[i+1]-a[i];
		b[i]=sum;
	}
	temp1=b[0];
	for(int i=0;i<=n-2;i++)
	{
		temp2=temp1;
		k=b[i];
		temp1=u(temp2,k);
	}
	cout<<((a[n-1]-a[0])/temp1+1)<<endl;
	return 0;
}
/*穷举法求最大公约数*/
int u(int a,int b)
{
	temp=(a>b)?b:a;
	while(temp>0)
	{
		if(a%temp==0&&b%temp==0)
		{
			break;
			temp--;
		}
	}
	return 0;
}
/*更相减损法求最大公约数*/
int u(int a,int b)
{
	int i=0;
	int temp,x;
	while(a%2==0&&b%2==0)
	{
		a/=2;
		b/=2;
		i+=1;
	}
	if(a<b)
	{
		temp=a;
		a=b;
		b=temp;
	}
	while(x)
	{
		x=a-b;
		a=(b>x)?b:x;
		b=(b<x)?b:x;
		if(b==(a-b))
		break;
	}
	if(i==0)
	return b;
	else
	return (int)pow(2,i)*b;
}
/* stein算法求最大公公约数 */
int u(int a,int b)
{
	int factor=0;
	int temp;
	if(a<b);
	{
		temp=a;
		a=b;
		b=temp;
	}
	if(b==0)
	{
		return 0;
	}
	while(a!=b)
	{
		if(a&0x1) //当a是偶数; 
		{
			if(b&0x1) //当a,b都是偶数;
			{
				b=(a-b)>>1; //右移一位相当于除2; 
				a-=b;
			}
			else //当a是偶数,y是奇数;
			{
				b>>=1;
			} 
		}
		else //a,b都是奇数; 
		{
			a>>=1;
			b>>=1;
			++factor; 
		}
	}
	return(a<<factor); //左移一位相当于乘2 
} 

/*              总结              */
/*        辗转相除法时间最短        */

猜你喜欢

转载自blog.csdn.net/weixin_43888067/article/details/88835575