1196 – Corner I

1. Problem Understanding Phase (Minimum Number Matrix Problem)
We need to print a special N×N matrix characterized by:
-
The value of each element in the matrix equals the smaller of its row index i and column index j
-
Using nested loops to traverse the matrix
-
Each number output occupies a width of 3 characters
2. Core Algorithm Idea
The program adopts the method of double loops:
-
The outer loop controls the row index i from 1 to N
-
The inner loop controls the column index j from 1 to N
-
Using min(i,j) to calculate the value at each position
-
Using setw(3) to control the output format
3. Detailed Execution Process (Example with N=3)
Output Matrix:
1 1 1
1 2 2
1 2 3
Reference Code
#include<bits/stdc++.h> // Include all standard library headers
using namespace std; // Use standard namespace
int main() { int n; // Define matrix size variable
cin >> n; // Read user input for matrix size
// Outer loop: control row index i from 1 to n for(int i = 1; i <= n; i++) { // Inner loop: control column index j from 1 to n for(int j = 1; j <= n; j++) { // Output the smaller value of i and j, width set to 3 characters cout << setw(3) << min(i, j); } // New line after each row output cout << endl; }
return 0; // Program ends normally
}
Super Detailed Program Documentation (For Beginners)
1. Program Function Description
This program implements the following functions:
-
Input: A positive integer N (matrix size)
-
Output: An N×N matrix where each element’s value is the smaller of the row and column indices
-
Output format: Each number occupies a width of 3 characters, right-aligned
-
Example: When input is 3, the output is the matrix shown above
2. Core Algorithm Analysis
Double Loop Structure:
-
Outer Loop:
-
Control variable i (row index) increments from 1 to N
-
Each loop iteration processes one row of the matrix
Inner Loop:
-
Control variable j (column index) increments from 1 to N
-
Calculate and output min(i,j)
Key Functions:
-
min(i,j): Returns the smaller of the two numbers
-
setw(3): Sets the output field width to 3
3. Variable Role Description
-
<span>n</span>: Stores the user input for matrix size -
<span>i</span>: Outer loop variable, representing the current row index -
<span>j</span>: Inner loop variable, representing the current column index
4. Execution Process Example (N=3)
-
i=1:
-
j=1: Output 1
-
j=2: Output 1
-
j=3: Output 1
-
New line
i=2:
-
j=1: Output 1
-
j=2: Output 2
-
j=3: Output 2
-
New line
i=3:
-
j=1: Output 1
-
j=2: Output 2
-
j=3: Output 3
-
New line
5. Loop Execution Diagram
Outer loop i=1→3
│
├── Inner loop j=1→3
│ ├── Output min(1,1)=1
│ ├── Output min(1,2)=1
│ └── Output min(1,3)=1
│
├── Inner loop j=1→3
│ ├── Output min(2,1)=1
│ ├── Output min(2,2)=2
│ └── Output min(2,3)=2
│
└── Inner loop j=1→3
├── Output min(3,1)=1
├── Output min(3,2)=2
└── Output min(3,3)=3
6. Important Notes
-
Format Control:
-
setw(3) ensures each number occupies 3 characters
-
Default right alignment
Boundary Handling:
-
Loop variables start from 1
-
Includes boundary value n
Input Limitations:
-
The program does not check the range of input
-
In practical use, n should not be too large
7. Reference Test Cases
|
Input N |
Output Matrix |
|---|---|
|
1 |
1 |
|
2 |
1 11 2 |
|
3 |
1 1 11 2 21 2 3 |
|
4 |
1 1 1 11 2 2 21 2 3 31 2 3 4 |
This explanation is entirely based on the original code, providing a detailed analysis of the execution process and visualizing the call relationships to help beginners deeply understand the implementation principles of double loops printing matrices.