| → |
||||||||||||||||||||||||||
| ↓ |
|
总结了一下关于打印菱形的思路。
通常是从循环变量之间的映射关系入手,推导出相应的公式。这种思路的源点,往往会将坐标轴的原点放在左上方,也就是在[2N + 1]的矩形内打印出内嵌的菱形。如下图所示,横向[row]的取值范围[0, 2N+1),纵向[col]的取值范围[0, 2N + 1),变量[N]表示要打印菱形对角线长的1/2。
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
| 0 | * | ||||||
| 1 | * | * | * | ||||
| 2 | * | * | * | * | * | ||
| 3 | * | * | * | * | * | * | * |
| 4 | * | * | * | * | * | ||
| 5 | * | * | * | ||||
| 6 | * |
照此思路有如下几种解法:
解法一:将菱形从中间分开,可以看到[输出空格数 + 星号数 = N](*从零计数)。那么可以将空格输出和星号输出分别进行。
[空格输出]的控制变量由[col]完成。对应的输出条件[col < abs(row - N)]。
[星号输出]的控制变量亦由[col]完成。对应的输出条件[col < (2 * (N - abs(row - N)) + 1]。
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
| 0 | * | ||||||
| 1 | * | * | * | ||||
| 2 | * | * | * | * | * | ||
| 3 | * | * | * | * | * | * | * |
| 4 | * | * | * | * | * | ||
| 5 | * | * | * | ||||
| 6 | * |
★ 代码如下:
/*
这里将菱形从中切开,比较容易寻找到规律。
*/
#include <stdio.h>
#include <math.h>
int main(void)
{
int count, row, col;
printf("Number of rows = ?\n");
scanf("%d", &count);
for (row = 0; row < (count * 2 + 1); row++) {
// 控制空格的输出
for (col = 0; col < abs(row - count); col++) {
printf(" ");
}
// 控制 * 号的输出
for (col = 0; col < (2 * (count - abs(row - count)) + 1); col++) {
printf("*");
}
printf("\n");
}
return 0;
}
解法二:认为是在一个[2N + 1]的矩形画布上输出菱形。鉴于菱形的对称特性,利用坐标之间的不等式关系,可以找到每一个星号的可能的输出范围。
那下图为例,
[红色*坐标] [row, col] = [0, 3] ==> (row + col) = 3
[蓝色*坐标] [row, col] = [6, 3] ==> (row + col) = 9
由此可知 (row + col) ∈ [N, 3 * N]
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
| 0 | * | ||||||
| 1 | * | * | * | ||||
| 2 | * | * | * | * | * | ||
| 3 | * | * | * | * | * | * | * |
| 4 | * | * | * | * | * | ||
| 5 | * | * | * | ||||
| 6 | * |
但对于两个变量[row]和[col]而言,显然一个条件式并不能够正确的定位,现在需要构建另一个条件式。
仍以上图为例,可以得到对应的条件式
[红色*坐标] [row, col] = [0, 3] ==> (row - col) = -3
[蓝色*坐标] [row, col] = [6, 3] ==> (row - col) = 3
由上可以推得 (row - col) ∈ [-N, N]
到这里两个控制变量被限制在了两个条件式中,这时就可以正确的定位到每一个星号的位置了。
★ 代码如下:
#include <stdio.h>
int main(void)
{
int row, col, count;
printf("Number of rows = ?\n");
scanf("%d", &count);
for (row = 0; row < (2 * count + 1); row++) {
for (col = 0; col < (2 * count + 1); col++) {
// 这里要求输出 * 的位置满足[count <= (row + col) <= 3 * count]&&[-count <= (row - col) <= count]
if (count <= (row + col) && (row + col) <= 3 * count && -count <= (row - col) && (row - col) <= count) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
以上两种解法默认坐标系在左上角,实际可以平移坐标系,使得横纵坐标之间的关系能够更好的表达。如下图
| -3 | -2 | -1 | 0 | 1 | 2 | 3 | |
| -3 | * | ||||||
| -2 | * | * | * | ||||
| -1 | * | * | * | * | * | ||
| 0 | * | * | * | * | * | * | * |
| 1 | * | * | * | * | * | ||
| 2 | * | * | * | ||||
| 3 | * |
解法三:观察在新的坐标系中,每个星号所在的横纵坐标之间的关系可以表示为[row + col <= N]
[红色*坐标] [row, col] = [0, -3] ==> (row + col) = -3
[蓝色*坐标] [row, col] = [3, 0] ==> (row + col) = 3
由此可知 (abs(row) + abs(col)) ∈ [0, N]
以上两种解法默认坐标系在左上角,实际可以平移坐标系,使得横纵坐标之间的数值关系能够更好的表示。如下图
| -3 | -2 | -1 | 0 | 1 | 2 | 3 | |
| -3 | * | ||||||
| -2 | * | * | * | ||||
| -1 | * | * | * | * | * | ||
| 0 | * | * | * | * | * | * | * |
| 1 | * | * | * | * | * | ||
| 2 | * | * | * | ||||
| 3 | * |
★ 代码如下:
/**
灵活运用坐标系可以简化程式。
**/
#include <stdio.h>
#include <math.h>
int main(void)
{
int row, col, count;
printf("Number of row count = ?\n");
scanf("%d", &count);
for (row = -count; row <= count; row++) {
for (col = -count; col <= count; col++) {
if (abs(row) + abs(col) <= count) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
下面的方法充分利用了[printf函数]本身提供的功能,可以实现极其精简的代码。
解法四:采用通常默认的坐标方式来表示变量之间的关系。
| row | 输出宽度 | 输出宽度函数关系 | 星号个数 | 模板星号 | 需要删除星号个数 | 删除星号个数函数关系 |
| 0 | 5 | row + N + 1 | 1 | ********* | 8 | 2 * N - 2 * row |
| 1 | 6 | 3 | 6 | |||
| 2 | 7 | 5 | 4 | |||
| 3 | 8 | 7 | 2 | |||
| 4 | 9 | 9 | 0 | |||
| 5 | 8 | (3 * N + 1) - row | 7 | 2 | 2 * row - 2 * N | |
| 6 | 7 | 5 | 4 | |||
| 7 | 6 | 3 | 6 | |||
| 8 | 5 | 1 | 8 |
对应的关系列出后,就很容写出对应的代码了。
★ 代码如下:
#include <stdio.h>
int main(void)
{
int row, col, count;
int i = 0;
char a[100];
printf("Number of rows = ?\n");
scanf("%d", &count);
for (; i != (2 * count + 1); i++) {
a[i] = '*';
}
row = 0;
for (; row != (2 * count + 1); row++) {
printf("%*s\n", (row < (count + 1)?(row + count + 1):((3 * count + 1) - row)), a + (row < (count + 1)?(2 * count - 2 * row):(2 * row - 2 * count)));
}
}
解法五:采用坐标轴平移后的方式来表示变量之间的关系。
| row | 输出宽度 | 输出宽度函数 | 星号个数 | 星号模板 | 要删除的星号个数 | 删除星号个数函数关系 |
|
| -4 | 5 | (2 * N + 1) - abs(row) | 1 | (2 * N + 1) - abs(row) - abs(row) | ********* | 8 | 2 * abs(row) |
| -3 | 6 | 3 | 6 | ||||
| -2 | 7 | 5 | 4 | ||||
| -1 | 8 | 7 | 2 | ||||
| 0 | 9 | 9 | 0 | ||||
| 1 | 8 | 7 | 2 | ||||
| 2 | 7 | 5 | 4 | ||||
| 3 | 6 | 3 | 6 | ||||
| 4 | 5 | 1 | 8 |
依上表对应关系,经过坐标平移后的对应关系更加简洁,代码量更小。
★ 代码如下:
#include <stdio.h>
#include <math.h>
int
main(void)
{
int row, col, count;
int i;
char p[100];
printf("Number of rows = ?\n");
scanf("%d", &count);
i = 0;
for (; i != (2 * count + 1); i++) {
p[i] = '*';
}
row = -count;
for (; row <= count; row++) {
printf("%*s\n", ((2 * count + 1) - abs(row)), p + (2 * abs(row)));
}
return 0;
}
★ 以上代码均在 Ubuntu 10.04 下编译通过。
浙公网安备 33010602011771号