C++ Simulation Algorithm Practical Exercises 01

All problems can be searched in the Luogu problem set!

B3844 Draw a Square

Problem Description

Input a positive integer n, and output a square pattern with n rows and n columns (refer to the sample input and output). The pattern consists of uppercase letters.

The first row starts with the uppercase letter A, the second row starts with the uppercase letter B, and so on; in each row, the second column is the next letter after the first column, the third column is the next letter after the second column, and so on; specifically, the next letter after uppercase Z is uppercase A.

Input Format

Input a line containing a positive integer n. It is stipulated that 2≤n≤40.

Output Format

Output the required square pattern.

Input Sample 01: 3 Output Sample 01: ABC

BCD

CDE Input Sample 02: 5 Output Sample 02: ABCDE

BCDEF

CDEFG

DEFGH

EFGHI

// B3844 Draw a Square
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n; // n rows and n columns matrix
  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){ // n rows and n columns nested loop 
      // Calculate the current letter, A + i row
      // The subsequent ones follow the accumulated i + j
      char c = 'A' + (i + j) % 26;
      cout<<c<<" "; 
    }
    cout<<endl;
  } 
    return 0;
}

B3845 Pythagorean Triples Problem Description

Pythagorean triples are an interesting mathematical concept. If three positive integers a, b, c satisfy a^2 + b^2 = c^2, and 1≤a≤b≤c, we call the triplet (a, b, c) a Pythagorean triple. Can you count how many sets of Pythagorean triples satisfy c≤n through programming?

Input Format

Input a line containing a positive integer n. It is stipulated that 1≤n≤1000.

Output Format

Output a line containing an integer C, representing the number of sets of Pythagorean triples that satisfy the condition. Input Sample 01: 5 Output Sample 01: 1 Input Sample 02: 13 Output Sample 02: 3 Explanation/Hint

【Sample Explanation 1】

There is only one Pythagorean triple (3, 4, 5) that satisfies c≤5.

【Sample Explanation 2】

There are 3 Pythagorean triples that satisfy c≤13, namely (3, 4, 5), (6, 8, 10), and (5, 12, 13).

// B3845 Pythagorean Triples 
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  int sum = 0; 
  cin>>n; // indicates the range
  // Check all combinations that satisfy the Pythagorean triples between 1 - n
  for(int a = 1; a <= n; a++){
   // Iterate possible values of a, minimum is 1, maximum is n,
    for(int b = a; b <= n; b++){
      // Iterate values of b
      for(int c = b; c<=n; c++){
        if(a*a + b*b == c*c){ // Pythagorean triple 
          sum++; // Satisfies the condition 
        }
      } 
    } 
  } 
  cout<<sum<<endl;
  return 0; 
}

B3836 Hundred Chickens Problem Problem Description

“Hundred Chickens Problem” is a famous mathematical problem from ancient China’s “Zhang Qiujian’s Calculation Classic”. The essence is:

“Each rooster costs 5 yuan, each hen costs 3 yuan, and every 3 chicks cost 1 yuan; now with 100 yuan, how many ways can you buy 100 chickens?”

Xiao Ming really likes this story, and he decided to expand this problem and solve it using programming:If each rooster costs x yuan, each hen costs y yuan, every z chicks cost 1 yuan; now with n yuan, how many ways can you buy m chickens?

Input Format

Input a line containing five integers, corresponding to x, y, z, n, m in the problem description. It is stipulated that 1≤x,y,z≤10, 1≤n,m≤1000.

Output Format

Output a line containing an integer C, representing the number of solutions.

Input Sample 01:5 3 3 100 100 Output Sample 01:4 Input Sample 02:1 1 1 100 100 Output Sample 02:5151 Explanation/Hint

【Sample 1 Explanation】

This is the “Hundred Chickens Problem” described in the problem. The 4 solutions are:

0 roosters, 25 hens, and 75 chicks.

4 roosters, 18 hens, and 78 chicks.

8 roosters, 11 hens, and 81 chicks.

12 roosters, 4 hens, and 84 chicks.

// B3836 Hundred Chickens Problem
#include<bits/stdc++.h>
using namespace std;
int main(){
  int x,y,z,n,m; // x roosters, y hens, z chicks, n money, m quantity
  cin>>x>>y>>z>>n>>m; // Input
  int sum = 0; // Accumulate solutions
  for(int i=0; i<=m; i++){ // Number of roosters 
    for(int j=0; j<=m-i; j++){ // Number of hens is total quantity - number of roosters 
      int k = m - i - j; // Number of chicks
      if(k % z == 0 && x*i + y*j +k/z == n){
            // Chick quantity satisfies and all three satisfy total quantity
        sum++; // Accumulate 
      } 
    } 
  } 
  cout<<sum<<endl;
  return 0; 
}

B3837 Draw a Triangle Problem Description

Input a positive integer n, and use uppercase letters to form a triangle pattern (refer to the sample input and output): the first row of the triangle pattern has 1 letter, the second row has 2 letters, and so on; in the triangle pattern, the letters A-Z are filled from top to bottom, left to right, and after filling with uppercase letter Z, it will start again with uppercase letter A.

Input Format

Input a line containing a positive integer n. It is stipulated that 2≤n≤40.

Output Format

Output the required triangle pattern. Note that there should be no extra spaces on the right side of each row of the triangle pattern.

Input Sample 01: 3 Output Sample 01: A

BC

DEF Input Sample 02: 7 Output Sample 02: A

BC

DEF

GHIJ

KLMNO

PQRSTU

VWXYZAB

// B3837 Draw Triangle 01
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n; 
  // Concept of Pascal's Triangle
  cin>>n; // n rows of the triangle
  int x = 65; // (uppercase A --- 65)
  for(int i=1; i<=n; i++){
    for(int j=0; j<i; j++){
// One in the first row, two in the second row....
      cout<<(char)x; 
      x++; // Increment once for each inner loop iteration, effectively moving forward once 
      // After Z, traverse back to A
      if(x > 90){
        x = 65; // If the ASCII value exceeds 90, reset to start from A 
      } 
    }
    cout<<endl; // Outer loop line break 
  } 
  return 0; 
}
// B3837 Draw Triangle 02
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  int sum = 0;
  for(int i=1; i<=n; i++){
    // The first row i has the first letter A ---- ASCII--65
    int num = 65+sum; 
    for(int j=0; j<i; j++){
      // Calculate the ASCII code of the letter to be output. If it exceeds 90 (Z), loop back to 65 (A)
      char a = (num + j - 65) % 26 + 65;
      // char a = ('A' + j - 'A') % 26 + 'A' 
      cout<<a;
    }
    cout<<endl;
    sum += i; // Update the total number of letters in the previous i rows 
  }
  return 0;
}

B3840 Find Prime Numbers Problem Description

Xiao Ming has just learned the concept of prime numbers:If a positive integer greater than 1 cannot be divided by any other positive integers except for 1 and itself, then this positive integer is a prime number.Now, Xiao Ming wants to find how many prime numbers are there between two positive integers A and B (including A and B).

Input Format

Input only one line with two positive integers A, B. It is stipulated that 2≤A≤B≤1000.

Output Format

Output a line containing an integer C, representing the number of prime numbers found. Input Sample 01: 2 10 Output Sample 01: 4 Input Sample 02: 98 100 Output Sample 02: 0 Explanation/Hint

【Sample Explanation 1】There are 4 prime numbers between 2 and 10, namely: 2, 3, 5, and 7.

// B3840 Find Prime Numbers
#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,b,sum=0;
  cin>>a>>b; // Check prime numbers between a-b (including A, B)
  for(int n = a; n<=b; n++){ // Iterate all numbers in the range 
    bool isPrime = true; // Boolean type for judgment
    for(int i=2; i*i<=n; i++){ // Check for prime numbers 
      if(n % i == 0){ // Not a prime number 
        isPrime = false; // Not
        break; 
      } 
    }
    if(isPrime == 1){
      sum++; // After the inner loop ends, if isPrime remains unchanged, then this number is a prime number 
    } 
  } 
  cout<<sum<<<endl;
  return 0; 
}

B3841 Self-Power Number Judgment Problem Description

A self-power number is defined as an N-digit number, where the sum of each digit raised to the Nth power equals the number itself. For example, 153 is a 3-digit number, and the sum of its digits raised to the 3rd power is 1^3 + 5^3 + 3^3 = 153, so 153 is a self-power number; 1634 is a 4-digit number, and the sum of its digits raised to the 4th power is 1^4 + 6^4 + 3^4 + 4^4 = 1634, so 1634 is a self-power number. Now, input several positive integers and determine whether they are self-power numbers.

Input Format

The first line of input is a positive integer M, indicating that there are M positive integers to be judged. It is stipulated that 1≤M≤100.

From the second line onwards, M lines, each containing a positive integer to be judged. It is stipulated that these positive integers are all less than 10^8.

Output Format

Output M lines; if the corresponding positive integer to be judged is a self-power number, output the uppercase letter T, otherwise output the uppercase letter F.Hint: You do not need to wait until all inputs are finished to output; you can judge one number and output it, then input the next number.

Input Sample 01: 3

152

111

153 Output Sample 01: F

F

T Input Sample 02: 5

8208

548834

88593477

12345

5432 Output Sample 02: T

T

T

F

B3865 Xiao Yang’s X MatrixProblem Description

Xiao Yang wants to construct an X matrix (N is odd), where both diagonals are half-width plus signs +, and the rest are half-width minus signs -. For example, a 5×5 X matrix is as follows:

+---+
-+-+-
--+--
-+-+-
+---+

Please help Xiao Yang print the corresponding “X matrix” based on the given N.

Input Format

A line with an integer N (5≤N≤49, guaranteed to be odd). Output Format

Output the corresponding “X matrix”.

Please strictly follow the format requirements for output, do not add any extra spaces, punctuation, blank lines, or any symbols. You should output exactly N lines, each line containing exactly N characters, which are either + or -.

Input Sample 01: 5 Output Sample 01: +—+

-+-+-

–+–

-+-+-

+—+ Input Sample 02: 7 Output Sample 02:

+—–+

-+—+-

–+-+–

—+—

–+-+–

-+—+-

+—–+

// Xiao Yang's X Matrix
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++){
      if(i == j || j+i == n+1){
        cout<<"+";
      }else{
        cout<<"-";
      }
    }
    cout<<endl;
  }
  return 0;
}

B3866 Number Black HoleProblem Description

Given a three-digit number, the digits cannot be the same. For example, 352 is valid, while 112 is not. Rearranging the three digits of this three-digit number to get the largest number, subtracting the smallest number from it, forms a new three-digit number. The above process can be repeated. Amazingly, it will eventually yield 495!

For instance, rearranging 352 gives the largest number 532, the smallest number 235, and their difference is 297; transforming 297 gives 972−279=693; transforming 693 gives 963−369=594; transforming 594 gives 954−459=495. Thus, after 4 transformations, we arrive at 495.

Now, given the three-digit number, can you determine how many transformations it takes to reach 495 through programming?

Input Format

Input a line containing a valid three-digit number N.

Output Format

Output a line containing an integer C, representing the number of transformations taken to reach 495.

Input Sample: 352 Output Sample: 4

// B3866 Number Black Hole
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  for(int t=0; ; t++){ 
    if(n == 495){
      cout<<t<<endl;
      break;
    }
    int a=n%10, b=n/10%10, c=n/100; // Units, tens, hundreds
    int maxx = 0, minn = 0; // Maximum, minimum
    if(a >= b && b >= c){
      maxx = a*100 + b*10 + c;
      minn = c*100 + b*10 + a;
    }else if(a >= c && c >= b){
      maxx = a*100 + c*10 + b;
      minn = b*100 + c*10 + a; 
    }else if(b >=a && a >=c){
      maxx = b*100 + a*10 + c;
      minn = c*100 + a*10 + b;
    }else if(b >= c && c >=a){
      maxx = b*100 + c*10 + a;
      minn = a*100 + c*10 + b;
    }else if(c >=a && a >=b){
      maxx = c*100 + a*10 + b;
      minn = b*100 + a*10 + c; 
    }else{
      maxx = c*100 + b*10 + a;
      minn = a*100 + b*10 + c; 
    }
    n = maxx - minn; 
  }
  return 0;
}

B3923 [GESP202312 Level 2] Xiao Yang’s Problem SolvingProblem Description

To prepare for the exam, Xiao Yang does problems every day. On the first day, Xiao Yang did a problems; on the second day, he did b problems; starting from the third day, the number of problems Xiao Yang does each day is the sum of the previous two days.

In addition, Xiao Yang also stipulates that when he does m or more problems on a certain day, he will stop doing problems for the following days.

How many problems has Xiao Yang done in total by the Nth day?

Input Format

A total of 4 lines. The first line is an integer a, the second line is an integer b, the third line is an integer m, and the fourth line is an integer N.

It is guaranteed that 0≤a,b≤10; a,b < m < 1,000,000; 3≤N≤364.

Output Format

One line with an integer, indicating how many problems Xiao Yang has done in total by the Nth day.

Input Sample 01: 1

2

10

5 Output Sample 01: 19 Input Sample 02: 1

1

5 8

Output Sample 02: 12

Explanation/Hint

Sample Explanation 1

Xiao Yang did 1 problem on the first day, 2 problems on the second day, 3 problems on the third day, 5 problems on the fourth day, and 8 problems on the fifth day. Therefore, he has done a total of 1+2+3+5+8=19 problems.

Sample Explanation 2

Xiao Yang did 1, 1, 2, 3, 5 problems in the first 5 days, and since he did 5 problems on the 5th day, and m=5, he will stop doing problems thereafter. Therefore, Xiao Yang has done a total of 1+1+2+3+5=12 problems.

#include &lt;stdio.h&gt;
int main(int argc, char **argv)
{
    long long a=0, b=0, m=0, n=0, ans=0, c=0, i;
    scanf("%lld%lld%lld%lld", &amp;a, &amp;b, &amp;m, &amp;n);
    ans = a+b;
    for(i=3; i<=n; i++){
        c = a+b;
        ans += c;
        a = b;
        b = c;
        if(c>=m) break;
}
    printf("%lld\n", ans);
    return 0;
}

Practice more, think more!!

Many parents or institutions want similar materials and documentscan scan the code to join the group, as a communication platform for programming learning(Please note your information, advertising is prohibited)C++ Simulation Algorithm Practical Exercises 01

Leave a Comment