[Blue Bridge Cup] Test Questions-Basic Exercises-Full Version (with percentage code and partial explanation)

"Lanqiao Cup" Practice System (lanqiao.cn) 

Table of contents

1. A+B problem

2. Array sorting

3. Hexadecimal to octal

4. Hexadecimal to decimal

5. Decimal to hexadecimal

6. Special palindromes

7. Palindrome number

8. Special numbers

9. Yanghui Triangle

10. Find integers

11. Sequence features

12. Alphabet graphics

13.01 Strings

14. Leap year judgment

15.Fibonacci sequence

16. Area of ​​a circle

17. Sequence summation

18. Factorial calculation

19. High precision addition

20. Huffman tree

21.2n queen problem

22. Timekeeping assistant

23. Retrieval of numbers

24. Prediction of the tortoise and the hare

25. Chip test

26. String of FJ

27. Dance of Sine

28. How to read numbers

29. The price of perfection

30. Rectangular area intersection

31. Matrix multiplication

32. Decomposing prime factors

33. String comparison

34. Time Shift 


1. A+B problem

#include<bits/stdc++.h>
using namespace std;
int main(){
	int A,B;
	cin>>A>>B;
	cout<<A+B<<endl;
	return 0;
}

2. Array sorting

Given an array of length n, arrange the array in ascending order.

Output one line, and output the sorted sequence in ascending order.

  • Bubble sorting method: The basic principle is to compare the size of the data to be sorted two by two, and exchange them when the order of the two data does not meet the order condition, otherwise, it remains unchanged, so that the smallest (or largest) node each time Floats to the front of the sequence like a bubble.
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n];
	for(int i=0; i<n;i++){
		cin>>a[i];	
	}
	//COMPARE
	int temp=0;
	for(int i=0; i<n-1; i++){//-1防止数组角标越界
		for(int j=0; j<n-i-1; j++){
			if(a[j+1]<a[j]) {
				//temp=a[j+1];
				//a[j+1]=a[j];
				//a[j]=temp;	
				swap(a[j+1],a[j]);
			}
		}		
	}
	
	for(int i=0; i<n;i++){
		cout<<a[i]<<" " ;	
	}
	return 0;
}

3. Hexadecimal to octal

Given n positive hexadecimal integers, output their corresponding octal numbers.

  • Hexadecimal: 0~9 A~F 0000~1111
  • Octal: 0~7 000~111

The C library function char *strcpy(char *dest, const char *src) copies the string pointed to by src to dest. 

strcpy(Arry_eight,Arrx.c_str());
#include<bits/stdc++.h> 
using namespace std;
//二进制-八进制
void transform_8(string Arrx) {
	int n;
	n=Arrx.length();
	//检查是否需要补0 
	if(n%3==1) Arrx = "00"+Arrx;
	else if(n%3==2) Arrx = "0"+Arrx;
	//更新长度
	n=Arrx.length();
	char Arry_eight[n];
	//C 库函数 char *strcpy(char *dest, const char *src) 把 src 所指向的字符串复制到 dest。 
	strcpy(Arry_eight,Arrx.c_str());
	int i,j;
	//八进制位数 
	i=n/3; 
	//字符数组对应的八进制位数,数组从0开始计数 
	j=i-1; 	
	//数组前三均为0,则八进制此处为0,不在记录 
	if(Arry_eight[0]=='0'&&Arry_eight[1]=='0'&&Arry_eight[2]=='0') {
		j--;
		i--;
	}
	//八进制的个数为i 
	char Arrx_eight[i];
	//3位一组 
	int x, y, z;
	x=n-1;
	y=n-2;
	z=n-3;
	for(int k= i; k>0; k--){
		 if(Arry_eight[z]=='0'&&Arry_eight[y]=='0'&&Arry_eight[x]=='0') Arrx_eight[j]='0'; 
		 else if(Arry_eight[z]=='0'&&Arry_eight[y]=='0'&&Arry_eight[x]=='1') Arrx_eight[j]='1';  
		 else if(Arry_eight[z]=='0'&&Arry_eight[y]=='1'&&Arry_eight[x]=='0') Arrx_eight[j]='2';  
		 else if(Arry_eight[z]=='0'&&Arry_eight[y]=='1'&&Arry_eight[x]=='1') Arrx_eight[j]='3'; 
		 else if(Arry_eight[z]=='1'&&Arry_eight[y]=='0'&&Arry_eight[x]=='0') Arrx_eight[j]='4';  
		 else if(Arry_eight[z]=='1'&&Arry_eight[y]=='0'&&Arry_eight[x]=='1') Arrx_eight[j]='5';
		 else if(Arry_eight[z]=='1'&&Arry_eight[y]=='1'&&Arry_eight[x]=='0') Arrx_eight[j]='6';  
		 else if(Arry_eight[z]=='1'&&Arry_eight[y]=='1'&&Arry_eight[x]=='1') Arrx_eight[j]='7'; 
		 j--;
		 x-=3;
		 y-=3;
		 z-=3;
	} 
	for(int p=0; p<i; p++){
		cout<<Arrx_eight[p];
	}
	cout<<endl;	 
}


//十六进制-二进制
void transform_2(char num[], int n){
	string Arry[n];
	string Arrx;
	for(int i=0; i<n; i++){
		//如果为数字 
		if(num[i]=='0'||num[i]=='1'||num[i]=='2'||num[i]=='3'||num[i]=='4'
		 ||num[i]=='5'||num[i]=='6'||num[i]=='7'||num[i]=='8'||num[i]=='9'){
		 	if(num[i]=='0' && i!=0) Arry[i]="0000";
		 	if(num[i]=='1') Arry[i]="0001";
		 	if(num[i]=='2') Arry[i]="0010";
		 	if(num[i]=='3') Arry[i]="0011";
		 	if(num[i]=='4') Arry[i]="0100";
		 	if(num[i]=='5') Arry[i]="0101";
		 	if(num[i]=='6') Arry[i]="0110";
		 	if(num[i]=='7') Arry[i]="0111";
		 	if(num[i]=='8') Arry[i]="1000";
		 	if(num[i]=='9') Arry[i]="1001";
		 } 
		else if(num[i]=='A' || num[i]=='a') Arry[i]="1010";
		else if(num[i]=='B' || num[i]=='b') Arry[i]="1011";
		else if(num[i]=='C' || num[i]=='c') Arry[i]="1100";
		else if(num[i]=='D' || num[i]=='d') Arry[i]="1101";
		else if(num[i]=='E' || num[i]=='e') Arry[i]="1110";
		else if(num[i]=='F' || num[i]=='f') Arry[i]="1111";	 
	}
	//字符串合并
	for(int i=0; i<n; i++) {
		Arrx += Arry[i];
	}
	//转换成八进制 
	transform_8(Arrx);
}

int main(){
	int n; 
	//n行
	cin>>n; 
	string aa[n];
	//输入字符串 
	for(int i=0; i<n; i++){
		cin>>aa[i];
	} 
	//将每个字符串转成字符数组进行定位
	for(int i=0; i<n; i++){
		string a=aa[i];
		char c[aa[i].length()];
		//把字符串转换成字符指针,string转换成char* ,然后再将字符指针转换到字符数组C里面
		strcpy(c,a.c_str()); 
		//转换成二进制 
		transform_2(c,aa[i].length());
	} 
	return 0;
}

4. Hexadecimal to decimal

Input a positive hexadecimal string of no more than 8 digits from the keyboard, convert it to a positive decimal number and output it.
Note: 10~15 in the hexadecimal number are represented by uppercase English letters A, B, C, D, E, F respectively.

  • No more than eight hexadecimal digits, if the int type is used to store the answer, enter the sample FFFFFFFF , the answer is wrong, because the decimal number corresponding to FFFFFFFF is 4294967295 is greater than the range of int, so the answer needs to be used The data type of is long integer type.
#include<bits/stdc++.h>
using namespace std;
//转换为二进制 
void transform_2(char num[], int n){
	string Arry[n];
	string Arrx;
	for(int i=0; i<n; i++){
		//如果为数字 
		if(num[i]=='0'||num[i]=='1'||num[i]=='2'||num[i]=='3'||num[i]=='4'
		 ||num[i]=='5'||num[i]=='6'||num[i]=='7'||num[i]=='8'||num[i]=='9'){
		 	if(num[i]=='0' && i!=0) Arry[i]="0000";
		 	if(num[i]=='1') Arry[i]="0001";
		 	if(num[i]=='2') Arry[i]="0010";
		 	if(num[i]=='3') Arry[i]="0011";
		 	if(num[i]=='4') Arry[i]="0100";
		 	if(num[i]=='5') Arry[i]="0101";
		 	if(num[i]=='6') Arry[i]="0110";
		 	if(num[i]=='7') Arry[i]="0111";
		 	if(num[i]=='8') Arry[i]="1000";
		 	if(num[i]=='9') Arry[i]="1001";
		 } 
		else if(num[i]=='A' || num[i]=='a') Arry[i]="1010";
		else if(num[i]=='B' || num[i]=='b') Arry[i]="1011";
		else if(num[i]=='C' || num[i]=='c') Arry[i]="1100";
		else if(num[i]=='D' || num[i]=='d') Arry[i]="1101";
		else if(num[i]=='E' || num[i]=='e') Arry[i]="1110";
		else if(num[i]=='F' || num[i]=='f') Arry[i]="1111";	 
	}
	//字符串合并
	for(int i=0; i<n; i++) {
		Arrx += Arry[i];
	}
	int len=Arrx.length();
	long long res=0;        //重要说明,即注意事项
	for(int i=0; i<len;i++){
		if(Arrx[i]=='1'){
			res+=pow(2,len-1-i);
		}
	} 
	cout<<res;
}

int main(){
	string s;
	cin>>s;
	char c[s.length()];
	strcpy(c,s.c_str());
	transform_2(c,s.length());
	return 0;
}

5. Decimal to hexadecimal

Hexadecimal number is an integer representation that is often used in programming. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, a total of 16 symbols, respectively representing the decimal number 0 to 15. The hexadecimal counting method is full 16 into 1, so the decimal number 16 is 10 in hexadecimal, and the decimal 17 is 11 in hexadecimal, and so on, the decimal 30 is in hexadecimal It is 1E in the system.
Given a non-negative integer, represent it in hexadecimal form.

#include <iostream>
using namespace std;
int main()
{
    int a;
    cin>>a;
    int yu=a%16; //取余数 
    int shang=a/16;  //取商 
    char result[200];
    int count=0;

    while(shang!=0) //商大于等于0,也就是这个数比16大,17是‘11’(16进制)
    {
        switch(yu)
        {
            case 0: result[count]='0';break;
            case 1: result[count]='1';break;
            case 2: result[count]='2';break;
            case 3: result[count]='3';break;
            case 4: result[count]='4';break;
            case 5: result[count]='5';break;
            case 6: result[count]='6';break;
            case 7: result[count]='7';break;
            case 8: result[count]='8';break;
            case 9: result[count]='9';break;
            case 10: result[count]='A';break;
            case 11: result[count]='B';break;
            case 12: result[count]='C';break;
            case 13: result[count]='D';break;
            case 14: result[count]='E';break;
            case 15: result[count]='F';break;
        }
       
        count++;
        yu=shang%16;//余数
        shang=shang/16;
    }
    

    if(shang==0) 
    {
        switch(yu)
        {
            case 0: result[count]='0';break;
            case 1: result[count]='1';break;
            case 2: result[count]='2';break;
            case 3: result[count]='3';break;
            case 4: result[count]='4';break;
            case 5: result[count]='5';break;
            case 6: result[count]='6';break;
            case 7: result[count]='7';break;
            case 8: result[count]='8';break;
            case 9: result[count]='9';break;
            case 10: result[count]='A';break;
            case 11: result[count]='B';break;
            case 12: result[count]='C';break;
            case 13: result[count]='D';break;
            case 14: result[count]='E';break;
            case 15: result[count]='F';break;
        }
    }

    for(int i=count;i>=0;i--)
    {
        cout<<result[i];
    }
}

6. Special palindromes

123321 is a very special number that reads the same from the left as it reads from the right.
Input a positive integer n, program to find all such five-digit and six-digit decimal numbers, satisfying that the sum of each digit is equal to n.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
	cin>>n;
	//5位
	for(int a=1; a<=9;a++){
		for(int b=0; b<=9; b++){
			for(int c=0; c<=9; c++){
				if(a+b+c+b+a==n){
					cout<<a<<b<<c<<b<<a<<endl;
				} 									
			}
		}
	}
	//6位 
	for(int a=1; a<=9;a++){
		for(int b=0; b<=9; b++){
			for(int c=0; c<=9; c++){
				if(a+b+c+c+b+a==n){
					cout<<a<<b<<c<<c<<b<<a<<endl;
				} 									
			}
		}
	}
	return 0;
}

7. Palindrome number

1221 is a very special number, it reads the same from the left as it reads from the right, programming finds all such four-digit decimal numbers.

#include <bits/stdc++.h>
using namespace std;
int main()
{   
	for(int a=1; a<=9;a++){
		for(int b=0; b<=9; b++){
			if(a*1000+b*100+b*10+a<=9999){
				cout<<a<<b<<b<<a<<endl; 									
			}
		}
	}
	
	return 0;
}

8. Special numbers

 153 is a very special number, it is equal to the cube sum of its digits, that is, 153=1*1*1+5*5*5+3*3*3. Program to find all three-digit decimal numbers that satisfy this condition.

#include <bits/stdc++.h>
using namespace std;
int main()
{   
	for(int a=1; a<=9;a++){
		for(int b=0; b<=9; b++){
			for(int c=0; c<=9; c++){
				if(a*100+b*10+c==a*a*a+b*b*b+c*c*c){
					cout<<a*100+b*10+c<<endl;
				}
			}
		}
	}
	
	return 0;
}

9. Yanghui Triangle

Yang Hui's triangle is also called Pascal's triangle, and its i+1th row is the coefficient of the expansion of (a+b)i.

One of its important properties is that each number in a triangle is equal to the sum of the numbers on its two shoulders.

The first 4 rows of the Yang Hui triangle are given below:  

   1 

  1 1 

 1 2 1 

1 3 3 1

Given n, output its first n lines.

#include <bits/stdc++.h>
using namespace std;
int main()
{   
	int n;
	cin>>n;
	int lst[n][n+2];
	memset(lst, 0 ,sizeof lst); //memset的作用一般就是用来初始化数组例如用来将a数组全部归零
	lst[0][1]=1;
	
	for(int i=1; i<n; i++){
		for(int j=1; j<n+1; j++){
			lst[i][j]=lst[i-1][j]+lst[i-1][j-1]; 
		}
	}
	
	for(int  i=0; i<n; i++){
		for(int j=1; j<=i+1; j++){
			cout<<lst[i][j]<<" ";
		}
		cout<<endl;
	}
	
	return 0;
}

10. Find integers

 Given a sequence of n integers, what is the first occurrence of the integer a in the sequence.

#include <bits/stdc++.h>
using namespace std;
int main()
{   
	int n;
	cin>>n;
	int num[n];
	for(int i=0; i<n; i++){
		cin>>num[i];
	}
	int a;
	cin>>a;
	for(int i=0; i<n; i++){
		if(num[i]==a){
			cout<<i+1<<endl;
			break;
		}else if((num[i]!=a) && (i==n-1)){
			cout<<"-1"<<endl;
		}
	}
	
	
	return 0;
}

11. Sequence features

Given n numbers, find the maximum, minimum, and sum of these n numbers.

The use of sort function!

#include <bits/stdc++.h>
using namespace std;
int main()
{   
	int n;
	cin>>n;
	int num[n];
	for(int i=0; i<n; i++){
		cin>>num[i];
	}
	sort(num, num+n, greater<int>());
	cout<<num[0]<<endl;
	cout<<num[n-1]<<endl;
	int sum=0;
	for(int i=0; i<n; i++){
		sum=sum+num[i];
	}
	cout<<sum<<endl;
	
	
	return 0;
}

12. Alphabet graphics

Letters can be used to form some beautiful graphics, an example is given below:

ABCDEFG

BABCDEF

CBABCDE

DCBABCD

EDCBABC

This is a graph with 5 rows and 7 columns. Please find out the pattern of this graph and output a graph with n rows and m columns.

#include <bits/stdc++.h>
using namespace std;
int main()
{   
	int n,m;
	cin>>n>>m;
	string str1="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	for(int i=0; i<n; i++){
		string res="";
		//第一部分
		for(int j=i; j>i-m; j--) {
			if(j<0) break;
			res+=str1[j];
		}
		//第二部分
		for(int k=1; k<m-i;k++){
			res+=str1[k];
		}
		cout<<res<<endl;	 
	}
	return 0;

}

13.01 Strings

For a 01 string with a length of 5 bits, each bit may be 0 or 1, and there are 32 possibilities in total. The first few of them are:

00000

00001

00010

00011

00100

Please output these 32 kinds of 01 strings in ascending order.

It may be complicated, but it is actually converted to binary and then filled with 0!

#include <bits/stdc++.h>
using namespace std;
void transform_to_2(int x){
	int yushu=x%2; //余数
	int shang=x/2; //商
	char result[32];
	int count=0;
	
	while(shang!=0){
		switch(yushu){
			case 0: result[count]='0'; break;
			case 1: result[count]='1'; break;
		}
		count++;
		yushu=shang%2;
		shang=shang/2;
	} 
	
	if(shang==0){
		switch(yushu){
			case 0: result[count]='0'; break;
			case 1: result[count]='1'; break;
		}
	} 

	int n=count;
		if(n==0)  cout<<"0000";
		if(n==1)  cout<<"000";
		else if(n==2)  cout<<"00";
		else if(n==3)  cout<<"0";

		
	for(int i=count; i>=0; i--)
	{
		
		cout<<result[i];
	}
	
}
int main()
{   
	for(int i=0; i<32; i++){
		transform_to_2(i);
		 cout<<endl;
	}
	return 0;

}

14. Leap year judgment

Leap year formula: Leap every four years, no leap for a hundred years, and another leap for four hundred years!

#include <bits/stdc++.h>
using namespace std;

int main()
{   
	int y;
	cin>>y;
	if((y%400==0)||(y%4==0&&y%100!=0)){
		cout<<"yes";
	}else{cout<<"no";}
	return 0;

}

15.Fibonacci sequence

The recursive formula of the Fibonacci sequence is: Fn=Fn-1+Fn-2, where F1=F2=1.

When n is relatively large, Fn is also very large. Now we want to know what is the remainder of dividing Fn by 10007.

Find the remainder separately, add up and find the remainder!

#include <bits/stdc++.h>
using namespace std;

int main()
{   
	long long n;
	cin>>n;
	long long a[n];
	a[0]=1;
	a[1]=1;
	for(int i=2; i<n; i++){
		a[i]=a[i-1]%10007+a[i-2]%10007;
//		a[i]=a[i]%10007;
//		cout<<a[i]<<endl;
	}

	cout<< a[n-1]%10007 <<endl;
	return 0;

}

16. Area of ​​a circle

Given the radius r of a circle, find the area of ​​the circle.

The representation of PI! A calculator can be used to assist!

#include <bits/stdc++.h>
using namespace std;
//const double PI=3.14159265358979323846;  //要取更精确的值,否则数据大时计算结果有误
int main()
{   
	int r;
	cin>>r;
	double PI=atan(1.0)*4;
	double s=PI*r*r;
	cout << fixed << setprecision(7);
	cout<<s;
//	printf("%.7f", s);
	return 0;

}

17. Sequence summation

Find the value of 1+2+3+...+n.

  • (first item + last item) * number of items/2
#include <bits/stdc++.h>
using namespace std;
int main()
{   
	long long n;
	cin>>n;
	long long sum=0;
	sum = (1+n)*n/2;
	cout<<sum;
	return 0;

}

18. Factorial calculation

C++ Blue Bridge Cup Basic Exercise Factorial Calculation

19. High precision addition

C++ Blue Bridge Cup basic practice high-precision addition

Replenish:

  • int x=(a[alen-1-i])-'0';
  • a[alen-1-i] means starting from the last element of the array a and counting forward to the i-th element.
  • (a[alen-1-i])-'0' means converting the character numbers in the array a to integer numbers. This is because in the computer, character numbers are stored according to the ASCII code, and the ASCII code of '0' is 48, so the corresponding integer can be obtained by subtracting the ASCII code of '0' from the character number number.

20. Huffman tree

 C++ Blue Bridge Cup Basic Practice Huffman Tree

21.2n queen problem

C++ Blue Bridge Cup Basic Exercise 2n Queen

22. Timekeeping assistant

C++ Blue Bridge Cup basic practice time assistant

23. Retrieval of numbers

C++ Lanqiao Cup Basic Exercises: Returning Numbers

24. Prediction of the tortoise and the hare

C++ Lanqiao Cup Basic Exercise Prediction of the Tortoise and the Hare

25. Chip test

C++ Blue Bridge Cup Basic Practice Chip Test

26. String of FJ

C++ Blue Bridge Cup basic practice FJ string

27. Dance of Sine

C++ Blue Bridge Cup Basic Practice Sine Dance

28. How to read numbers

C++ Blue Bridge Cup Basic Practice Sine Dance

29. The price of perfection

The perfect price of C++ Blue Bridge Cup basic exercises

30. Rectangular area intersection

Rectangular Area Intersection of C++ Blue Bridge Cup Basic Practice

31. Matrix multiplication

C++ Blue Bridge Cup basic practice matrix multiplication

32. Decomposing prime factors

C++ Blue Bridge Cup Basic Exercise Decomposition Quality Factor

33. String comparison

C++ Lanqiao Cup basic practice string comparison

34. Time Shift 

C++ Blue Bridge Cup basic practice time conversion 

Guess you like

Origin blog.csdn.net/MengYa_Dream/article/details/129541278