Blue Bridge Cup title - newspaper pages / coal balls / square circles / printing squares / calculation formula

1, Pages of newspaper

Planet X is the same as our earth's City Morning Post,
It's just a bunch of individual pieces of paper. Each paper is printed with 4 editions.
For example, a newspaper contains four pages: 5, 6, 11, 12,
You can be sure that it should be the top 2 newspaper.
We found a newspaper of Planet X in space with four pages:
1125,1126,1727,1728
Please calculate the total number of pages in this newspaper (that is, the largest page number, not how many pages are used)?
Please fill in the number representing the total number of pages.

Train of thought:
If there are three newspapers, each newspaper is {1,2,11,12}, {3,4,9,10}, {5,6,7,8}
So the maximum page number is 12 + 1-1 10 + 3-1 8 + 5-1
So max+min-1; that is, 1728 + 1125-1 = 2852

2, Number of coal balls

There is a pile of coal balls, piled in a triangular pyramid. Specific:
One on the first floor,
The second layer consists of 3 (arranged in triangles),
The third layer consists of 6 (arranged in triangles),
The fourth layer consists of 10 (arranged in triangles),
...
If there are 100 floors, how many coal balls are there?
Please fill in the number indicating the total number of coal balls.

Train of thought:
Title: 1 + (1 + 2) + (1 + 2 + 3) + + (1+2+... +100) =?
The code can be used for calculation.

int main(){
	int i,j,sum1=0,sum2=0;
	for(i=1;i<=100;i++){  //100 floors in total, plus 100 times 
		sum1=0;
		for(j=1;j<=i;j++){  //Total number of each floor 
			sum1+=j;
		}
		sum2+=sum1;
	}
	printf("%d",sum2);
	return 0;
}

Operation result: 171700

3, Square cycle

If we sum every bit of a positive integer squared, we can get a new positive integer.
Do the same for the new positive integers.
In this way, you will find that no matter what number you start taking,
In the end, if it doesn't fall into 1, it will fall into the same circle.
Please write the largest number in this circle.
Please fill in the maximum number.

Idea: as the following code

int main(){
	int a=135,a1=0,s=0;
	while(1){//Cycle of a 
		a1=0;
		while(a>0){//Sum every bit of a positive integer squared to get a new positive integer.
			a1=a1+(a%10)*(a%10);
			a=a/10;
		}
		a=a1;
		if(s==a1)	break;
		else if(s<a1)
			s=a1;
	}
	printf("%d\n",s);
	return 0;
}

Operation result: 145

4, Print grid

Xiaoming wants to output m x n squares on the console.
For example, the output of 10x4 is as follows:

#include <stdio.h>
//Print the square of m column and n row 
void f(int m, int n)
{
	int row;
	int col;
	
	for(row=0; row<n; row++){
		for(col=0; col<m; col++) printf("+---");
		printf("+\n");
		for(col=0; col<m; col++) printf("|   ");
		printf("|\n");		
	}
	
	printf("+");
	_____________________________;   //Fill in the blanks
	printf("\n");
}

int main()
{
	f(10,4);
	return 0;
}

Note: only fill in the missing part of the dash, do not add any existing content or explanatory text.

Fill in the blank: for (Col = 0; col < m; col + +) printf("- +")
Specific analysis:

void f(int m, int n){
	int row,col;
	for(row=0; row<n; row++){//Print lines other than last 
		for(col=0; col<m; col++) printf("+---");
		printf("+\n");
		for(col=0; col<m; col++) printf("|   ");
		printf("|\n");		
	}
	printf("+");//Similar to for (Col = 0; col < m; col + +) printf ("+ ---")
	for(col=0;col<m;col++)	printf("---+");   //Fill in the blanks
	printf("\n");
}

5, Quick sort

Sorting is often used in various situations. Fast sorting is a very common and efficient algorithm.
The idea is: first select a "ruler" and use it to sieve the whole 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 we can 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;
}

Fill in the blank: swap(a,p,j);
Specific:

int partition(int a[], int p, int r){
//The idea of a quick sorting, p, r is the upper and lower limit of the sorting interval, x is the marker element - ruler
    int i = p;
    int j = r + 1;
    int x = a[p];
    while(1){
        while(i<r && a[++i]<x);//The front part doesn't stop until it's bigger than the ruler 
        while(a[--j]>x);	//The later part doesn't stop until it is smaller than the ruler 
        if(i>=j) break;
        swap(a,i,j);	//Big in front and small in back 
    }
	swap(a,p,j);//Fill in the blank / / swap the j element smaller than the ruler in the last move with the ruler 
    return j;
}

6, Rounding formula

In this formula, AI represents the number of 19, and different letters represent different numbers. For example:
6 + 8 / 3 + 952 / 714 is a solution,
5 + 3 / 1 + 972 / 486 is another solution.
How many solutions are there to this formula?
Note: your submission should be an integer. Do not fill in any extra content or explanatory text.

Train of thought:

  1. violence
  2. Depth first search: Thinking: finding the qualified equation in the full permutation of 1-9
#include<iostream>
using namespace std;
int ans=0,num[9];//ans= answer 
int v[10];//Mark whether used 
void judge(){//Judgement condition 
    if((num[0]+(double)num[1]/num[2]+(double)(num[3]*100+num[4]*10+num[5])/(num[6]*100+num[7]*10+num[8]))==10)
        ans++;
    return;
}
void dfs(int i){//I is the i-th bit of num [] 
    if(i==8+1){//i==8+1 means the first 8 numbers in num array have been placed 
        judge();
        return;
    }
    for(int j=1;j<10;j++){
        if(v[j]==0){//Judge whether the number has been used 
            v[j]=1;
            num[i]=j;
            dfs(i+1);//Place the next number 
            v[j]=0;//Backtracking, when a full permutation is satisfied, the next attempt is made
        }
    }
}
int main(){
    dfs(0);
    cout<<ans<<endl;
return 0;
}
Published 5 original articles, won praise 2, visited 67
Private letter follow

Posted by AnarKy on Tue, 25 Feb 2020 20:34:50 -0800