[Blue Bridge Cup] Exam Questions Training-Analysis of the Exam Questions of the 7th Lanqiao Cup Provincial C/C++ University Group B

first question 

1. The number
of briquettes There is a pile of briquettes in a triangular pyramid shape. Specifically:
1 in the first layer,
3 in the second layer (arranged in a triangle),
6 in the third layer (arranged in a triangle),
10 in the fourth layer (arranged in a triangle),
....
If there are 100 layers in total, How many briquettes are there?
Please fill in the number representing the total number of briquettes.
Note: What you submit should be an integer, do not fill in any extra content or explanatory text.

Answer: 171700

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	
	int sum = 0;
	int add = 1;
	int now = 0;
	for(int i = 0; i < 100; i++){
		now += add;
		sum += now;
		add++;
	}
	cout << sum ; 
	
	return 0;
}

 

Second question

Birthday candles
A certain person has held a birthday party every year since a certain year, and every time he blows out the same number of candles as his age.
Counting now, he blew out a total of 236 candles.
Excuse me, at what age did he start his birthday party?
Please fill in the age when he started the birthday party.
Note: What you submit should be an integer, do not fill in any extra content or explanatory text.

Answer: 26

#include <iostream>
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	
	for(int x = 1; x < 100; x++){
		for(int n = 0; n < 100; n++){
			if(n * x + n*(n-1)/2 == 236){
				cout << x;
			} 
		} 
	}	

	return 0;
}

 

Third question

Make up formula In
Picture.png
this formula, AI represents numbers from 1 to 9, and different letters represent different numbers.
For example:
6+8/3+952/714 is one solution,
5+3/1+972/486 is another solution.
How many solutions are there in this formula?
Note: Your submission should be a whole number, do not fill in any extra content or explanatory text.

Answer: 29

#include <iostream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int count = 0;
	for(int a = 1; a < 10; a++){
		for(int b = 1; b < 10; b++){
			if(b != a){
				for(int c = 1; c < 10; c++){
					if( c != b && c != a){
					for(int d = 1; d < 10; d++){
						if(d != c && d != b && d != a){
						for(int e = 1; e < 10; e++){
							if(e != d && e != c && e != b && e != a ){
							for(int f = 1; f < 10; f++){
								if(f != e && f != d && f != c && f != b && f != a){
								for(int g = 1; g < 10; g++){
									if(g != f && g != e && g != d && g != c && g != b && g != a){
									for(int h = 1; h < 10; h++){
										if(h != g && h != f && h != e && h != d && h != c && h != b && h != a){
											for(int i = 1; i < 10; i ++){
												if(i != h && i != g && i != g && i != f && i !=e && i != d && i != c && i != b && i !=a){
													int num1 = a * c * (g*100 + h*10 + i);
													int num2 = b * (g*100 + h*10 + i);
													int num3 = c * (d*100 + e*10 + f);
													int num4 = 10 * c * (g*100 + h*10 + i);
													if( num1 + num2 + num3 == num4){
														count ++;
													}
												}
											}
										}
									}
								}
								}
							}
							}
						}
						}
					}
					}
				}
			}
			}		
		}
	}
	
	cout << count;
	return 0;
}

Full arrangement: 

int main(){
	int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	int count = 0;
	do{
		int sum1, sum2, sum3, sum;
		sum1 = a[1] * a[3] * (a[7]*100 + a[8]*10 + a[9]);
		sum2 = a[2] * (a[7]*100 + a[8]*10 + a[9]);
		sum3 = a[3] * (a[4]*100 + a[5]*10 + a[6]);
		sum = 10 * a[3] * (a[7]*100 + a[8]*10 + a[9]);
		if(sum1 + sum2 + sum3 == sum){
			count ++;
		}
	}while(next_permutation(a+1, a+1+9));
	cout << count; 
}

 

Fourth question

Quick sort
Sorting is often used in various situations.
Quick sort is a very commonly used and efficient algorithm.
The idea is: first choose a "ruler" and
use it to sift the entire queue
to ensure that the elements on the left are not larger than it, and the elements on the right are not smaller than it.
In this way, the sorting problem is divided into two sub-intervals.
Then sort the subintervals separately.
The following code is an implementation, please analyze and fill in the missing code in the underlined part.

#include <stdio.h>
void swap(int a[], int i, int j)
{
	int t = a[i];
	a[i] = a[j];
	a[j] = t;
}
int partition(int a[], int p, int r)
{
    int i = p;
    int j = r + 1;
    int x = a[p];
    while(1){
        while(i<r && a[++i]<x);
        while(a[--j]>x);
        if(i>=j) break;
        swap(a,i,j);
    }
	______________________;
    return j;
}
void quicksort(int a[], int p, int r)
{
    if(p<r){
        int q = partition(a,p,r);
        quicksort(a,p,q-1);
        quicksort(a,q+1,r);
    }
}   
int main()
{
	int i;
	int a[] = {5,13,6,24,2,8,19,27,6,12,1,17};
	int N = 12;
	
	quicksort(a, 0, N-1);
	
	for(i=0; i<N; i++) printf("%d ", a[i]);
	printf("\n");
	
	return 0;
}

Answer: swap(a, p, j); 

	int i = first;
	int j = end;
	int x = r[first];
	
	while(1){
		while(i < end && r[i++] < x)
		while(j > first && r[j--] > x);
		if(i >= j){
			break;
		}
		swap(r[i], r[j]);
	}
	swap(r[i], r[j]);

By the way, review the quick queue: 

#include <iostream>
using namespace std;

void swap(int *i,  int *j){
	int temp;
	temp = *i;
	*i = *j;
	*j = temp;
}

int Partion(int r[],  int first,  int end){
	int i = first, j = end;
	while(i < j){
		while(i < j && r[i] <= r[j]){
			j--;	//右侧扫描 
		}
		if(i < j){
			swap(&r[i], &r[j]);
			i++;	//右侧较小值交换到前面 
		}
		
		while(i < j && r[i] <= r[j]){
			swap(&r[i], &r[j]);
			j--;	//左侧较大值交换到后面 
		} 
	}
	return i; 
}

void QuickSort(int r[], int first, int end){
	int pivot;
	if(first < end){
		pivot = Partion(r, first, end);
		QuickSort(r, first, pivot-1);
		QuickSort(r, pivot+1, end);
	}
		
} 


int main(int argc, char** argv) {
	int a[] = {5,13,6,24,2,8,19,27,6,12,1,17};
	int N = 12;
	
	QuickSort(a, 0, N-1);
	
	for(int i = 0; i < N; i++){
		cout << i << " "; 
	} 

	return 0;
}

Fifth question

To draw lots for
Planet X, a 5-member observation group will be sent to Star W.
Among them:
Country A can send up to 4 people.
Country B can send up to 2 people.
Country C can send up to 2 people.

.How many different combinations of different countries will the observation mission sent to W Star in the end?
The following procedure solves this problem.
In the array a[] is the largest number of places that each country can send.
The result of the program execution is:
DEFFF
CEFFF
CDFFF
CDEFF
CCFFF
CCEFF
CCDFF
CCDEF
BEFFF
BDFFF
BDEFF
BCFFF
BCEFF
BCDFF
BCDEF
….
(Omitted below, a total of 101 lines)

#include <stdio.h>
#define N 6
#define M 5
#define BUF 1024
 
void f(int a[], int k, int m, char b[])
{
	int i,j;
	
	if(k==N){ 
		b[M] = 0;
		if(m==0) printf("%s\n",b);
		return;
	}
	
	for(i=0; i<=a[k]; i++){
		for(j=0; j<i; j++) b[M-m+j] = k+'A';
		______________________;  //填空位置
	}
}
int main()
{	
	int  a[N] = {4,2,2,1,1,3};
	char b[BUF];
	f(a,0,M,b);
	return 0;
}

Answer: f(a, k+1, mi, b);  

Understand the code analysis:

#include <stdio.h>
#define N 6		//六个国家 
#define M 5		//5个人 
#define BUF 1024
 
void f(int a[], int k, int m, char b[])
{
	int i,j;
	
	if(k==N){ 	//k是国家编号,当k==N时表示此时已经全部安排了 
		b[M] = 0;
		if(m==0) printf("%s\n",b);	//m表示剩余名额数量 
		return;
	}
	
	for(i=0; i<=a[k]; i++){
		for(j=0; j<i; j++) 
			b[M-m+j] = k+'A';		//M-m+j 表示选取几个国家中的五个人去的几种方式 
//		______________________;  //填空位置,得出这里是个递归 
		f(a, k+1, m-i, b); 
	}
}
int main()
{	
	int  a[N] = {4,2,2,1,1,3}; 	//六个国家, a[i] 分别表示每个国家可以去的人数 
	char b[BUF];
	f(a,0,M,b);
	return 0;
}

 

Sixth question

Fill in the number of squares. Fill in the numbers
from

0 to 9 in the following 10 squares . Requirement: Two consecutive numbers cannot be adjacent.
(The left and right, the top and bottom, and the opposite corners are all considered adjacent)
How many possible filling schemes are there?
Please fill in an integer representing the number of plans.
Note: What you submit should be an integer, do not fill in any extra content or explanatory text.

Answer: 1580

Here, all the traversal graphs used in DFS are from left to right and then start a new line, instead of turning from left to right, this method can be relatively simple, and it can be checked every time the number is filled, only four aspects need to be checked , Namely (left, top left, top, top right).

Then every time you check, the difference between the number in different directions and the number, the absolute value is 1, which means that the adjacent ends directly return false, until all the directions are tested, you can return true as a suitable number.

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int a[4][4];
int vis[11];
int cnt=0;
bool check(int x,int y,int v){
    //转向
    int dir[4][2]={
   
   {0,-1},{-1,-1},{-1,0},{-1,1}};//左 左上  上 右上
    for(int i=0;i<4;i++){
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(xx<3 && xx>=0 && yy<4 && yy>=0){
            if(abs(a[xx][yy]-v)==1)
				return false;
        }
    }
    return true;
}
void DFS(int x,int y){
    if(x==2 && y==3){
		cnt++;
		return;
	}	//遇到(2,3)说明结束
    
    for(int i=0; i<=9; i++){
        //要确保这个数没被选过并且放在这里符合题意
        if(vis[i]==0 && check(x,y,i)){        
            vis[i]=1;
			a[x][y]=i;
            if(y+1 < 4) 
				DFS(x, y+1);	//向右搜索
            else 
				DFS(x+1, 0);	//另起一行
            vis[i]=0;
        }
    }
    return ;
}
int main(){
    for(int i=0; i<4; i++)//初始化一个较小的数
        for(int j=0; j<4; j++)
            a[i][j] = -20;
            
    DFS(0,1); 	//从(0,1)开始
    printf("%d\n",cnt);
    return 0;
}

 

Seventh question

Cut the stamps
As shown in the picture, there are 12 postage stamps with 12 zodiac signs connected together.

Now you have to cut out 5 sheets from it, and the requirements must be connected. (Just connecting one corner is not considered connected) For
example, in the figure below, the part shown in pink is a qualified cut.


Please calculate how many different clipping methods there are.
Please fill in an integer representing the number of plans.
Note: What you submit should be an integer, do not fill in any extra content or explanatory text.

 

Question 8

Four square sum
Four square sum theorem, also known as Lagrange's theorem:
Every positive integer can be expressed as the sum of the squares of at most 4 positive integers.
If 0 is included, it can be expressed as the sum of the squares of 4 numbers.
For example:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2+ 1^2 + 2^2
(The ^ sign means power)
for a given For positive integers, there may be multiple representations of the sum of squares.
You are required to sort the 4 numbers:
0 <= a <= b <= c <= d
and arrange all possible representations in ascending order according to a, b, c, d as the joint primary key, and finally output the first representation
program The input is a positive integer N (N<5000000)
. 4 non-negative integers are required to be output, sorted from small to large, separated by spaces.
For example, input:
5
, the program should output:
0 0 1 2
For another example, input:
12
then The program should output:
0 2 2 2
Peak memory consumption <256M
CPU consumption <3000ms

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	int num;
	cin >> num; 
	
	
	for(int a = 0; a <= 3000; a ++){
		for(int b = a; b <= 3000; b++){
			for(int c = b; c <= 3000; c++){
				for(int d = c; d <= 3000; d++){
					if(a*a + b*b + c*c+ d*d == num){
						cout << a << " " << b << " " << c << " " << d ;
						return 0; 
					}
				}
			}
		}
	}
	
	return 0;
}

 

Question 9

Exchange bottles
There are N bottles, numbered 1 ~ N, placed on the shelf.
For example, there are 5 bottles:
2 1 3 5 4 It is
required to pick up 2 bottles at a time and exchange their positions.
After several times, the serial number of the bottle is:
1 2 3 4 5
For such a simple situation, it is obvious that it can be reset at least 2 times.
What if there are more bottles? You can solve it by programming.
The input format is two lines: the
first line: a positive integer N (N<10000), indicating the number of bottles. The
second line: N positive integers, separated by spaces, indicating the current arrangement of the bottles.
The output data is a positive integer in one line, which means how many exchanges can be done at least to complete the sorting.
For example, input:
5
3 1 2 5 4 The
program should output:
3
Another example, input:
5
5 4 3 2 1 The
program should output:
2
Peak memory consumption <256M
CPU consumption <1000ms 

Sort of similar to selection sort 

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	int num;
	cin >> num;
	int data[10100];
	for(int i = 1; i <= num; i++){
		cin >> data[i];
	}
	int count = 0;
	for(int i = 1; i <= num; i++){
		if(data[i] != i){
			int temp = data[i];
			for(int j = 1; j <= num; j++){
				if(data[j] == i){
					data[i] = data[j];
					data[j] = temp;
					count ++;
				}
			}
		}
	}
	cout << count;

	
	return 0;
}

 Review the selection sorting, each time the selection sorting selects the smallest value to sort in the unsorted


//复习选择排序
void swap(int *x, int *y){
	int temp = *x;
	*x = *y;
	*y = temp;
}
int scanForMin(int A[], int i, int len){
	int temp = i;
	for(int j = i+1; j < len; j ++){
		if(A[j] < A[temp]){
			temp = j;
		}
	}
	return temp;
}
void selectSort(int a[], int N){
	int minPos;
	for(int i = 0; i < N; i++){
		minPos = scanForMin(a, i, N);
		swap(&a[i], &a[minPos]);
	}
} 
int main(){
	int A[6] = {2, 5, 9, 7, 1, 4};
	selectSort(A, 6);
	
	for (int i = 0; i < 6; i++)
	{
		cout << A[i] << " ";
	}
	cout << endl;

 

Tenth question

There
is an M-level award in a grand prix on planet X with the largest proportion . The bonus for each level is a positive integer.
And, the ratio between two adjacent levels is a fixed value.
In other words: the bonus numbers of all levels constitute a geometric series. For example:
16,24,36,54
whose equal ratio is: 3/2.
Now, we randomly investigate the number of prizes of some winners.
Please calculate the largest possible equivalence based on this.
Input format: The
first line is a number N (0<N<100), which means that the next line contains N positive integers. The
second line contains N positive integers Xi (Xi<1 000 000 000 000), separated by spaces. Each integer represents the amount of bonus of someone surveyed.
Required output:
A score in the form of A/B, requiring A and B to be relatively prime. Represents the possible maximum scale factor The
test data ensures that the input format is correct and the maximum scale exists.
For example, input:
3
1250 200 32 The
program should output:
25/4
For another example, input:
4
3125 32 32 200
Resource convention:
Peak memory consumption <256M
CPU consumption <3000ms

 

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/114965814