c++ - 钻石形状打印不正确

标签 c++ shapes

好的,所以我必须创建这个菱形,它应该是菱形,但右上半部分缺失,左下半部分缺失。我有两个形状,但它们没有以菱形形式显示,而是打印出一个在另一个之上。

我假设某处。我必须打印出相同数量的空间才能将整个底部形状向右移动?

#include <iostream>

using namespace std;

int main()
{
   int row, col, siz;

   cout << "Enter the size of the diamond: " << endl;
   cin >> siz;

   cout << endl << endl;


   for(row = 1; row <= siz; row++){

      for(col = 1; col <= siz; col ++){
         if(col <= siz - row){
            cout << " ";
         }
         else{
                cout << "@";
             }
        }
        cout << "\n";
    }

    for(row = 1; row <= siz; row++){

        for(col = row; col <= siz; col++){
            cout << "@";
        }
        cout << "\n";
    }

    return 0;
}

最佳答案

这是我对你问题的解决方案:

#include <iostream>
using namespace std;

int main()
{
   int row, col, siz;

   cout << "Enter the size of the diamond: " << endl;
   cin >> siz;

   cout << endl << endl;

   // drawing top left side of diamon
   for(row = 1; row <= siz; row++){
      for(col = 1; col <= siz; col ++){
         if(col <= siz - row){
            cout << " ";
         }
         else{
                cout << "@";
             }
        }
        cout << "\n";
    }

    // drawing bottom right side of diamon
    for(row = 1; row <= siz; row++){
        for(col = 1; col <= siz*2-row; col++){
            if(col<siz){
               cout << " "; 
            }
            else{
                cout << "@";
            }
        }
        cout << "\n";
    }

    return 0;
}

解释: 下半场的第一列必须先绘制“siz”时间“”,然后再绘制“siz”时间“@”。所以它必须循环 siz*2;从那时起你总是有'siz'时间''然后每次都少一个'@'所以这就是为什么循环应该看起来像for(col = 1; col <= siz*2-row; col++)

关于c++ - 钻石形状打印不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42967776/

相关文章:

android - 按钮不会改变其在选择器中的形状

css3 - 如何制作这个形状?

android - 如何制作进度圈

c++ - 我是否需要在所有绘图代码之后重复 glOrtho 调用?

c++ - 在结构中的数组中初始化结构中的数组时出现问题

c++ - 如何在 C++ 中生成 UUID,而不使用 boost 库?

c++ - 是什么阻止了 __attribute__((packed)) 进入 ISO C/C++?

java - 调整复合(设计模式)形状的大小

c++ - QT将参数传递给QDialog要求

python - 当我们使用列表理解时如何保持原始列表的形状?