我该如何打印 - [英] How do I print this-

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

问题描述

______ 4

____4 3 4

__4 3 2 3 4

4 3 2 1 2 3 4

__4 3 2 3 4

___ 4 3 4

______4



写一个C ++程序来获取双金字塔上方。

'_' - 这表示空间,就是金字塔。



我尝试过:



 #include< iostream>使用namespace std 

;

int main()
{
int i,j;
for(i = 4; i> = 1; i - ){
for(j = 1; j< = i; j ++){
cout<<;
}
for(int j = 4; j> = i; j - ){
cout<< j;
}
for(j = i + 1; j< = 4; j ++){
cout<< j;
cout<<'\ n';
}
for(i = 2; i< = 4; i ++){
for(j = 1; j< i; j ++){
cout<< j;
}
for(j = 1 + i; j< = 4; j ++){
cout<< j;
cout<<'\ n';
}
}

}
返回0;
}





我试过这个,我觉得我做了一些非常愚蠢的错误。请帮助我。

解决方案

首先正确地缩进你的代码,或者,如果你打算使用可执行的1TB,那么至少要始终如一:

 int main(){
int i,j;
for(i = 4; i> = 1; i - ){
for(j = 1; j< = i; j ++){
cout<<;
}
for(int j = 4; j> = i; j - ){
cout<< j;
}
for(j = i + 1; j< = 4; j ++){
cout<< j;
cout<<'\ n';
}
for(i = 2; i< = 4; i ++){
for(j = 1; j< i; j ++){
cout<< j;
}
for(j = 1 + i; j< = 4; j ++){
cout<< j;
cout<<'\ n';
}
}

}
返回0;
}

现在你可以看到你在做什么。我想到的第一件事就是:你有两个循环,都改变 i ,其中一个在另一个里面...所以你的代码永远不会退出。 / blockquote>

让我们猜测输出有太多行

  for ( j = i + 1; j< =  4 ; j ++){
cout<< j;
cout<< ' \ n'; // 删除此行
}
cout<< ' \ n'; // 并将其放在那里



你的程序很复杂,只有4号。

a小分析应该有助于让它变得更好。钻石具有垂直和水平对称性,全部围绕中心(1)

尺寸行Cols Center 
1 1 1 0 0
2 3 3 1 1
3 5 5 2 2
4 7 7 3 3



将大小存储在变量中并减少行,列和中心应该不复杂。

你能够 请注意,所有'2'都距离中心1 col或1行。

'3'更进一步:行距离+ cols距离给出位置值。

如果值更大,请打印一个空格。

我让你填补这些洞作为练习。



建议:

- 学习一种或多种分析方法,EW Djikstra / N. Wirth Stepwize Refinment / top-Down方法是一个良好的开端。

Structured Programming.pdf [ ^ ]

https://en.wikipedia。 org / wiki / Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/wiki/Structured_programming [ ^ ]

https://en.wikipedia.org/wiki/Edsger_W._Dijkstra [ ^ ]

https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF [ ^ ]


______4
____4 3 4
__4 3 2 3 4
4 3 2 1 2 3 4
__4 3 2 3 4
___ 4 3 4
______4

Write a C++ program to obtain the above double pyramid.
'_' - this indicates space, that's how it is a pyramid.

What I have tried:

#include <iostream>

using namespace std;

int main()
{
     int i,j;
     for(i=4; i>=1; i--){
        for(j=1; j<=i; j++){
           cout<<" ";
        }
     for(int j=4; j>=i; j--){
        cout<<j;
     }
          for( j=i+1; j<=4; j++){
            cout<<j;
            cout<<'\n';
          }
          for(i=2; i<=4; i++){
            for(j=1; j<i; j++){
                cout<<j;
            }
            for(j=1+i; j<=4; j++){
                cout<<j;
                cout<<'\n';
            }
          }

     }
      return 0;
}



I have tried this and I have a feeling that I have done some really stupid mistake. Please help me.

解决方案

Start by indenting your code properly, or, if you are going to use the execrable 1TB then at least consistently:

int main(){
    int i,j;
    for(i=4; i>=1; i--){
        for(j=1; j<=i; j++){
            cout<<" ";
            }
        for(int j=4; j>=i; j--){
            cout<<j;
            }
        for( j=i+1; j<=4; j++){
            cout<<j;
            cout<<'\n';
            }
        for(i=2; i<=4; i++){
            for(j=1; j<i; j++){
                cout<<j;
                }
            for(j=1+i; j<=4; j++){
                cout<<j;
                cout<<'\n';
                }
            }
        
        }
    return 0;
    }

And now you can see what you are doing. And the first thing that springs to mind is this: you have two loops, both altering i and one of them is inside the other ... so your code never exits.


Lets guess the output have too many lines

for( j=i+1; j<=4; j++){
    cout<<j;
    cout<<'\n'; // Remove this line
}
cout<<'\n'; // and put it there


Your program is complicated and for size 4 only.
a little analyze should help to get it better. The diamond have a vertical and horizontal symmetry, all is around the center (1)

Size Rows Cols Center
 1    1    1    0,0
 2    3    3    1,1
 3    5    5    2,2
 4    7    7    3,3


Should not be complicated to store the Size in a variable and reduce Rows, Cols and Center.
You can notice that all '2' are at a distance of 1 col or 1 row from center.
'3' are 1 step further: distance in rows + distance in cols give you the value at position.
if value is more the the size, print a space.
I let you fill the holes as an exercise.

Advice:
- Learn one or more analyze methods, E.W. Djikstra/N. Wirth Stepwize Refinment/top-Down method is a good start.
Structured Programming.pdf[^]
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]


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

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