空心菱形(一个循环)

代码

#include<stdio.h>

int n;

void init() {
	printf("请输入n:");
	scanf("%d", &n);
}

void output() {
	int spot = 0;
	int linecount = 0;
	int mid = n / 2;
	int pos_1 = mid;
	int pos_2 = 2 * mid - pos_1;
	while (linecount < n) {
		if (spot == pos_1 || spot == pos_2) {
			if (pos_1 == pos_2) {
				linecount++;
				printf("*");
				spot = 0;
				pos_1 = linecount > mid ? pos_1 + 1 : pos_1 - 1;
				pos_2 = 2 * mid - pos_1;
				printf("\n");
			}
			else {
				if (spot == pos_1) {
					printf("*");
					spot++;
				}
				else {
					linecount++;
					printf("*");
					spot = 0;
					pos_1 = linecount > mid ? pos_1 + 1 : pos_1 - 1;
					pos_2 = 2 * mid - pos_1;
					printf("\n");
				}
			}
		}
		else {
			printf(" ");
			spot++;
		}
	}
}

int main() {
	init();
	output();
	return 0;
}

posted @ 2022-11-16 19:56  彭乐祥  阅读(41)  评论(0编辑  收藏  举报