[Blue Bridge Cup] The 7th C/C++ Group B Provincial Tournament

1. 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 in total Layer, 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.

 

#include <stdio.h>
#include <stdlib.h>

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

int main(int argc, char *argv[]) {
	int i;
	int sum = 0;
	for(i = 0 ; i < 101 ; i++){
		sum += (i+1)*i/2; 
	}
	printf("%d",sum);
	return 0;
}

Answer: 171700

2. 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.

 

#include <stdio.h>
#include <stdlib.h>

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

int main(int argc, char *argv[]) {
	int i,j;
	int n = 0;
	for(i = 1; i < 120; i++){
		for(j = i; j <120; j++){
			n += j;
			if(n == 236){
				printf("%d",i);
			}
		}
		n = 0;
	} 
	return 0;
}

Answer: 26

 

3. Make up the formula

 


In this formula, A~I represent numbers from 0 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.
Correction: A~I represent numbers from 1 to 9

Violence law:

#include <stdio.h>
#include <stdlib.h>

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

int main(int argc, char *argv[]) {
	int a, b, c, d, e, f, g, h, i;
	int sum=0;
	for(a = 1; a < 10; a++){
		for(b = 1; b < 10; b++){
			for(c = 1; c < 10; c++){
				for(d = 1; d < 10; d++){
					for(e = 1; e < 10; e++){
						for(f = 1; f < 10; f++){
							for(g = 1; g < 10; g++){
								for(h = 1; h < 10; h++){
									for(i = 1; i < 10; i++){
										if((a*c*(g*100+h*10+i))+(b*(g*100+h*10+i))+((d*100+e*10+f)*c) == (10*c*(g*100+h*10+i))&&
										 (a!=b)&&(a!=c)&&(a!=d)&&(a!=e)&&(a!=f)&&(a!=g)&&(a!=h)&&
										 (a!=i)&&(b!=c)&&(b!=d)&&(b!=e)&&(b!=f)&&(b!=g)&&(b!=h)&&
										 (b!=i)&&(c!=d)&&(c!=e)&&(c!=f)&&(c!=g)&&(c!=h)&&(c!=i)&&
										 (d!=e)&&(d!=f)&&(d!=g)&&(d!=h)&&(d!=i)&&(e!=f)&&(e!=g)&&
										 (e!=h)&&(e!=i)&&(f!=g)&&(f!=h)&&(f!=i)&&(g!=h)&&(g!=i)&&(h!=i)){
											//printf("%d %d %d %d %d %d %d %d %d\n",a,b,c,d,e,f,g,h,i);
											sum++;
										}
									} 
								} 
							} 
						} 
					} 
				} 
			} 
		} 
	} 
	printf("%d",sum);
	return 0;
}

Answer: 29

 

Full array method:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

int main(){
    
    int a[10];
    //1 2 3 4 5 6 7 8 9 
    //A B C D E F G H I
    int i;
    int sum[4];
    int ans;
    
    for(i=1;i<=9;++i){
        a[i]=i;
    }
    
    ans=0;
    do{
        
        sum[0]=a[1]*a[3]*(a[7]*100+a[8]*10+a[9]);
        sum[1]=a[2]*(a[7]*100+a[8]*10+a[9]);
        sum[2]=(a[4]*100+a[5]*10+a[6])*a[3];
        
        sum[3]=10*a[3]*(a[7]*100+a[8]*10+a[9]);
        
        if(sum[0]+sum[1]+sum[2]==sum[3]){
            ++ans;
        }
        
    }while(next_permutation(a+1,a+1+9));
    
    printf("%d\n",ans);
    
    return 0;
}

4. Quick sort

 

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 codes in the underlined part.

Note: Only fill in the missing content, do not write any existing codes or explanatory text on the title.

 

#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);
    }
    //______________________;
    swap(a,p,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)

 

5. Draw lots

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.
....
So how many different combinations of 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 results of the program execution are:
DEFFF
CEFFF
CDFFF
CDEFF
CCFFF
CCEFF
CCDFF
CCDEF
BEFFF
BDFFF
BDEFF
BCFFF
BCEFF
BCDFF
BCDEF
....
(Omitted below, a total of 101 lines)

Read the code carefully and fill in the missing content in the underlined part.
Note: Do not fill in any existing content or explanatory text.

#include <stdio.h>
#define N 6
#define M 5
#define BUF 1024

int sum=0;

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);
            ++sum;
        }
        return;
    }
    
    for(i=0; i<=a[k]; i++){
        for(j=0; j<i; j++) b[M-m+j] = k+'A';
        //______________________;  //填空位置
        //f(a,k+1,m-i,b);
        f(a,k+1,m-j,b);
    }
}
int main()
{    
    int  a[N] = {4,2,2,1,1,3};
    char b[BUF];
    f(a,0,M,b);
    
    printf("sum = %d\n",sum);
    return 0;
}

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

No follow-up. .

Guess you like

Origin blog.csdn.net/weixin_42339197/article/details/88360444