如何使用 C 仅使用 for/while 循环和 if/else 语句(无数组)绘制 # 的螺旋
How to draw a spiral of # with only for/while loops and if/else statements (without arrays) using C
所以我是 C 的新手,想知道如何创建以下螺旋形状:
这是我的滑稽尝试..
#include <stdio.h>
int main(void) {
int size;
int i,j;
printf("Enter size: ");
scanf("%d", &size);
for (i = 0, j = 0; i < size - 4; i++, j+=2) {
int row = 1;
while (row <= size) {
int column = 1;
while (column <= size) {
if ((row == 1 + j && column > j && column < size - j)||
(row == size - (j) && column > j && column < size - j) ||
(column == size - (j) && row > j && row <= size - j) ||
(column == 1 + j && row > (2+j) && row < size - j)) {
printf("*");
}
else {
printf("-");
}
column++;
}
row++;
printf("\n");
}
}
return 0;
}
如有任何帮助,我们将不胜感激!
我很喜欢这个问题,运行 遇到了一些想出方法的麻烦。不过,足够多的混乱使模式变得明显;这是结果:
#include <stdio.h>
#define BLANK " "
#define FILLED "# "
void spiral(int size) {
int i, j;
if (!(size & 1)) { size++; }
int half = size / 2 + 1;
/* top */
for (i = 0; i < half; i++) {
if (i & 1) {
for (j = 0; j < size; j++) {
if (!(j & 1) && (j < i - 1 || j >= size - i)) {
printf(FILLED);
}
else {
printf(BLANK);
}
}
}
else {
for (j = 0; j < size; j++) {
if ((j & 1) && (j < i - 1 || j >= size - i)) {
printf(BLANK);
}
else {
printf(FILLED);
}
}
}
puts("");
}
/* bottom half */
for (; i < size; i++) {
if (i & 1) {
for (j = 0; j < size; j++) {
if (!(j & 1) && (j < size - i || j > i)) {
printf(FILLED);
}
else {
printf(BLANK);
}
}
}
else {
for (j = 0; j < size; j++) {
if ((j & 1) && (j < size - i || j > i)) {
printf(BLANK);
}
else {
printf(FILLED);
}
}
}
puts("");
}
}
int main() {
int i;
for (i = 1; i < 35; i += 2 ) {
spiral(i);
puts("\n");
}
return 0;
}
输出:
#
# # #
#
# # #
# # # # #
#
# # # #
# #
# # # # #
# # # # # # #
#
# # # # # #
# # #
# # # # #
# #
# # # # # # #
# # # # # # # # #
#
# # # # # # # #
# # #
# # # # # #
# # # #
# # # # # # #
# #
# # # # # # # # #
# # # # # # # # # # #
#
# # # # # # # # # #
# # #
# # # # # # # #
# # # # #
# # # # # # #
# # # #
# # # # # # # # #
# #
# # # # # # # # # # #
# # # # # # # # # # # # #
#
# # # # # # # # # # # #
# # #
# # # # # # # # # #
# # # # #
# # # # # # # #
# # # # # #
# # # # # # # # #
# # # #
# # # # # # # # # # #
# #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # #
# # #
# # # # # # # # # # # #
# # # # #
# # # # # # # # # #
# # # # # # #
# # # # # # # # #
# # # # # #
# # # # # # # # # # #
# # # #
# # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # #
# # # # # # #
# # # # # # # # # #
# # # # # # # #
# # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
解释:
令i
代表当前行索引,j
代表当前列索引。
把问题分成两半。下半部分更容易,因为左右半部分是对称的,所以偶数行和奇数行都在交替的列索引 if j < size - i || j > i
.
上打印它们各自的图案
上半部分有点棘手:模式相同,只是左侧落后右侧 1,这意味着我们需要从左侧减去 1:if j < i - 1 || j >= size - i
.
请注意,此代码可以使用一些清理来减少重复;我保留了它的详细信息,以便清楚它是如何工作的。我还冒昧地调整了要打印的字符,因为原始字符在眼睛上更硬——这很容易使用 #define
s 或向函数添加参数进行调整。
所以我是 C 的新手,想知道如何创建以下螺旋形状:
这是我的滑稽尝试..
#include <stdio.h>
int main(void) {
int size;
int i,j;
printf("Enter size: ");
scanf("%d", &size);
for (i = 0, j = 0; i < size - 4; i++, j+=2) {
int row = 1;
while (row <= size) {
int column = 1;
while (column <= size) {
if ((row == 1 + j && column > j && column < size - j)||
(row == size - (j) && column > j && column < size - j) ||
(column == size - (j) && row > j && row <= size - j) ||
(column == 1 + j && row > (2+j) && row < size - j)) {
printf("*");
}
else {
printf("-");
}
column++;
}
row++;
printf("\n");
}
}
return 0;
}
如有任何帮助,我们将不胜感激!
我很喜欢这个问题,运行 遇到了一些想出方法的麻烦。不过,足够多的混乱使模式变得明显;这是结果:
#include <stdio.h>
#define BLANK " "
#define FILLED "# "
void spiral(int size) {
int i, j;
if (!(size & 1)) { size++; }
int half = size / 2 + 1;
/* top */
for (i = 0; i < half; i++) {
if (i & 1) {
for (j = 0; j < size; j++) {
if (!(j & 1) && (j < i - 1 || j >= size - i)) {
printf(FILLED);
}
else {
printf(BLANK);
}
}
}
else {
for (j = 0; j < size; j++) {
if ((j & 1) && (j < i - 1 || j >= size - i)) {
printf(BLANK);
}
else {
printf(FILLED);
}
}
}
puts("");
}
/* bottom half */
for (; i < size; i++) {
if (i & 1) {
for (j = 0; j < size; j++) {
if (!(j & 1) && (j < size - i || j > i)) {
printf(FILLED);
}
else {
printf(BLANK);
}
}
}
else {
for (j = 0; j < size; j++) {
if ((j & 1) && (j < size - i || j > i)) {
printf(BLANK);
}
else {
printf(FILLED);
}
}
}
puts("");
}
}
int main() {
int i;
for (i = 1; i < 35; i += 2 ) {
spiral(i);
puts("\n");
}
return 0;
}
输出:
#
# # #
#
# # #
# # # # #
#
# # # #
# #
# # # # #
# # # # # # #
#
# # # # # #
# # #
# # # # #
# #
# # # # # # #
# # # # # # # # #
#
# # # # # # # #
# # #
# # # # # #
# # # #
# # # # # # #
# #
# # # # # # # # #
# # # # # # # # # # #
#
# # # # # # # # # #
# # #
# # # # # # # #
# # # # #
# # # # # # #
# # # #
# # # # # # # # #
# #
# # # # # # # # # # #
# # # # # # # # # # # # #
#
# # # # # # # # # # # #
# # #
# # # # # # # # # #
# # # # #
# # # # # # # #
# # # # # #
# # # # # # # # #
# # # #
# # # # # # # # # # #
# #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # #
# # #
# # # # # # # # # # # #
# # # # #
# # # # # # # # # #
# # # # # # #
# # # # # # # # #
# # # # # #
# # # # # # # # # # #
# # # #
# # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # #
# # # # # # #
# # # # # # # # # #
# # # # # # # #
# # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
解释:
令i
代表当前行索引,j
代表当前列索引。
把问题分成两半。下半部分更容易,因为左右半部分是对称的,所以偶数行和奇数行都在交替的列索引 if j < size - i || j > i
.
上半部分有点棘手:模式相同,只是左侧落后右侧 1,这意味着我们需要从左侧减去 1:if j < i - 1 || j >= size - i
.
请注意,此代码可以使用一些清理来减少重复;我保留了它的详细信息,以便清楚它是如何工作的。我还冒昧地调整了要打印的字符,因为原始字符在眼睛上更硬——这很容易使用 #define
s 或向函数添加参数进行调整。