【思路】将菱形分成两部分打印,上半段6+1行,下半段6行
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{ //打印上半部分
int i = 0;
for (i = 1; i <= 7; i++)
{
int j = 0;
for (int j = 1; j <= 7 - i; j++)
{
printf(" ");
}
for (j = 8 - i; j < 7 + i; j++)
{
printf("*");
}
printf("\n");
}
//打印下半部分
for (i = 1; i <= 6; i++)
{
int j = 0;
for (int j = 1; j <= i; j++)
{
printf(" ");
}
for (j = i+1; j <= 13-i; j++)
{
printf("*");
}
printf("\n");
}
}
【扩展】可以打印任意大小的菱形
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int n = 0;//上半段的行数
scanf("%d", &n);
int i = 0;
for (i = 1; i <= n+1; i++)
{
int j = 0;
for (int j = 1; j <= n+1 - i; j++)
{
printf(" ");
}
for (j = n+2 - i; j < n+1 + i; j++)
{
printf("*");
}
printf("\n");
}
for (i = 1; i <= n; i++)
{
int j = 0;
for (int j = 1; j <= i; j++)
{
printf(" ");
}
for (j = i+1; j <= 2*n+1-i; j++)
{
printf("*");
}
printf("\n");
}
}