2017 8th Blue Bridge Cup C/C++ Program Design Undergraduate Group B Provincial Competition Topics Summary

1Title: Shopping List
Xiao Ming has just found a job, the boss is very nice, but the boss's wife loves shopping. When the boss is busy, he often asks Xiao Ming to go to the mall to do shopping on his behalf. Xiao Ming was very tired, but he couldn't refuse.
No, the XX promotion is here again! The boss's wife opened a long shopping list, all with discounts.
Xiao Ming also has a quirk. Unless it is a last resort, he never swipes his card and gets it directly in cash.
Xiao Ming is very upset now. Please help him calculate how much cash he needs to withdraw from the ATM to complete this shopping.
ATMs can only provide 100-yuan banknotes. Xiao Ming wants to withdraw as little cash as possible, as long as it is enough.
Your task is to calculate the minimum amount of cash that Xiao Ming needs to withdraw.
Here's a vexing shopping list, with item names hidden for privacy.
**** 180.90 12% off
**** 10.25 50% off
**** 56.14 10% off
**** 104.65 10% off
**** 100.30 12% off
**** 297.15 50% off
**** 26.75 15% off
** ** 130.62 50% off**** 240.28 52
% off
**** 270.62 20% off
**** 115.87 12% off
**** 247.34 5% off
**** 73.21 10% off
**** 101.00 50% off
**** 79.54 50% off**** 278.44
30% off
**** 199.26 50% off
**** 12.97 10% off
**** 166.30 22% off
**** 125.50 52% off
**** 84.98 10% off**** 113.35 10%
off
**** 166.57 50% off
**** 42.56 10% off
** ** 81.90
5% off **** 131.78 20% off**** 255.89 22%
off
**** 109.17 10% off
**** 146.69 62% off
**** 139.33 65% off
**** 141.16 22% off
*** * 154.74 20% off
**** 59.42 20% off
**** 85.44 62% off
**** 293.70 12% off
**** 261.79 65% off
**** 11.30 12% off
**** 268.27 52% off
**** 128.29 12% off
**** 251.03 20% off
**** 208.39 25% off
**** 128.88 25% off
**** 62.06 10% off
**** 225.87 25% off
**** 12.89 75% off
**** 34.28 25% off
**** 62.16 52% off
**** 129.12 50% off****
218.37
50% off**** 289.69 20% off

It should be noted that the 88% discount refers to 88% of the list price, while the 20% discount is calculated at 80%, and the rest are analogous.
In particular, half price is calculated at 50%.
Please submit the amount that Xiao Ming wants to withdraw from the ATM, in yuan.
The answer is an integer, similar to 4300, the end must be 00, do not fill in any extra content.
Special reminder: You are not allowed to bring calculators or turn on your cell phone.
Problem-solving idea 1:
①Calculate with the help of Excel table

insert image description here
insert image description here
Note that the question is an integer that means how much money you need to take out. Be sure to see the meaning of the question
. ②Programming to solve the problem

#include <iostream>
using namespace std;
int main(){
    freopen("t.txt","r",stdin);//将stdin定向到文件 stdin 是标准读入
    double ans = 0,a;
    int dis;
    string s;
    while(cin >> s >>a >> dis ){
        if(dis < 10){
            dis = dis*10;   
        }
        ans += dis*a/100;
    }
    printf("%.5f",ans);
    return 0;
}//5136.85950

Answer: 5200
2. Arithmetic prime number sequence

2,3,5,7,11,13,… is a sequence of prime numbers.
Similar: 7, 37, 67, 97, 127, 157 The arithmetic sequence composed entirely of prime numbers is called the arithmetic prime sequence.
The above sequence has a tolerance of 30 and a length of 6.
In 2004, Green and Chinese Tao Zhexuan cooperated to prove that there is a prime number arithmetic sequence of any length.
This is an amazing achievement in the field of number theory!
Based on this theory, please use the computer in your hand to search with confidence:
What is the minimum tolerance of the arithmetic prime sequence of length 10?
Note: What needs to be submitted is an integer, do not fill in any redundant content and description text.
Problem solving ideas:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int a[10000] = {0};
bool ok(int n, int k) {
    for(int i = 0; i < 10; i++) {
        if(! a[n + i * k]){
            return false;   
        }
    }
    return true;
}
int main() {
    for(int i = 2; i < 10000; i++) {
        int t = 1;
        for(int j = 2; j*j <= i; j++) {
            t = i % j;
            if(t == 0) break;
        }
        if(t == 0){
            a[i] = 0;   //不是素数 
        }else{
            a[i] = 1; //素数 
        }       
    }
    for(int k = 1; ; k++) {
        for(int i = 2; i < 10000; i++) {
            if(a[i] && ok(i,k)) {
                cout << k;
                return 0;
            }
        }
    }
    return 0;
}

3. Pressure calculation

A certain batch of precious metal raw materials are neatly stacked in the high-tech laboratory of Planet X.
The shape and size of each piece of metal raw material are exactly the same, but the weight is different.
Metal materials are stacked strictly in a pyramid shape.

							 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 

The number in it represents the weight of the metal block (the larger unit of measure).
The X on the bottom layer represents 30 extremely high-precision electronic scales.
Assuming that the weight of each piece of raw material falls equally on the two metal pieces below it, in the
end, the weight of all metal pieces falls on the bottom electronic scale strictly and precisely.
Electronic scales have small units of measurement, so the numbers displayed are large.
The staff found that the indication of the electronic scale with the smallest reading is: 2086458231
Please calculate: What is the indication of the electronic scale with the largest reading?
Note: What needs to be submitted is an integer, do not fill in any redundant content.
Problem- solving idea:
In fact, this question looks very scary, but in fact it is just a lot of data. The model is very simple: put all the metal blocks in the i-th row in the 1-i positions of the i-th row. In this way, the m-th block in the k-th row will be equally shared on the two metal blocks in the row below that support it. This only needs to transfer the weight down from the first row to the last row. You can know the weight of the bottom layer.

#include <iostream>
#include <algorithm>
using namespace std;
double a[31][31] = {0};
int main() {
    for(int i = 1; i <= 29; i++) {
        for(int j = 1; j <= i; j++) {
            cin >> a[i][j];
        }
    }
    for(int i = 1; i <= 30; i++) {
        for(int j = 1; j <= i; j++) {
            a[i][j] += a[i-1][j] * 0.5 + a[i-1][j-1] * 0.5;
        }
    }
    double maxn = a[30][1], minn = a[30][1];
    for(int i = 1; i <= 30; i++){
        maxn = max(a[30][i],maxn);
        minn = min(a[30][i],minn);
    }
    printf("%lf",2086458231*maxn/minn);
    return 0;
}//72665192664.000000

4. Title: Grid Split

For a 6x6 grid, cut in two along the sides of the grid.
The two parts are required to be exactly the same shape.

As shown in the figure: p1.png, p2.png, p3.png are feasible segmentation methods.
insert image description hereinsert image description hereinsert image description here
Try to calculate:
Including these three division methods, how many different division methods are there in total.
Note: Rotational symmetry belongs to the same segmentation method.

Please submit this integer without any superfluous content or explanatory text.
Problem- solving ideas:
Approach: Carefully observe the sample data and find that to meet the requirements of the problem, only the cut line needs to be symmetrical about the center of the midpoint of the pattern. Then we can regard the borders between lattices as edges, and the intersections between edges as points. Then start from (3,3), find an edge to reach the outer circle of the pattern, but it is worth noting that starting from (3,3) is the wrong two people, and the lines of the two people are always symmetrical. So when marking in dfs, you need to mark two in one step. The final result needs to be divided by 4, because the title says that it is the same kind of rotational symmetry.

#include <iostream>
using namespace std;
int vis[7][7] = {0}, ans = 0;
int x[] = {0,0,-1,1}, y[] = {1,-1,0,0};
void dfs(int a, int b) {
    if(a == 0 || a == 6 || b == 0 || b == 6) {
        ans++;
        return ;
    } else {
        for(int i = 0; i < 4; i++) {
            int tx = a+x[i];
            int ty = b+y[i];
            if(vis[tx][ty] == 0) {
                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() {
    vis[3][3] = 1;
    dfs(3,3);
    cout << ans/4;
    return 0;
}

5Title: Take Digits

There are many ways to find the kth digit of an integer.
The following method is one.

// Find the digit length of x when expressed in decimal
int len(int x){ if(x<10) return 1; return len(x/10)+1; }


// Take the kth digit of
x int f(int x, int k){ if(len(x)-k==0) return x%10; return f(x / 10,k) ; //fill in the blanks }


int main(){ int x = 23574; printf("%d\n", f(x,3)); return 0; }



For the test data in the question, 5 should be printed.

Please analyze the source code carefully and add the missing code in the underlined part.

Note: Submit only missing code, do not fill in any existing content or descriptive text.
Answer: return f(x / 10,k) ; //fill in the blanks

6. Maximum common substring The
problem of maximum common substring length is to
find the maximum length of all substrings of two strings that can be matched.

For example: "abcdkkk" and "baabcdadabc",
the longest common substring that can be found is "abcd", so the maximum common substring length is 4.

The following program is solved by the matrix method, which is a more effective solution for the case where the size of the string is not large.

Please analyze the idea of ​​this solution and complete the missing code in the underlined part.

#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;
}

Note: Submit only missing code, do not submit existing code and symbols. Also do not submit explanatory text. Practice: It is a very classic model, and there are a lot of blogs on Baidu's largest public substring. Prompt that the meaning of a[i][j] means that the first i characters of the s1 string and the first j characters of the s2 string each contain the maximum common substring length of the last character. This is a dynamic programming problem.
7.7 Title: Date Issue
Xiao Ming is sorting out a batch of historical documents. Many dates appear in these historical documents. Xiao Ming knows that these dates are from January 1, 1960 to December 31, 2059. What troubles Xiao Ming is that the format of these dates is very inconsistent, some use year/month/day, some use month/day/year, and some use day/month/year. What's more troublesome is that the first two digits of the year are also omitted, so that there are many possible dates corresponding to a date in the literature.
For example, 02/03/04, it may be March 04, 2002, February 3, 2004, or March 02, 2004.
Given a date in the literature, can you help Xiao Ming determine what possible dates correspond to it?
Enter
a date in the format "AA/BB/CC". (0 <= A, B, C <= 9)
input

Output several different dates, one line for each date, in the format "yyyy-MM-dd". Multiple dates are arranged from morning to night.
sample input

02/03/04

Sample output

2002-03-04
2004-02-03
2004-03-02

Resource convention:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms
Please output strictly as required, and do not superfluous and print redundant content like: "Please enter...".
When submitting your program, take care to select the desired language type and compiler type.

Problem- solving idea: It is stipulated
whether the three parameters passed in can form a date yy-mm-dd
:
yy∈[00,59] yy = 20yy
yy∈[60,99] yy = 19yy
That is, yy does not need to be judged, the default is legal , but in some cases it is necessary to judge whether yy is a leap year/average year.
mm∈[01,12] otherwise invalid
dd∈[01,31] otherwise invalid
In particular:
dd∈[29,31] needs to be discussed in terms of month, leap year/average year

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct{
    int year, month, day;
}date;
bool isyn(int y){
    return (y % 4 == 0) || (y % 100 && y % 400 == 0);
}
void print(const date &d){
    printf("%02d-%02d-%02d\n", d.year, d.month, d.day);
}
bool compare(date d1, date d2){
    if(d1.year != d2.year){
        return d1.year < d2.year;
    }
    if(d1.month != d2.month){
        return d1.month < d2.month;
    }
    return d1.day < d2.day;
}
bool check(date d){
    static int month_days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(isyn(d.year)){
        month_days[2] = 29;
    }else{
        month_days[2] = 28;
    }
    if(d.year < 1960 || d.year > 2059){
        return false;
    }
    if(d.month < 1 || d.month > 12){
        return false;
    }
    if(d.day < 1 || d.day > month_days[d.month]){
        return false;
    }
} 
int main(){
    int aa, bb, cc;
    scanf("%d/%d/%d", &aa, &bb, &cc);
    date d[6] = {
        {2000 + aa, bb, cc},
        {1900 + aa, bb, cc},
        {2000 + cc, aa, bb},
        {1900 + cc, aa, bb},
        {2000 + cc, bb, aa},
        {1900 + cc, bb, aa}
    };
    sort(d, d + 6, compare);
    for(int i = 0; i < 6; ++ i){
        if(check(d[i])){
            print(d[i]);
        }
    }
    return 0;
}

8 Title: Steamed buns make up the number
Xiao Ming eats breakfast at a steamed bun shop almost every morning. He found that this steamed bun shop has N kinds of steamers, and the i-th steamer can hold Ai buns exactly. Each type of steamer has a very large number of cages, which can be considered as infinite cages.
Whenever a customer wants to buy X steamed buns, the uncle who sells steamed buns will quickly select several cages of steamed buns, so that there are exactly X steamed buns in these cages. For example, there are 3 types of steamers, which can hold 3, 4 and 5 buns respectively. When a customer wants to buy 11 buns, the uncle will choose 2 cages of 3 plus 1 cage of 5 (may also choose 1 cage of 3 plus 2 cages of 4).
Of course, sometimes Uncle Baozi can't make up the quantity that customers want to buy anyway. For example, there are 3 types of steamers, which can hold 4, 5 and 6 buns respectively. And when the customer wanted to buy 7 buns, the uncle couldn't come out.
Xiao Ming wanted to know how many kinds of numbers there were that Uncle Baozi couldn't figure out.
enter

The first line contains an integer N. (1 <= N <= 100)
Each of the following N lines contains an integer Ai. (1 <= Ai <= 100)

output

An integer representing the answer. If there are infinite numbers that cannot be made up, output INF.

For example,
enter:
2
4
5
The program should output:
6
For another example,
enter:
2
4
6

The program should output:
INF

Example explanation:
For example 1, the numbers that cannot be made up include: 1, 2, 3, 6, 7, 11.
For example 2, all odd numbers cannot be made up, so there are infinitely many.

Extended Euclidean Transformation

9. Divide chocolates
On Children's Day, K children visited Xiaoming's house. Xiao Ming took out a collection of chocolates to entertain the children.
Xiao Ming has a total of N pieces of chocolate, of which the i-th piece is a rectangle composed of Hi x Wi squares.
To be fair, Xiao Ming needs to cut out K pieces of chocolate from these N pieces of chocolate and distribute them to the children. The cut chocolate needs to meet:
1. The shape is a square, and the side length is an integer
2. The size is the same.
For example, a 6x5 chocolate can be cut into 6 2x2 chocolates or 2 3x3 chocolates.
Of course, the children all want the chocolate to be as large as possible. Can you help Xiao Hi calculate the maximum side length?
Input
The first line contains two integers N and K. (1 <= N, K <= 100000)
Each of the following N lines contains two integers Hi and Wi. (1 <= Hi, Wi <= 100000)
The input guarantees that each child will get at least one 1x1 chocolate.
Output
Output the maximum possible side length of the cut square chocolate.

Sample input:
2 10
6 5
5 6

Sample output:
2
Analysis: At first, I was confused, and later I realized that the product of the length and width divided by the side length of the chocolate is the number of chocolates that can be completely cut out. As long as the number is greater than the number of people, the side length can be considered to meet the conditions. Here the dichotomy method can be easily used to find the result

10. k times interval
Given a sequence of length N, A1, A2, … AN, if the sum of a continuous subsequence Ai, Ai+1, … Aj (i <= j) is a multiple of K, we This interval [i, j] is called a K times interval.
Can you find the total number of K-fold intervals in the sequence?
enter

The first line contains two integers N and K. (1 <= N, K <= 100000)
Each of the following N lines contains an integer Ai. (1 <= Ai <= 100000)

output

Output an integer representing the number of K-fold intervals.

For example,
input:
5 2
1
2
3
4
5 The
program should output:
6
Problem solving ideas:
Find the number of times that the sum of the interval [l, r] is a multiple of k. To find the interval sum, we can find it by the prefix sum. We specify that sum[i] represents the sum of the first element to the i-th element. Then sum[r] - sum[l-1] is the sum of the interval [l,r]. The sum of the interval [l,r] is a multiple of k ie (sum[r] - sum[l-1])%k == 0 ie sum[r]%k == sum[l-1]%k
  Then, We find each prefix sum, and take the modulo during the process, and two equal prefix sums can form a k-fold interval. After computing the prefix sum, we can use a two-level for loop to count the number of k-fold intervals. However, due to the large amount of data, this will time out. So can we record the number of k-fold intervals in the process of calculating the prefix sum?
We use an array cnt[i] to represent the number of prefixes and modulo equal to i before the current position. For example:
  the sequence 1 2 3 4 5 mod = 2
  modulo the sum of the first 1 number, there is 0 prefix before it is 1 and the modulo is 1, and the number + 0
  modulo the sum of the first 2 numbers , is 1, there is 1 prefix before and modulo is 1, the number+1 is
  modulo the sum of the first 3 numbers, is 0, there is 0 prefix before and the modulo is 0, the number+0 is
  the first 4 The sum of the numbers is modulo, there is 1 prefix before it is 0 and the modulo is 0, the number+1 is
  modulo the sum of the 5 numbers, it is 1, there are 2 prefixes before and the modulo is 1, the number + 2
  ans = 4 so far. But ans should be equal to 6, because after this calculation, we have missed the case that the sum of the first i numbers is a multiple of k, that is, the sum of the interval [0, i] is a multiple of k, so we have to use ans = On the basis of 4, add the prefix and the number of 0 after modulo, that is, ans+2 = 6;
1. Because (sum[r] - sum[l-1]) % k == 0, sum[r can be derived ] % k == sum[l - 1] % k.
2. Therefore, take the prefix sum modulo K separately.
3. Count the number of each number after the modulo is taken.

#include <bits/stdc++.h> 
#define LL long long
#define MAXN 1000
using namespace std;
int sum[100001],num[100001],cnt[100001]; // sum[i] 前i个元素的和 
int n,k;
LL ans = 0;
int main(){//前缀和 
    //freopen("input.txt","r",stdin);
    memset(cnt, 0, sizeof(cnt));
    memset(sum, 0, sizeof(sum));
    scanf("%d%d",&n, &k);
    for(int i = 1; i <= n; i++){
        scanf("%d",&num[i]);
        sum[i] = (sum[i-1] + num[i])%k;
        ans += cnt[sum[i]];
        cnt[sum[i]]++;
    }
    printf("%lld\n",ans+cnt[0]);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325686293&siteId=291194637