c语言字母金字塔怎么输出如下图
#include "stdio.h"
void main()
{
char zm[36]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
int i,j,k;
for(i=0;i<10;i++)
{
for(k=10-i;k>=0;k--)
printf(" ");
for(j=0;j<i+1;j++)
{
printf("%c",zm[j]);
if(j<i)
printf(" ");
}
printf("\n");
}
}
输出为
总是不对 展开
先用楼主的思路走一遍
#include "stdio.h"
void main()
{
char zm[36]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
int i,j=0,k,z; //记住赋初始值
for(i=0;i<8;i++) //不是10
{
for(k=6-i;k>=0;k--) //改为6
printf(" ");
for(z=0;z<i+1;z++) //用Z来循环 ,j自主加就行,之前方法会自动归零
{
printf("%c",zm[j]);
if(z<i)
printf(" ");
j++; //自主累加就行
}
printf("\n");
}
}
这个程序还有很多改进方式
1,数组可以去掉,直接用char输出,j初始值等于 65,判断j>90时j=48,
#include "stdio.h"
void main()
{
char j=65;
int i,k,z;
for(i=0;i<8;i++)
{
for(k=6-i;k>=0;k--)
printf(" ");
for(z=0;z<i+1;z++)
{
printf("%c",j);
if(z<i)
printf(" ");
if(j<90)
j++;
else
j=48;
}
printf("\n");
}
}
2,用j作主循环会好点,根据j判断i 会减少一层循环
#include "stdio.h"
void main()
{
char j=1;
int i=1,k=1,s=1;
while(i<=8)
{
if(k<9-i)
{
printf(" ");
k++;
}
else
{
if(j<=s)
{ if(j<=26)
printf("%c ",j+64);
else printf("%c ",j+21);
j++;
}
else
{printf("\n");
i=i+1;
s=s+i;
k=1;
}
}
}
}
#include "stdio.h"
void main()
{
char zm[36]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
int i,j,k,t=0;//定义一个变量t,初始化为0
for(i=0;t<36;i++)//循环结束的条件是输出了所有的字符
{
for(k=10-i;k>=0;k--)
printf(" ");
for(j=0;j<i+1;j++)
{
printf("%c",zm[t++]);//输出zm[t++]
if(j<i)
printf(" ");
if(t>=36)//结束输出
break;
}
printf("\n");
}
}
/*
A
B C
D E F
G H I J
K L M N O
P Q R S T U
V W X Y Z 0 1
2 3 4 5 6 7 8 9
Press any key to continue
*/
#include <stdio.h>
void main() {
char zm[]={'A','B','C','D','E','F','G','H','I',
'J','K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z','0','1','2','3','4',
'5','6','7','8','9'
};
int i,j,k,cnt = 0;
for(i = 0;i < 8;i++) {
for(k = 10 - i;k >= 0;k--) printf(" ");
for(j = 0;j < i + 1;j++)
printf("%c ",zm[cnt++]);
printf("\n");
}
}