代码实现
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS 1
//求出上方层数
int panduan(int a) {
a = a - 1;
int m = 1;
for (int i = 2; a - (4 * i - 2) >= 0; i++) {
m++;
a = a - (4 * i - 2);
}
return m;
}
//打印图形
void dyx(int m, char a)
{
for (int i = m; i > 1; i--) {
for (int p = m; p > i; p--) {
printf(" ");
}
for (int q = 1; q <=(2*i-1); q++) {
printf("%c",a);
}
printf("\n");
}
for (int i = 1; i <= m - 1; i++) {
printf(" ");
}
printf("%c\n",a);
for (int i = 2; i <= m; i++) {
for (int p = m; p > i; p--) {
printf(" ");
}
for (int q = 1; q <= (2 * i - 1); q++) {
printf("%c",a);
}
printf("\n");
}
}
//求余数
void qiuyu(int a, int n) {
if (n == 1) {
printf("%d", a - 1);
}
else {
int b = a - (2 * n * n - 1);
printf("%d", b);
}
}
//主函数
int main()
{
int a = 0;
char c = ' ';
int n=0;
scanf("%d %c", &a,&c);
if(a>0)
n=panduan(a);
dyx(n,c);
qiuyu(a, n);
return 0;
}