c语言编程 求大神 求解答 5C

• Write a program that outputs a diamond shape • Use ‘*’, ‘ ‘, and new line character(‘\n’)
• Use the for statement
• The first line has four ‘ ‘ and one ‘*’
• The second line has three ‘ ‘ and three ‘*’
• The third line has two ‘ ‘ and five ‘*’
• The fourth line has one ‘ ‘ and seven ‘*’
• The fifth line has nine ‘*’, and the following line is in reverse图片说明

c

6个回答

 #include <stdio.h>


 void PrintDiamond(void)
{
    printf("    *\n");
    printf("   ***\n");
    printf("  *****\n");
    printf(" *******\n");
    printf("*********\n");
    printf(" *******\n");
    printf("  *****\n");
    printf("   ***\n");
    printf("    *\n");
}

int main(void)
{
    PrintDiamond();
    return 0;
}
#include<cstdio>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
int main(){
    fo(i,1,5){
        fo(j,1,5-i)printf(" ");
        fo(j,1,i*2-1)printf("*");
        fo(j,1,5-i)printf(" ");
        printf("\n");
    }
    fd(i,4,1){
        fo(j,1,5-i)printf(" ");
        fo(j,1,i*2-1)printf("*");
        fo(j,1,5-i)printf(" ");
        printf("\n");
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
int i,j,k;
for(i=0;i<=4;i++)
{
for(j=0;j<=3-i;j++)
printf(" ");
for(k=0;k<=2*i;k++)
printf("*");
printf("\n");
}
for(i=0;i<=3;i++)
{
for(j=0;j<=i;j++)
printf(" ");
for(k=0;k<=6-2*i;k++)
printf("*");
printf("\n");
}
return 0;
}

fo(i,1,5){
fo(j,1,5-i)printf(" ");
fo(j,1,i*2-1)printf("*");
fo(j,1,5-i)printf(" ");
printf("\n");
}
fd(i,4,1){
fo(j,1,5-i)printf(" ");
fo(j,1,i*2-1)printf("*");
fo(j,1,5-i)printf(" ");
printf("\n");
}

可以想象成一个坐标系,原点是0,0 ,然后区域内是*,区域外是空格

①其实如果只要只一个形状的话,可以直接printf(" ……“)(我就不加星号了。。。
②如果是要输出n乘n的矩阵的话,那么就

 for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
        if(abs(i-(n<<1))+abs(j-(n<<1)<5)printf(" ");else printf("*");

就是这种样子了。。。c站的代码片真难受

MARSHALBEN
longint64 哦哦哦,还需要换行。。。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐