我如何...打印结构 [英] How do i...print the structure

查看:74
本文介绍了我如何...打印结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

4 3 2 1 2 3 4

3 2 1 2 3

2 1 2

1



我尝试过:



 #include< stdio.h> ; 

int main()
{
int i,space,rows,col;



for(rows = 4; rows> = 1; rows--)
{

for(col = rows; col> = 1; col--)
{
printf(%d,col);
}

for(col = 2; col< = rows; col ++)
{
printf(%d,col);
}

printf(\ n);
}

返回0;
}

解决方案

你的代码 - 虽然收缩很糟糕 - 有效。当我将你的代码复制到在线IDE并运行它时,我得到:

 4 3 2 1 2 3 4 
3 2 1 2 3
2 1 2
1


...程序以退出代码0完成

那么我在做什么你不是?


学会正确缩进代码,显示其结构,有助于阅读和理解。

  #include   <   stdio.h  >  

int main()
{
int i ,空间,行,col;
for (rows = 4 ; rows> = 1 ; rows--)
{
for (col = rows; col> = 1 ; col--)
{
printf( % d,列);
}

for (col = 2 ; col< = rows; col ++)
{
printf( %d,col) ;
}

printf( \ n);
}

return 0 ;
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]



您对此代码有疑问吗?


这是您要找的输出:

 4 3 2 1 2 3 4 
3 2 1 2 3
2 1 2
1





你需要输出悬空空间,这意味着你总是必须从4打印,即使只是一个空场。代码更改在这里:

 int main()
{
int rows,col;

for(rows = 4; rows> = 1; rows--)
{
for(col = 4; col> = 1; col--)
{
if(col< = rows)
{
printf(%d,col);
}
其他
{
printf();
}
}
for(col = 2; col< = 4; col ++)
{
if(col< = rows)
{
printf(%d,col);
}
其他
{
printf();
}
}

printf(\ n);
}

返回0;
}


4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1

What I have tried:

#include <stdio.h>

int main()
{
    int i, space, rows,col;



    for(rows=4; rows>=1; rows--)
    {
        
         for(col=rows;col>=1;col--)
       {
           printf(" %d",col);
       }

 for(col=2;col<=rows;col++)
       {
           printf(" %d",col);
       }

        printf("\n");
    }

    return 0;
}

解决方案

And your code - while badly indented - works. When I copy your code into an online IDE and run it I get:

 4 3 2 1 2 3 4                                                           
 3 2 1 2 3                                                               
 2 1 2                                                                   
 1                                                                       
                                                                         
                                                                         
...Program finished with exit code 0

So what am I doing that you are not?


Learn to indent properly your code, it show its structure and it helps reading and understanding.

#include <stdio.h>

int main()
{
    int i, space, rows,col;
    for(rows=4; rows>=1; rows--)
    {
        for(col=rows;col>=1;col--)
        {
            printf(" %d",col);
        }
        
        for(col=2;col<=rows;col++)
        {
            printf(" %d",col);
        }
        
        printf("\n");
    }

    return 0;
}


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Do you have a problem with this code ?


Is this is the output you are looking for:

4 3 2 1 2 3 4
  3 2 1 2 3
    2 1 2
      1



You need to output spaces for the overhangs, this means you always have to print from 4, even if only an empty field. The code change is here:

int main()
{
    int rows, col;

    for (rows = 4; rows >= 1; rows--)
    {
        for (col = 4; col >= 1; col--)
        {
            if (col <= rows)
            {
                printf(" %d", col);
            }
            else
            {
                printf("  ");
            }
        }
        for (col = 2; col <= 4; col++)
        {
            if (col <= rows)
            {
                printf(" %d", col);
            }
            else
            {
                printf("  ");
            }
        }

        printf("\n");
    }

    return 0;
}


这篇关于我如何...打印结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆