2017-03-23 25 views
1

好了,所以我要创建这个钻石形,排序的,它应该是一个菱形但最高右半丢失,矩形的左半边缺失。我有两种形状,但它们没有以菱形形式出现,而是将其中一个打印出来。钻石形状心不是印刷正确

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

#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; 
} 
+2

通过与您的开发环境几乎肯定就应该使这个问题的短期工作的调试器分步执行代码。 – user4581301

+1

您没有分别处理上半部分和下半部分的间距。看到这里http://www.sitesbay.com/cpp-program/cpp-print-diamond-of-star。在发布问题之前,最好在网络上搜索解决方案,您将获得更快的解决方案以及社区不需要搜索解决方案并为您提供链接。你会首先自己动手学习更多。如果还有更多问题,社区随时准备帮助你。在发布这个问题时,你应该展示一些你自己的研究来找到问题的解决方案。 –

回答

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++)

0

,我想分享我的答案,如果我明白你的问题,我想你正在寻求下面的形状。

  * 
     * * 
     * * * 
    * * * * 
    * * * * * 
      * * * * * 
      * * * * 
      * * * 
      * * 
      * 

这是菱形缺少右上和左下部分

#include <iostream> 

using namespace std; 

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

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

    for(row = 1; row <= size; row++){ 
    for (col = 1; col <= size; col++){ 
     if(col+row == (size/2)) 
     for(int i=0;i<row;i++) 
      cout <<"*"; 
     if(row >= size/2 && col >= size/2) 
     { 
      if(col + row < size+size/2) 
      cout <<"*"; 
     } 
     else 
     cout <<" "; 
    } 
    cout << endl; 
    } 

    return 0; 
} 

我希望你觉得它有用。