分印钻石 [英] Printing diamond of stars

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

问题描述

我打印分的钻石,但我的code是编译和运行,但它只是印刷钻石的一半......请人帮忙。

下面我提到我的code。 我无法理解的问题,因为系统为什么仅执行第一半,而不是金刚石的另一半。

 #包括<的iostream>
使用名字空间std;
诠释的main()
{
    INT N,I = 0,J;
    霉素>将N;

    而(ⅰ&其中; =(N / 2))
    {
    J = 0;
    而(J&≤(N / 2)-i){
        COUT<<;
        当J = J + 1;

    }
    J = 0;
    而(J<我+ 1){
        的cout&其中;&所述;*;
        当J = J + 1;
    }
    J = 0;
    而(J&其中; = I-1){
        的cout&其中;&所述;*;
        当J = J + 1;
    }
    COUT<<\ N的;
    I = I + 1;
    }

而(ⅰ&其中; =(N / 2))

      {
        J = 0;
        而(J&≤(N / 2)-i)
        {
            COUT<<;
            当J = J + 1;

        }
        J = 0;
        而(J<我)
            {
            的cout&其中;&所述;*;
            当J = J + 1;
            }
        J = 0;
        而(J&所述I-1){
            的cout&其中;&所述;*;
            J ++;
        }
        COUT<<\ N的;
        一世 - ;
      }

返回0;
}
 

解决方案

当您在第一个循环我结束,你有 I = N / 2 + 1
在第二循环中,您希望下降。因此,它必须是,而(I> 0)。,而不是它现在是

I am printing diamond of stars but my code is compiling and running but it is only printing the half of diamond......anyone please help.

below I have mentioned my code. I cannot understand the problem as why the system is only executing the first half and not the other half of diamond.

#include<iostream>
using namespace std;
int main()
{
    int n,i=0,j;
    cin>>n;

    while(i<=(n/2))
    {
    j=0;
    while(j<(n/2)-i){
        cout<<" ";
        j=j+1;

    }
    j=0;
    while(j<i+1){
        cout<<"*";
        j=j+1;
    }
    j=0;
    while(j<=i-1){
        cout<<"*";
        j=j+1;
    }
    cout<<"\n";
    i=i+1;
    }

while(i<=(n/2))

      {
        j=0;
        while(j<(n/2)-i)
        {
            cout<<" ";
            j=j+1;

        }
        j=0;
        while(j<i)
            {
            cout<<"*";
            j=j+1;
            }
        j=0;
        while(j<i-1){
            cout<<"*";
            j++;
        }
        cout<<"\n";
        i--;
      }

return 0;
}

解决方案

When you first loop in i ends, you have i = n/2 + 1
In the second loop you want i to decrease. So it must be while (i > 0) and not what it currently is.

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

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