C语言输出数字菱形
发布时间:2016-10-24 21:58
财富值79
我写的参考:
//数字菱形
#include <stdio.h>
int main()
{
int i, j, x, a, n, t, s;
char c;
printf("请选择构成菱形方式(1-数字菱形,2-字母菱形):");
scanf("%d", &t);
if(t == 1)
{
s = 17;
c = '1';
}
else
{
s = 51;
c = 'A';
}
printf("请输入3-%d间的奇数:", s);
x = 0;
while(scanf("%d", &x) != 1 || x % 2 == 0 || x < 3 || x > s)
{
printf("输入错误,请重输:");
fflush(stdin);
}
x=(x+1)/2;
for(i = -x; i <= x; i++)
{
if(i <= 0)
n = (i + x);
else
n = (-i + x);
for(a = 1; a <= x - n; a++)
printf(" ");
for(j = 1; j <= n * 2 - 1; j++)
printf("%c", c + n - 1);
printf("
");
}
}