您的位置:首页 > 其它

蓝桥寒假训练1->2013年第四届蓝桥杯省赛

2017-02-03 13:58 225 查看


1.高斯日记

大数学家高斯有个好习惯:无论如何都要记日记。

他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210

后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?

高斯出生于:1777年4月30日。

在高斯发现的一个重要定理的日记上标注着:5343,因此可算出那天是:1791年12月15日。

高斯获得博士学位的那天日记上标着:8113

请你算出高斯获得博士学位的年月日。


提交答案的格式是:yyyy-mm-dd, 例如:1980-03-21

请严格按照格式,通过浏览器提交答案。

注意:只提交这个日期,不要写其它附加内容,比如:说明性的文字。

高斯获得博士学位的年月日是(4分)

//解题思路:
//法一:查看日历
//法二:代码
#include <cstdio>
#include <iostream>
using namespace std;
int judge(int year){
if(year%4==0&&year%100!=0||year%400==0)//判断是否为瑞年(2月29天)
return 366;
return 365;
}
int main(){
int loc,year,month,day=8113-5343-16;//从1792年开始算
int m[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
for(year=1792;day/judge(year);year++){
day-=judge(year);
}
loc=judge(year)-365;
for(month=1;day/m[loc][month];month++){
day-=m[loc][month];
}
printf("%04d-%02d-%02d\n",year,month,day);
return 0;
}
//1799-07-16



2.马虎的算式

小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。

有一次,老师出的题目是:36 x 495 = ?

他却给抄成了:396 x 45 = ?

但结果却很戏剧性,他的答案竟然是对的!!

因为 36 * 495 = 396 * 45 = 17820

类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54

假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)

能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?


请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。

满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。

答案直接通过浏览器提交。

注意:只提交一个表示最终统计种类数的数字,不要提交解答过程或其它多余的内容。

能满足形如:
ab * cde = adb * ce
这样的算式一共有(5分)种
//解题思路:
//法一:
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int count=0;
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
if(j==i)
continue;
for(int t=1;t<=9;t++){
if(t==i||t==j)
continue;
for(int x=1;x<=9;x++){
if(x==i||x==j||x==t)
continue;
for(int y=1;y<=9;y++){
if(y==i||y==j||y==t||y==x)
continue;
if((i*10+j)*(t*100+x*10+y)==(i*100+x*10+j)*(t*10+y))
count++;
}
}
}
}
}
cout<<count<<endl;
return 0;
}
//142
//法二:先生成1-9的全排列,再取前五位
//但要注意最后除以后四位全排列的个数,4!=24
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
int count=0;
int a[9]={1,2,3,4,5,6,7,8,9};
do{
int num1=a[0]*10+a[1];
int num2=a[2]*100+a[3]*10+a[4];
int num3=a[0]*100+a[3]*10+a[1];
int num4=a[2]*10+a[4];
if(num1*num2==num3*num4)
count++;
}while(next_permutation(a,a+9));
cout<<count/24<<endl;
return 0;
}


3. 第39级台阶
小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级!

站在台阶前,他突然又想着一个问题:

如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?

请你利用计算机的优势,帮助小明寻找答案。


要求提交的是一个整数。

注意:不要提交解答过程,或其它的辅助说明文字。

有(8分)种不同的上法
//解题思路:
//简单深搜
#include <cstdio>
#include <iostream>
using namespace std;
int count;
void dfs(int foot_step,int stairs){//当前步数,台阶数
if(stairs>39)
return;
if(stairs==39){
if(foot_step%2==0)
count++;
return;
}
for(int i=1;i<=2;i++){
dfs(foot_step+1,stairs+i);
}
}
int main(){
dfs(0,0);
cout<<count<<endl;
return 0;
}
//51167078


4.黄金连分数
黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现。有时需要把这个数字求得很精确。

对于某些精密工程,常数的精度很重要。也许你听说过哈勃太空望远镜,它首次升空后就发现了一处人工加工错误,对那样一个庞然大物,其实只是镜面加工时有比头发丝还细许多倍的一处错误而已,却使它成了“近视眼”!!

言归正传,我们如何求得黄金分割数的尽可能精确的值呢?有许多方法。

比较简单的一种是用连分数:

1
黄金数 =  --------------------------
1
1 + --------------------
1
1 + ---------------
1
1 + ---------
1 + ...

这个连分数计算的“层数”越多,它的值越接近黄金分割数。

请你利用这一特性,求出黄金分割数的足够精确值,要求四舍五入到小数点后100位。

小数点后3位的值为:0.618

小数点后4位的值为:0.6180

小数点后5位的值为:0.61803

小数点后7位的值为:0.6180340


(注意尾部的0,不能忽略)

你的任务是:写出精确到小数点后100位精度的黄金分割值。

注意:尾数的四舍五入! 尾数是0也要保留!

显然答案是一个小数,其小数点后有100位数字,请通过浏览器直接提交该数字。

注意:不要提交解答过程,或其它辅助说明类的内容。

黄金分割值:0.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911375
//解题思路:
//查找规律,发现为斐波那契数列,选尽量往后的两个相邻数,模拟除法。
//使用的斐波那契数越往后,黄金分割数精确度越高
//下面的c++代码在long long int范围内选尽量往后的两个相邻数,也就才到第八十多项,最后结果不是正确答案
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int top;
unsigned long long int fib[10000];
unsigned long long int x,y;
fib[0]=0,fib[1]=1;
for(top=2;fib[top-1]+fib[top-2]<=1e18;top++){
fib[top]=fib[top-1]+fib[top-2];
}
y=fib[--top];
x=fib[--top];
for(int i=1;i<=102;i++){//模拟除法      //打印到小数点后101位,方便四舍五入
printf("%lld",x/y);
x=(x%y)*10;
}
return 0;
}
//四舍五入后:0.6180339887498948482045868343656381167528434309151518930402843639810346973807203378342944662358280796
//得到正确答案的方法,使用java的大整数类,得到尽量往后的两个相邻斐波那契数
import java.math.*;
public class Main{
public static void main(String[] args) {
BigInteger a=new BigInteger("1");
BigInteger b=new BigInteger("1");
BigInteger c=new BigInteger("0");
for (int i=0;i<1000;i++){//得到第1000多项斐波那契数a b
c=b;
b=a.add(b);
a=c;
}
c=a.divide(b);
//System.out.println(c);  此时c=0
int cnt=c.toString().length();
while (cnt<=100){//此处获取小数点后指定位数的方式不错   //获取到小数点后101位,方便四舍五入
a=a.multiply(BigInteger.TEN);
c=a.divide(b);
cnt=c.toString().length();
}
System.out.println(c);
}
}
//61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748
//即结果:0.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911375



5.前缀判断

如下的代码判断 needle_start指向的串是否为haystack_start指向的串的前缀,如不是,则返回NULL。

比如:"abcd1234" 就包含了 "abc" 为前缀

char* prefix(char* haystack_start, char* needle_start)
{
char* haystack = haystack_start;
char* needle = needle_start;

while(*haystack && *needle){
if((5分)) return NULL;  //填空位置
}

if(*needle) return NULL;

return haystack_start;
}


6.三部排序

一般的排序有许多经典算法,如快速排序、希尔排序等。

但实际应用时,经常会或多或少有一些特殊的要求。我们没必要套用那些经典算法,可以根据实际情况建立更好的解法。

比如,对一个整型数组中的数字进行分类排序:

使得负数都靠左端,正数都靠右端,0在中部。注意问题的特点是:负数区域和正数区域内并不要求有序。可以利用这个特点通过1次线性扫描就结束战斗!!

以下的程序实现了该目标。

其中x指向待排序的整型数组,len是数组的长度。


#include <cstdio>
#include <iostream>
using namespace std;
void sort3p(int* x, int len)
{
int p = 0;
int left = 0;
int right = len-1;
while(p<=right){
if(x[p]<0){
int t = x[left];
x[left] = x[p];
x[p] = t;
left++;
p++;
}
else if(x[p]>0){
int t = x[right];
x[right] = x[p];
x[p] = t;
right--;
}
else{
p++;//填空位置
}
}

}
int main(){
int array[]={25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0};
sort3p(array,14);
for(int i=0;i<14;i++)
printf("%d ",array[i]);
return 0;
}
//p++



如果给定数组:

25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0


则排序后为:

-3,-2,-16,-5,0,0,0,21,19,33,25,16,18,25

7.错误票据(qdulq) (4分)

标题:错误票据

某涉密单位下发了某种票据,并要在年终全部收回。

每张票据有唯一的ID号。全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。

因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。

你的任务是通过编程,找出断号的ID和重号的ID。

假设断号不可能发生在最大和最小号。


要求程序首先输入一个整数N(N<100)表示后面数据行数。

接着读入N行数据。

每行数据长度不等,是用空格分开的若干个(不大于100个)正整数(不大于100000)

每个整数代表一个ID号。

要求程序输出1行,含两个整数m n,用空格分隔。

其中,m表示断号ID,n表示重号ID

例如:

用户输入:

2
5 6 8 11 9
10 12 9

则程序输出:

7 9


再例如:

用户输入:

6
164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196
172 189 127 107 112 192 103 131 133 169 158
128 102 110 148 139 157 140 195 197
185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190
149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188
113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

则程序输出:

105 120


//法一:
//这个题真坑,其实不用做输入处理,n就是个摆设
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main(){
int n,num,book[100005];
int min_num=100000,max_num=-1;
int nID,mID;
memset(book,0,sizeof(book));
scanf("%d",&n);
while(cin>>num){
book[num]++;
min_num=min(min_num,num);
max_num=max(max_num,num);
}
for(int i=min_num;i<=max_num;i++){
if(book[i]>1)
nID=i;
if(!book[i])
mID=i;
}
printf("%d %d\n",mID,nID);
return 0;
}


//法二:输入处理
#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
using namespace std;
int main(){
int n,num,now,len,book[100005];
int min_num=100000,max_num=-1;
int nID,mID;
char ch[1000];
memset(book,0,sizeof(book));
cin>>n;
getchar();
for(int i=1;i<=n;i++){
gets(ch);
len=strlen(ch);
now=0;
for(int j=0;j<=len;j++){//此处用j<=len而不是ch[j]为了不漏掉每行最后一个数
if(!isdigit(ch[j])||j==len){
ch[j]='\0';
num=atoi(ch+now);
now=j+1;
if(num){
book[num]++;
min_num=min(min_num,num);
max_num=max(max_num,num);
}
}
}
}
for(int i=min_num;i<=max_num;i++){
if(book[i]>1)
nID=i;
if(!book[i])
mID=i;
}
printf("%d %d\n",mID,nID);
return 0;
}

之前还写了一个错误代码:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
using namespace std;
int main(){
char ch[1000],temp;
int n,num,book[100005];
int min_num=100000,max_num=-1;
int nID,mID;
int top,now;
memset(book,0,sizeof(book));
scanf("%d",&n);
getchar();
for(int i=1;i<=n;i++){
top=-1;
now=0;
do{
scanf("%c",&ch[++top]);
temp=ch[top];
if(!isdigit(ch[top])){
ch[top]='\0';
num=atoi(ch+now);//当字符串为空,atoi返回0
now=top+1;
if(num){
book[num]++;
min_num=min(min_num,num);
max_num=max(max_num,num);
}
}
}while(temp!='\n');
}
for(int i=min_num;i<=max_num;i++){
if(book[i]>1)
nID=i;
if(!book[i])
mID=i;
}
printf("%d %d\n",mID,nID);
return 0;
}
此代码用回车判断每行结束,对于最后一行,这是不行的,因为没说最后以回车结束数据输入,在测试文件中最后一行后会紧跟一个文件结束符,而不是回车

8.翻硬币(qdulq) (10分)

小明正在玩一个“翻硬币”的游戏。

桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零)。

比如,可能情形是:
**oo***oooo


如果同时翻转左边的两个硬币,则变为:
oooo***oooo


现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?

我们约定:把翻动相邻的两个硬币叫做一步操作,那么要求:

程序输入:

两行等长的字符串,分别表示初始状态和要达到的目标状态。每行的长度<1000

程序输出:

一个整数,表示最小操作步数

例如:

用户输入:

**********
o****o****


程序应该输出:
5


再例如:

用户输入:

*o**o***o***
*o***o**o***

程序应该输出:

1


//法一:直接对比着目标状态,线形处理初始状态即可
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int count=0;
string start,goal;
cin>>start>>goal;
for(int i=0;i<start.size()-1;i++){
if(start[i]!=goal[i]){
count++;
start[i+1]=(start[i+1]=='*'?'o':'*');
}
}
cout<<count<<endl;
return 0;
}
//法二:写深搜
//不过对于大数据会超时
#include <cstdio>
#include <map>
#include <iostream>
using namespace std;
string start,goal;
int len,min_step=1000;
map<string,int> book;//用map标记字符串
void dfs(string a,int step){
if(a==goal){
if(step<min_step)
min_step=step;
return;
}
if(step>=min_step||step>=len-1)
return;
for(int i=0;i<len-1;i++){
string temp=a;
for(int j=i;j<=i+1;j++){
temp[j]=(temp[j]=='*'?'o':'*');
}
if(book.find(temp)==book.end()){
book[temp]=1;
dfs(temp,step+1);
book.erase(temp);
}
}
}
int main(){
cin>>start>>goal;
len=start.size();
book.clear();
book[start]=1;
dfs(start,0);
cout<<min_step<<endl;
return 0;
}
9.带分数(qdulq) (16分)

100 可以表示为带分数的形式:100 = 3 + 69258 / 714
还可以表示为:100 = 82 + 3546 / 197

注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。

类似这样的带分数,100 有 11 种表示法。

题目要求:

从标准输入读入一个正整数N (N<1000*1000) 程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。

注意:不要求输出每个表示,只统计有多少表示法!

例如:

用户输入:

100

程序输出:

11


再例如:

用户输入:

105

程序输出:

6


水题

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
int num,count=0;
int a,b,c;
int array[9]={1,2,3,4,5,6,7,8,9};
cin>>num;
do{//生成全排列
for(int i=0;i<=6;i++){//分割
for(int j=i+1;j<=7;j++){
a=b=c=0;
for(int t=0;t<=i;t++)
a=a*10+array[t];
for(int t=i+1;t<=j;t++)
b=b*10+array[t];
for(int t=j+1;t<9;t++)
c=c*10+array[t];
if(b%c==0&&a+b/c==num)
count++;
}
}
}while(next_permutation(array,array+9));
cout<<count<<endl;
return 0;
}


10.连号区间数

小明这些天一直在思考这样一个奇怪而有趣的问题:

在1~N的某个全排列中有多少个连号区间呢?这里所说的连号区间的定义是:

如果区间[L, R] 里的所有元素(即此排列的第L个到第R个元素)递增排序后能得到一个长度为R-L+1的“连续”数列,则称这个区间连号区间。

当N很小的时候,小明可以很快地算出答案,但是当N变大的时候,问题就不是那么简单了,现在小明需要你的帮助。


输入格式

第一行是一个正整数N (1 <= N <= 50000), 表示全排列的规模。

第二行是N个不同的数字Pi(1 <= Pi <= N), 表示这N个数字的某一全排列。


输出格式

输出一个整数,表示不同连号区间的数目。


样例输入1

4
3 2 4 1


样例输出1

7


样例输入2

53 4 2 5 1


样例输出2

9


//此题看上去难,其实仔细一想,因为每个数都不同,对于一段区间,若是最大值减去最小值等于下标相减,排序后就是连号的
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int n,min_num,max_num,count=0;
int array[50000];
cin>>n;
for(int i=0;i<n;i++){
scanf("%d",&array[i]);
}
for(int i=0;i<n;i++){//对于第一层for循环,i取每个值,都进行一次线性处理
min_num=50000,max_num=0;
for(int j=i;j<n;j++){
min_num=min(min_num,array[j]);
max_num=max(max_num,array[j]);
if(max_num-min_num==j-i)
count++;
}
}
cout<<count<<endl;
return 0;
}


总结:
此套题还算简单,有几个点值得注意:

1.理解题意,判断最佳方法最重要,如8.翻硬币,要是写搜索,就是小题大做了,还会运行超时

2.发现规律,别心急,如4.斐波那契的应用,发现斐波那契的过程要领会

3.注意一些坑,切勿浪费时间,如7.错误票据,本来直接读取数据即可,若是浮于表面,去做数据处理,太麻烦了

4.别想多,有的简单题就是简单题,别非要用个复杂算法,如8.翻硬币,直接处理即可,写啥搜索

5.思维模式,10.连号区间,若是不多想,就做起来麻烦了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: