



🌈 一、故事:小杨在画方格纸
1、想象有一张 n × n 的方格纸,小杨从:
第1行画到第n行
每行从第1列画到第n列
2、就像走格子:
cpp
(1,1) (1,2) (1,3) ...
(2,1) (2,2) (2,3) ...
...
每一个格子,我们决定画什么字符。
🎯 二、H字的规则(关键)
1、对于任意位置:
设:
cpp
行 = i
列 = j
我们判断:
2、 情况1:最左列
如果:
cpp
j == 1
输出:
cpp
|
3、情况2:最右列
如果:
cpp
j == n
输出:
cpp
|
4、情况3:中间行
中间行是:
cpp
i == (n+1)/2
输出:
cpp
-
5、 情况4:其他地方
输出:
cpp
a
🧠 三、完整逻辑图
cpp
如果 列==1 或 列==n → |
否则如果 行是中间行 → -
否则 → a
💻 四、参考程序:
cpp
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++) // 控制行
{
for(int j = 1; j <= n; j++) // 控制列
{
if(j == 1 || j == n)
{
cout << "|";
}
else if(i == (n+1)/2)
{
cout << "-";
}
else
{
cout << "a";
}
}
cout << endl;
}
return 0;
}
🌈 五、这是图形打印的万能模板
cpp
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(条件1)
输出A;
else if(条件2)
输出B;
else
输出C;
}
cout<<endl;
}
🎯 六、记忆口诀
先画行,再画列
每个格子单独判
左右两边是竖线
中间一行是横线
🎁 七、《GESP二级 图形打印万能模板(坐标法 )》
1、🌈 什么是"坐标法"?
(1)想象有一张棋盘 ♟:
例如 n = 5:
cpp
(1,1) (1,2) (1,3) (1,4) (1,5)
(2,1) (2,2) (2,3) (2,4) (2,5)
(3,1) (3,2) (3,3) (3,4) (3,5)
(4,1) (4,2) (4,3) (4,4) (4,5)
(5,1) (5,2) (5,3) (5,4) (5,5)
(2)每个位置都有:
cpp
行号 row
列号 col
我们只需要:
👉 判断这个位置应该输出什么字符
2、🎯 坐标法万能模板
cpp
for(int row=1; row<=n; row++)
{
for(int col=1; col<=n; col++)
{
if(条件)
cout<<"字符";
else
cout<<"字符";
}
cout<<endl;
}
🧩 图形1:实心正方形
1、输入:
cpp
5
2、输出:
cpp
*****
*****
*****
*****
*****
3、规律
每个位置都输出 *
4、程序
cpp
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
cout<<"*";
}
cout<<endl;
}
}
🧩 图形2:空心正方形
cpp
*****
* *
* *
* *
*****
1、规律
边界输出 *
否则空格
2、条件:
cpp
row==1
row==n
col==1
col==n
3、程序:
cpp
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
if(row==1 || row==n || col==1 || col==n)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
🧩 图形3:左上直角三角形
cpp
*
**
***
****
*****
1、规律:
cpp
col <= row
2、程序:
cpp
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
if(col<=row)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
🧩 图形4:右上直角三角形
cpp
*
**
***
****
*****
1、规律:
cpp
col >= n-row+1
2、程序:
cpp
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
if(col>=n-row+1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
🧩 图形5:左下三角形
cpp
*****
****
***
**
*
1、规律:
cpp
col <= n-row+1
2、程序:
cpp
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
if(col<=n-row+1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
🧩 图形6:右下三角形
cpp
*****
****
***
**
*
1、规律:
cpp
col >= row
2、程序:
cpp
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
if(col>=row)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
🧩 图形7:对角线
cpp
*
*
*
*
*
1、规律:
cpp
row == col
2、程序:
cpp
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
if(row==col)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
🧩 图形8:X图形 ⭐GESP常考
cpp
* *
* *
*
* *
* *
1、规律:
cpp
row==col
或
row+col==n+1
2、程序:
cpp
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
if(row==col || row+col==n+1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
🧩 图形9:十字
cpp
*
*
*****
*
*
1、规律:
cpp
row==mid
或
col==mid
2、程序:
cpp
int mid=(n+1)/2;
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
if(row==mid || col==mid)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
🌟 重要总结
| 判断类型 | 条件 |
|---|---|
| 边框 | row==1 row==n col==1 col==n |
| 左三角 | col<=row |
| 右三角 | col>=n-row+1 |
| 对角线 | row==col |
| 反对角线 | row+col==n+1 |
| 中间行 | row==mid |
| 中间列 | col==mid |
🎯 万能模板
cpp
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int mid=(n+1)/2;
for(int row=1;row<=n;row++)
{
for(int col=1;col<=n;col++)
{
if(条件1)
cout<<"字符";
else if(条件2)
cout<<"字符";
else
cout<<"字符";
}
cout<<endl;
}
}