2017年第八屆藍橋杯C/C++B組省賽題目解析


一、 購物單

小明剛剛找到工作,老板人很好,只是老板夫人很愛購物。老板忙的時候經常讓小明幫忙到商場代為購物。小明很厭煩,但又不好推辭。

這不,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,不要填寫任何多余的內容。

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

 

分析:本題考查C語言對文件的處理。讀取文件就行了,把清單復制出來去掉*號和折,記得把8折9折之類的換成80、90,半價換成50。

#include<stdio.h>
int main()
{
    double x,y;
    double count=0;
    freopen("r1.txt","r",stdin);
    while(scanf("%lf%lf",&x,&y)!=EOF)
    {
        count+=x*y/100;
    }
    printf("%lf",count);
    
    return 0;
}

 

答案:5110


二、等差素數列

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

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

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

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

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

 

#include<iostream> 
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
using namespace std;

int main()
{
    bool b[10001];
    memset(b, 1, sizeof(b));
    b[1] = 0;
    for (int i = 2; i <= 10000; i++)
        for (int j = 2; j <= sqrt(i); j++)
            if (!(i % j)) b[i] = 0; 
    for (int j = 1; j <= 1000; j ++ )
        for (int i = 2; i <= 10000 - j * 9; i++)
        {
            bool yes = 1;
            for (int k = 0; k <= 9; k++)
                yes = yes && b[i + k * j];
            if (yes) {
                cout << j;
                system("pause");
                return 0;
            }
        }
}

 

答案:210

 

三、承壓計算

X星球的高科技實驗室中整齊地堆放着某批珍貴金屬原料。

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

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

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

工作人員發現,其中讀數最小的電子秤的示數為:2086458231

請你推算出:讀數最大的電子秤的示數為多少?

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

 

分析:考查文件的讀取。把每一塊原料的重量平均分配到它地下的兩塊原料,最后枚舉最底下的一排數據即可。

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

int main()
{
    fstream in("data.in");
    double s[32][32], a, b;
    memset(s, 0, sizeof(s));
    
    for (int i = 1; i <= 29; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            in >> s[i][j];
            s[i][j] += (s[i - 1][j] / 2) + (s[i - 1][j - 1] / 2);
        } 
    }
    int i = 30;
    for (int j = 1; j <= i; j++)
    {
        in >> s[i][j];
        s[i][j] += (s[i - 1][j] / 2) + (s[i - 1][j - 1] / 2);
    }
    double mi = s[30][1], ma=s[30][1];
    for (int i = 1; i <= 30; i++)
    {  
        if (mi > s[30][i]) mi = s[30][i];
        if (ma < s[30][i]) ma = s[30][i];
    }
    cout << mi << " " <<(long long)(  ma * (((long long)2086458231) / mi));
    system("pause");
    return 0;

}

 

答案:3

 

四、方格分割

6x6的方格,沿着格子的邊線剪開成兩部分。
要求這兩部分的形狀完全相同。

如圖:

 

就是可行的分割法。

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

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

 

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int N = 6;
int ans = 0;
int mpt[N+1][N+1];
int dir[4][2] = {0,1,1,0,0,-1,-1,0};
void dfs(int x,int y)
{
    if(x == 0 || y == 0 || x == N || y == N){
        ans ++;
        return;
    }
    for(int i = 0 ; i < 4 ; i ++)
    {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if(mpt[tx][ty])continue;
        mpt[tx][ty] = 1;
        mpt[N-tx][N-ty] = 1;
        dfs(tx,ty);
        mpt[tx][ty] = 0;
        mpt[N-tx][N-ty] = 0;
    }
}
int main()
{
    mpt[N/2][N/2] = 1;
    dfs(N/2,N/2);
    printf("%d\n",ans/4);
    return 0;
}

 

答案:509

 

五、取數位

求1個整數的第k位數字有很多種方法。
以下的方法就是一種。

對於題目中的測試數據,應該打印5。

請仔細分析源碼,並補充划線部分所缺少的代碼。

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

// 求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;
}

 

#include<stdio.h>

// 求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 f(x/10,k);  //填空
}
    
int main()
{
    int x = 23574;
    printf("%d\n", f(x,3));
    return 0;
}
完整代碼

 

答案:f(x/10,k)

 

六、最大公共子串

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

比如:"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;
}

 

分析:考查dp。a[i][j]表示前一個字符串的前i位與后一個字符串的前j位的公共字符串長度

#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] = a[i-1][j-1]+1;  //填空
                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

 

七、日期問題

小明正在整理一批歷史文獻。這些歷史文獻中出現了很多日期。小明知道這些日期都在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<iostream>
#include<cstring> 
#include<cstdlib>
#include<algorithm>
#include<math.h> 
#include<string>
using namespace std;
string s;
struct ppp{
    long year, mouth, day;
} e[100];
long tot;
const int od[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int get_char(char c)
{
    return int(c) - int('0');
}

int cmp(const void  *i, const void *j)
{
    struct ppp *a = (struct ppp  *) i;
    struct ppp *b = (struct ppp  *) j;
    if (a->year != b->year)return a->year > b->year;
    if (a->mouth != b->mouth) return a->mouth > b->mouth;
    return a->day > b->day;
}

bool pdd(int year, int mouth, int day)
{
    int x = 0;
    if (!(year % 400) || !(year % 4)) x = 1;
    if (mouth > 12) return 0;
    if (mouth == 2 && day <= od[2] + x) return 1;
    if (day > od[mouth]) return 0;
    return 1;
}

void pd(int year, int mouth, int day)
{
    if (year < 60) year = 2000 + year;
    else year = 1900 + year;
    if (pdd(year, mouth, day))
    {
        ++tot;
        e[tot].year = year;
        e[tot].mouth = mouth;
        e[tot].day = day;
    }
}

int main()
{
    cin >> s;
    int x, y, z;
    x = get_char(s[0]) * 10 + get_char(s[1]);
    y = get_char(s[3]) * 10 + get_char(s[4]);
    z = get_char(s[6]) * 10 + get_char(s[7]);
    tot = -1;
    if (y <= z) swap(y, z);
    if (x <= y) swap(x, y);
    if (y <= z) swap(y, z);
    if (x == y && x == z) pd(x, y, z);
    if (x == y && x != z)
    {
        pd(x, y, z);
        pd(z, x, y);
        pd(x, z, y);
    }
    if (z == y && x != z)
    {
        pd(x, y, z);
        pd(z, x, y);
        pd(z, y, x);
    }
    if (x != y && x != z && y != z)
    {
        pd(x, y, z);   pd(x, z, y);
        pd(y, x, z);   pd(y, z, x);
        pd(z, x, y);   pd(z, y, x);
    }
    qsort(e, tot + 1, sizeof(ppp), cmp);
    for (int i = 0; i <= tot; i++)
        cout << e[i].year << "/" << e[i].mouth << "/" << e[i].day << endl;
    system("pause");
}

 

 

八、包子湊數

小明幾乎每天早晨都會在一家包子鋪吃早餐。他發現這家包子鋪有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>
不能通過工程設置而省略常用頭文件。

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

 

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
int gcd(int a,int b){
    if(b == 0) return a;
    return gcd(b,a%b);
}
int arr[110],n;
const int N = 10010;
bool bk[N];
int main()
{
    scanf("%d",&n);
    for(int i = 0 ; i < n ; i ++)
        scanf("%d",&arr[i]);
    int g = arr[0];
    for(int i = 1 ; i < n ; i ++)
        g = gcd(g,arr[i]);
    if(g != 1)
    {
        printf("INF\n");
    }else{
        bk[0] = true;
        for(int i = 0 ; i < n ; i ++)
        {
            for(int j = 0 ; j + arr[i] < N ; j ++)
                if(bk[j])bk[j+arr[i]]= true;
        }
        int count = 0;
        for(int i = N-1 ; i >= 0 ; i --){
            if(bk[i] == false) count++;
        }
        printf("%d\n",count);
    }
    return 0;
}

 

九、分巧克力

兒童節那天有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<iostream>
#include<cstring> 
#include<cstdlib>
#include<algorithm>
#include<math.h> 
#define zy 100002 
using namespace std ;
long n , k , h[ zy ] , w[ zy ] , ans ;

bool pd( long  t ) 
{
    long tot = 0 ; 
    for ( int i = 1 ; i <= n ; i ++ )
      tot += ( h[ i ] / t ) * ( w[ i ] / t );
    if ( tot >= k ) return 1 ;
    return 0 ;
}

void find( long head , long last )
{
    if ( head > last ) return ;
    long t = (last + head ) >> 1 ;
    if ( pd( t ) )
    {        
     ans = max( ans , t ) ;
     find( t + 1 , last ) ;
    }
    else find( head , t - 1 ) ;
}
int main()
{
     cin >> n >> k ; 
     for ( long i = 1 ; i <= n ; i ++ ) 
         cin >> h[ i ]>> w[ i ] ;
    ans = 1 ;
    find( 1 , zy ) ;
    cout << ans ; 
    return 0 ;
}

 

十、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>
不能通過工程設置而省略常用頭文件。

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

 

分析:首先統計前綴和sum[i] 表示A1+A2+…+Ai.所以對於任意一段區間[l,r]的和就是sum[r]-sum[l-1]。如果要保證這個區間和為K倍數就是:(sum[r]-sum[l-1])%k == 0.變形后就是:sum[r]%k==sum[l-1]%k,所以我們計算前綴和的時候順帶模K,然后統計前綴和中相同的數據就行了。復雜度O(n),注意數據可能會溢出!

#include<stdio.h> 
long long t[100010]={0};
long long a[100010]={0};
int main()
{
    long long k,n;
    scanf("%lld%lld",&n,&k);
    int i;
    for(i = 1; i<=n;i++)
        scanf("%lld",&a[i]);
    long long sum=0;
    
    for(i=1;i<=n;i++)
        a[i] = (a[i]+a[i-1])%k;
    for(i=1;i<=n;i++)
        sum+=(t[a[i]]++);
    
    sum+=t[0];
    printf("%lld\n",sum);
    
    return 0;
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM