1. 程式人生 > >2017第八屆藍橋杯 A組 C/C++ 真題及題解

2017第八屆藍橋杯 A組 C/C++ 真題及題解

1. (5')標題: 購物單
    小明剛剛找到工作,老闆人很好,只是老闆夫人很愛購物。老闆忙的時候經常讓小明幫忙到商場代為購物。小明很厭煩,但又不好推辭。
    這不,XX大促銷又來了!老闆夫人開出了長長的購物單,都是有打折優惠的。
    小明也有個怪癖,不到萬不得已,從不刷卡,直接現金搞定。
    現在小明很心煩,請你幫他計算一下,需要從取款機上取多少現金,才能搞定這次購物。

    取款機只能提供100元面額的紙幣。小明想盡可能少取些現金,夠用就行了。
    你的任務是計算出,小明最少需要取多少現金。

以下是讓人頭疼的購物單,為了保護隱私,物品名稱被隱藏了。
-----------------
****     180.90       88折

****      10.25       65折
****      56.14        9折
****     104.65        9折
****     100.30       88折
****     297.15        半價
****      26.75       65折
****     130.62        半價
****     240.28       58折
****     270.62        8折
****     115.87       88折
****     247.34       95折
****      73.21        9折
****     101.00        半價

****      79.54        半價
****     278.44        7折
****     199.26        半價
****      12.97        9折
****     166.30       78折
****     125.50       58折
****      84.98        9折
****     113.35       68折
****     166.57        半價
****      42.56        9折
****      81.90       95折
****     131.78        8折
****     255.89       78折

****     109.17        9折
****     146.69       68折
****     139.33       65折
****     141.16       78折
****     154.74        8折
****      59.42        8折
****      85.44       68折
****     293.70       88折
****     261.79       65折
****      11.30       88折
****     268.27       58折
****     128.29       88折
****     251.03        8折
****     208.39       75折
****     128.88       75折
****      62.06        9折
****     225.87       75折
****      12.89       75折
****      34.28       75折
****      62.16       58折
****     129.12        半價
****     218.37        半價
****     289.69        8折
--------------------

需要說明的是,88折指的是按標價的88%計算,而8折是按80%計算,餘者類推。
特別地,半價是按50%計算。

請提交小明要從取款機上提取的金額,單位是元。
答案是一個整數,類似4300的樣子,結尾必然是00,不要填寫任何多餘的內容。

特別提醒:不許攜帶計算器入場,也不能開啟手機。

分析:記事本將“折”處理掉,再用freopen輸入資料即可

程式碼:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
int main(){
	freopen("a.txt","r",stdin);
	char c[10];
	double price,discount,total = 0;
	while(scanf("%s%lf%lf",&c,&price,&discount)!=EOF){
		total+=(price*discount)/100;
	}
	cout<<total<<endl;
}
//輸出:5136.86

【答案】5200

2. (7')標題:等差素數列

2,3,5,7,11,13,....是素數序列。
類似:7,37,67,97,127,157 這樣完全由素陣列成的等差數列,叫等差素數數列。
上邊的數列公差為30,長度為6。

2004年,格林與華人陶哲軒合作證明了:存在任意長度的素數等差數列。
這是數論領域一項驚人的成果!

有這一理論為基礎,請你藉助手中的計算機,滿懷信心地搜尋:

長度為10的等差素數列,其公差最小值是多少?

注意:需要提交的是一個整數,不要填寫任何多餘的內容和說明文字。

程式碼:
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
const int N =100000;
int tot = 0;
int d[N],prime[N];
void init(){
	d[0] = 1;
	d[1] = 1;
	d[2] = 0;
	for(long long i = 2;i<N;i++){
		if(!d[i]){
			for(int j = i;j*i<N;j++){
				d[i*j] = 1;
			}
		}
	}
	for(int i =0;i<N;i++){
		if(d[i] == 0) prime[tot++] = i;
	}
}
int main(){
	init();
	for(int gap = 1;gap<N;gap++){
		for(int j = 0;j<tot;j++){
			int temp = prime[j];
			int flag = 1;
			for(int k = 1;k<10;k++){
				if(d[temp+gap]){
					flag = 0;
					break;
				}
				else temp+=gap;
			}
			if(flag){
				cout<<gap<<" "<<prime[j]<<endl;
				return 0;
			}
		}
	}
}
//輸出:210 199

【答案】210

3. (13')標題:承壓計算
X星球的高科技實驗室中整齊地堆放著某批珍貴金屬原料。

每塊金屬原料的外形、尺寸完全一致,但重量不同。
金屬材料被嚴格地堆放成金字塔形。

                             7 
                            5 8 
                           7 8 8 
                          9 2 7 2 
                         8 1 4 9 1 
                        8 1 8 8 4 1 
                       7 9 6 1 4 5 4 
                      5 6 5 5 6 9 5 6 
                     5 5 4 7 9 3 5 5 1 
                    7 5 7 9 7 4 7 3 3 1 
                   4 6 4 5 5 8 8 3 2 4 3 
                  1 1 3 3 1 6 6 5 5 4 4 2 
                 9 9 9 2 1 9 1 9 2 9 5 7 9 
                4 3 3 7 7 9 3 6 1 3 8 8 3 7 
               3 6 8 1 5 3 9 5 8 3 8 1 8 3 3 
              8 3 2 3 3 5 5 8 5 4 2 8 6 7 6 9 
             8 1 8 1 8 4 6 2 2 1 7 9 4 2 3 3 4 
            2 8 4 2 2 9 9 2 8 3 4 9 6 3 9 4 6 9 
           7 9 7 4 9 7 6 6 2 8 9 4 1 8 1 7 2 1 6 
          9 2 8 6 4 2 7 9 5 4 1 2 5 1 7 3 9 8 3 3 
         5 2 1 6 7 9 3 2 8 9 5 5 6 6 6 2 1 8 7 9 9 
        6 7 1 8 8 7 5 3 6 5 4 7 3 4 6 7 8 1 3 2 7 4 
       2 2 6 3 5 3 4 9 2 4 5 7 6 6 3 2 7 2 4 8 5 5 4 
      7 4 4 5 8 3 3 8 1 8 6 3 2 1 6 2 6 4 6 3 8 2 9 6 
     1 2 4 1 3 3 5 3 4 9 6 3 8 6 5 9 1 5 3 2 6 8 8 5 3 
    2 2 7 9 3 3 2 8 6 9 8 4 4 9 5 8 2 6 3 4 8 4 9 3 8 8 
   7 7 7 9 7 5 2 7 9 2 5 1 9 2 6 5 3 9 3 5 7 3 5 4 2 8 9 
  7 7 6 6 8 7 5 5 8 2 4 7 7 4 7 2 6 9 2 1 8 2 9 8 5 7 3 6 
 5 9 4 5 5 7 5 5 6 3 5 3 9 5 8 9 5 4 1 2 6 1 4 3 5 3 2 4 1 
X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X 

其中的數字代表金屬塊的重量(計量單位較大)。
最下一層的X代表30臺極高精度的電子秤。

假設每塊原料的重量都十分精確地平均落在下方的兩個金屬塊上,
最後,所有的金屬塊的重量都嚴格精確地平分落在最底層的電子秤上。
電子秤的計量單位很小,所以顯示的數字很大。

工作人員發現,其中讀數最小的電子秤的示數為:2086458231
請你推算出:讀數最大的電子秤的示數為多少?

注意:需要提交的是一個整數,不要填寫任何多餘的內容。

程式碼:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
double d[32][32]={0};
int main()  
{  
    for(int i=0;i<29;i++)  
        for(int j=0;j<=i;j++)  
            cin>>d[i][j];
    for(int i=0;i<30;i++){  
        for(int j=0;j<=i;j++){  
            d[i][j]+=(d[i-1][j]/2);  
            if(j-1>=0)  
                d[i][j]+=(d[i-1][j-1]/2); 
        }  
    }  
    double MIN=100000;
	double MAX=-1;  
    for(int i=0;i<30;i++) {  
        if(d[29][i]<MIN)  
            MIN=d[29][i];  
        if(d[29][i]>MAX)  
            MAX=d[29][i];  
    }  
    //讀數最大的電子稱的示數   
    printf("%lf\n",(2086458231*MAX/MIN));  
    return 0;  
} 
//輸出:72665192664.000000 
【答案】72665192664

4. 標題:方格分割
6x6的方格,沿著格子的邊線剪開成兩部分。
要求這兩部分的形狀完全相同。

如圖:p1.png, p2.png, p3.png 就是可行的分割法。

試計算:
包括這3種分法在內,一共有多少種不同的分割方法。
注意:旋轉對稱的屬於同一種分割法。

請提交該整數,不要填寫任何多餘的內容或說明文字。

分析:從中心點開始,路線對稱地進行DFS到邊緣

程式碼:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
double d[7][7] = {0};
bool vis[7][7];
int ans = 0;
int dir[4][2] = {0,1,0,-1,-1,0,1,0};
void dfs(int x,int y){
	if(x == 0 || y == 0 ||x == 6 || y == 6){
		ans++;
		return;
	}
	for(int i = 0;i<4;i++){
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];
		if(!vis[tx][ty]){
			vis[tx][ty] = 1;
			vis[6-tx][6-ty] = 1;
			dfs(tx,ty);
			vis[tx][ty] = 0;
			vis[6-tx][6-ty] = 0;
		}
	}
	return;
}
int main(){
	memset(vis,0,sizeof(vis));
	vis[3][3] = 1;
	dfs(3,3);
	cout<<ans/4<<endl;
	return 0;
}
//輸出:509 

5. (9')標題:取數位
求1個整數的第k位數字有很多種方法。

以下的方法就是一種。


// 求x用10進製表示時的數位長度   
int len(int x){  
    if(x<10) return 1;  
    return len(x/10)+1;  
}  
      
// 取x的第k位數字  
int f(int x, int k){  
    if(len(x)-k==0) return x%10;  
    return _____________________;  //填空  
}  
      
int main()  
{  
    int x = 23574;  
    printf("%d\n", f(x,3));  
    return 0;  
}  
對於題目中的測試資料,應該列印5。

請仔細分析原始碼,並補充劃線部分所缺少的程式碼。

注意:只提交缺失的程式碼,不要填寫任何已有內容或說明性的文字。

【答案】f(x/10,k)

6. (11')標題:最大公共子串

最大公共子串長度問題就是:
求兩個串的所有子串中能夠匹配上的最大長度是多少。

比如:"abcdkkk" 和 "baabcdadabc",
可以找到的最長的公共子串是"abcd",所以最大公共子串長度為4。

下面的程式是採用矩陣法進行求解的,這對串的規模不大的情況還是比較有效的解法。

請分析該解法的思路,並補全劃線部分缺失的程式碼。


#include <stdio.h>  
#include <string.h>  
  
#define N 256  
int f(const char* s1, const char* s2)  
{  
    int a[N][N];  
    int len1 = strlen(s1);  
    int len2 = strlen(s2);  
    int i,j;  
      
    memset(a,0,sizeof(int)*N*N);  
    int max = 0;  
    for(i=1; i<=len1; i++){  
        for(j=1; j<=len2; j++){  
            if(s1[i-1]==s2[j-1]) {  
                a[i][j] = __________________________;  //填空  
                if(a[i][j] > max) max = a[i][j];  
            }  
        }  
    }  
      
    return max;  
}  
  
int main()  
{  
    printf("%d\n", f("abcdkkk", "baabcdadabc"));  
    return 0;  
}  

【答案】a[i-1][j-1]+1

7. (19')標題:日期問題
小明正在整理一批歷史文獻。這些歷史文獻中出現了很多日期。小明知道這些日期都在1960年1月1日至2059年12月31日。令小明頭疼的是,這些日期採用的格式非常不統一,有采用年/月/日的,有采用月/日/年的,還有采用日/月/年的。更加麻煩的是,年份也都省略了前兩位,使得文獻上的一個日期,存在很多可能的日期與其對應。  

比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。  

給出一個文獻上的日期,你能幫助小明判斷有哪些可能的日期對其對應嗎?

輸入
----
一個日期,格式是"AA/BB/CC"。  (0 <= A, B, C <= 9)  

輸出
----
輸出若干個不相同的日期,每個日期一行,格式是"yyyy-MM-dd"。多個日期按從早到晚排列。  

樣例輸入
----
02/03/04  

樣例輸出
----
2002-03-04  
2004-02-03  
2004-03-02  

資源約定:
峰值記憶體消耗(含虛擬機器) < 256M
CPU消耗  < 1000ms

請嚴格按要求輸出,不要畫蛇添足地列印類似:“請您輸入...” 的多餘內容。

注意:
main函式需要返回0;
只使用ANSI C/ANSI C++ 標準;
不要呼叫依賴於編譯環境或作業系統的特殊函式。
所有依賴的函式必須明確地在原始檔中 #include <xxx>
不能通過工程設定而省略常用標頭檔案。

提交程式時,注意選擇所期望的語言型別和編譯器型別。


#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;

int mt[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isleap(int y){
	return (y%400==0||(y%4==0&&y%100!=0));
}
struct date{
	int y,m,d;
	date(){}
	date(int y,int m,int d):y(y),m(m),d(d){}
	bool operator<(date x)const{
		if(y == x.y){
			if(m == x.m) return d<x.d;
			return m<x.m;
		}
		return y<x.y;
	}
	bool valid(){
		if(y<1960||y>2059) return false;
		if(m<1||m>12) return false;
		if(isleap(y)){//閏年
			 if(m == 2) return (d>=1&&d<=29);
			 return (d>=1&&d<=mt[m]); 
			
		}
		else{
			return (d>=1&&d<=mt[m]);
		}
	}
	void print()const{
		printf("%d-%02d-%02d\n",y,m,d);
	}
};
set<date> s;
void insert(int a,int b,int c){
	date x = date(a,b,c);
	if(x.valid()) s.insert(x);
}
int main(){
	int a,b,c;
	scanf("%d/%d/%d",&a,&b,&c);
	
	insert(1900+a,b,c);
	insert(1900+c,a,b);
	insert(1900+c,b,a);
	insert(2000+a,b,c);
	insert(2000+c,a,b);
	insert(2000+c,b,a);
	set<date>::iterator it = s.begin();
	for(;it!=s.end();it++){
		it->print();
	}
	 return 0;
}

8. (21')標題:包子湊數
小明幾乎每天早晨都會在一家包子鋪吃早餐。他發現這家包子鋪有N種蒸籠,其中第i種蒸籠恰好能放Ai個包子。每種蒸籠都有非常多籠,可以認為是無限籠。

每當有顧客想買X個包子,賣包子的大叔就會迅速選出若干籠包子來,使得這若干籠中恰好一共有X個包子。比如一共有3種蒸籠,分別能放3、4和5個包子。當顧客想買11個包子時,大叔就會選2籠3個的再加1籠5個的(也可能選出1籠3個的再加2籠4個的)。

當然有時包子大叔無論如何也湊不出顧客想買的數量。比如一共有3種蒸籠,分別能放4、5和6個包子。而顧客想買7個包子時,大叔就湊不出來了。

小明想知道一共有多少種數目是包子大叔湊不出來的。

輸入
----
第一行包含一個整數N。(1 <= N <= 100)
以下N行每行包含一個整數Ai。(1 <= Ai <= 100)  

輸出
----
一個整數代表答案。如果湊不出的數目有無限多個,輸出INF。

例如,
輸入:
2  
4  
5   

程式應該輸出:
6  

再例如,
輸入:
2  
4  
6    

程式應該輸出:
INF

樣例解釋:
對於樣例1,湊不出的數目包括:1, 2, 3, 6, 7, 11。  
對於樣例2,所有奇數都湊不出來,所以有無限多個。  

資源約定:
峰值記憶體消耗(含虛擬機器) < 256M
CPU消耗  < 1000ms

請嚴格按要求輸出,不要畫蛇添足地列印類似:“請您輸入...” 的多餘內容。

注意:
main函式需要返回0;
只使用ANSI C/ANSI C++ 標準;
不要呼叫依賴於編譯環境或作業系統的特殊函式。
所有依賴的函式必須明確地在原始檔中 #include <xxx>
不能通過工程設定而省略常用標頭檔案。

提交程式時,注意選擇所期望的語言型別和編譯器型別。

分析:所有數的最大公約數不是1 --->有無限多湊不出來的數

        所有數的最大公約數是1 ---->遞推出所有不能湊出來的數

程式碼:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
const int N =105;
int ans = 0;
int gcd(int a,int b){
	int t;
	while(t = a%b){
		a=b;
		b=t;
	}
	return b;
}
int arr[N];
int d[N];
int main(){
	int n;cin>>n;
	for(int i =0;i<n;i++){
		cin>>arr[i];
	}
	int flag = arr[0];
	for(int i =1;i<n;i++){
		flag = gcd(flag,arr[i]);
	}
	if(flag!=1){
		cout<<"INF"<<endl;
	}
	else{
		d[0] =1;
		for(int i =0;i<n;i++){
			for(int j = 0;j+arr[i]<N;j++){
				if(d[j]) d[j+arr[i]] = 1;
			}
		}
	
		for(int i = N -1;i>=0;i--){
			if(d[i] == 0){
				ans++;
			}
		}
		cout<<ans<<endl;
	}
	
	 return 0;
}

9. 標題: 分巧克力
    兒童節那天有K位小朋友到小明家做客。小明拿出了珍藏的巧克力招待小朋友們。
    小明一共有N塊巧克力,其中第i塊是Hi x Wi的方格組成的長方形。

    為了公平起見,小明需要從這 N 塊巧克力中切出K塊巧克力分給小朋友們。切出的巧克力需要滿足:

    1. 形狀是正方形,邊長是整數  
    2. 大小相同  

例如一塊6x5的巧克力可以切出6塊2x2的巧克力或者2塊3x3的巧克力。

當然小朋友們都希望得到的巧克力儘可能大,你能幫小Hi計算出最大的邊長是多少麼?

輸入
第一行包含兩個整數N和K。(1 <= N, K <= 100000)  
以下N行每行包含兩個整數Hi和Wi。(1 <= Hi, Wi <= 100000) 
輸入保證每位小朋友至少能獲得一塊1x1的巧克力。   

輸出
輸出切出的正方形巧克力最大可能的邊長。

樣例輸入:
2 10  
6 5  
5 6  

樣例輸出:
2

資源約定:
峰值記憶體消耗(含虛擬機器) < 256M
CPU消耗  < 1000ms

請嚴格按要求輸出,不要畫蛇添足地列印類似:“請您輸入...” 的多餘內容。

注意:
main函式需要返回0;
只使用ANSI C/ANSI C++ 標準;
不要呼叫依賴於編譯環境或作業系統的特殊函式。
所有依賴的函式必須明確地在原始檔中 #include <xxx>
不能通過工程設定而省略常用標頭檔案。

提交程式時,注意選擇所期望的語言型別和編譯器型別。

分析:二分

程式碼:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
const int N =100005;
long long h[N];
long long w[N];
int n,k;
bool check(int num){
	long long sum = 0;
	for(int i =0;i<n;i++){
		sum+=(h[i]/num)*(w[i]/num);
		if(sum>=k) return true;
	}
	return false;
}
int bf(){
	long long l = 1,r=100005,ans;
	while(l<=r){
		ans =  (l+r) / 2;
		if(check(ans)) l = ans+1;
		else r = ans - 1;
	}
	l++;
	while(l--) if(check(l)) break;
	return l;
}
int main(){
	cin>>n>>k;
	for(int i = 0;i<n;i++) cin>>h[i]>>w[i];
	cout<<bf()<<endl;
	 return 0;
}

10. 標題: k倍區間
給定一個長度為N的數列,A1, A2, ... AN,如果其中一段連續的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍數,我們就稱這個區間[i, j]是K倍區間。  

你能求出數列中總共有多少個K倍區間嗎?  

輸入
-----
第一行包含兩個整數N和K。(1 <= N, K <= 100000)  
以下N行每行包含一個整數Ai。(1 <= Ai <= 100000)  

輸出
-----
輸出一個整數,代表K倍區間的數目。  

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

程式應該輸出:
6

資源約定:
峰值記憶體消耗(含虛擬機器) < 256M
CPU消耗  < 2000ms

請嚴格按要求輸出,不要畫蛇添足地列印類似:“請您輸入...” 的多餘內容。

注意:
main函式需要返回0;
只使用ANSI C/ANSI C++ 標準;
不要呼叫依賴於編譯環境或作業系統的特殊函式。
所有依賴的函式必須明確地在原始檔中 #include <xxx>
不能通過工程設定而省略常用標頭檔案。

提交程式時,注意選擇所期望的語言型別和編譯器型別。

分析:計算出所有1到i的和存到sum[i]中,則題目要求可以寫為(sum[i] - sum[j])%k == 0

可以化成:sum[i]%k == sum[j]%k

在計算sum陣列時,直接算出sum[i]%k

然後將同樣的值存入d陣列中就可以算出答案

程式碼:

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#include<Windows.h>
#include<cstdlib>
using namespace std;
const int N =100005;
int d[N],sum[N],n,k;
int main(){
	cin>>n>>k;
	for(int i =0;i<n;i++) cin>>sum[i];
	sum[0]%=k;
	int total = 0;
	for(int i =1;i<n;i++)
		sum[i] = (sum[i-1]+sum[i])%k;
	for(int i = 0;i<n;i++)
		total+=(d[sum[i]]++);
		/*如加入第一個數時,沒有組合,sum加上0,
		    加入第二個數時,有一種組合,sum加上1
			加入第三個數時,與前面兩個數形成兩種組合,sum加上2,
			以此類推*/ 
	total+=d[0];//加上單獨一個數的 
	cout<<total<<endl; 
	return 0;
}