繁体   English   中英

如何实现这个所需的 output?

[英]How to achieve this desired output?

我可能听起来与以前的帖子重复,但我一直遇到问题。 我的目标是让我当前的代码打印出我当前拥有的内容的反映。

这是我到目前为止的代码示例:

#include <iostream>
#include <string>
#include <iomanip>  
using namespace std;

int main()
{
    int numberOfDolls = 0;
    cout << "Number of dolls -> ";
    cin >> numberOfDolls;
    
    // Increment the proper number of dolls.  
    
    for (int i = 1; i <= numberOfDolls; i++)
    {
      for (int j = 1, n = numberOfDolls; j <= i; j++)
      { 
        cout << setw(n--) << '/' << endl;
      }
      for (int k = 1, s = numberOfDolls - i + 1; k <= i; k++)
      {
        cout << setw(s++) << '\\' << endl;
      }
      cout << setw(numberOfDolls + 1) << "-" << endl;
    } 
}

如果用户输入 3,它会打印:

   /
   \
    -
   /
  /
  \ 
   \
    -
   /
  /
 /
 \
  \
   \
    -

我需要最终产品如下所示。

   /   \
    \ /
     -

   /   \
  /     \
   \   /
    \ /
     -
  
   /   \ 
  /     \
 /       \
  \     /
   \   /
    \ /
     -

这可能有点难以理解,如果需要更多说明,请告诉我。

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    int numberOfDolls = 0;
    int deltaIndent = 0;
    int topMergin = 3;
    int botmMergin = 1;
    cout << "Number of dolls -> ";
    cin >> numberOfDolls;



    for (int i = 1; i <= numberOfDolls; i++) {

        deltaIndent = numberOfDolls - i;
        for (int j=i; j>0 ;j--)
            cout << setw(j + deltaIndent) << '/' << setw(topMergin + (i-j) * 2 + 1) << '\\' <<endl;

        for (int j = 1; j <= i ; j++)
            cout << setw(j + deltaIndent + 1) << '\\' << setw(botmMergin + (i-j) * 2 + 1) << '/' << endl;

        cout << setw(numberOfDolls + 2) << "-" << endl << endl;
    }
}

output

Number of dolls -> 4
   /   \
    \ /
     -

   /   \
  /     \
   \   /
    \ /
     -

   /   \
  /     \
 /       \
  \     /
   \   /
    \ /
     -

   /   \
  /     \
 /       \
/         \
 \       /
  \     /
   \   /
    \ /
     -

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM