-2

I need help, I created a short little program a while ago where it would print a simple pyramid with "*" like this:

  *
 ***
*****

but I decided to challenge myself and see if I could create a simple diamond shape like this:

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

Here is my code so far. I should also add that the value you input, for example 5, determines how big the diamond is.

#include <iostream>
#include <sstream>
using namespace std;

int main() {




int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;


for (int i = 0; i < value; i++) {
    //print spaces v v v
    for (int x = 0; x < (value - i - 1); x++) {
        cout << " ";
    }
    //print * v v v
    for (int y = 0; y < (2 * i + 1); y++) {
        cout << "*";
    }
    cout << endl;
}

for (int i = 0; i < value; i++) {
    int number = 0;
    number+= 2;
    //print spaces v v v
    for (int x = 0; x < (value - value + i + 1); x++) {
        cout << " ";
    }
    //print * v v v
    for (int y = 0; y < (/*ATTENTION: What do I do here? Plz help*/); y++) {
        cout << "*";
    }
    cout << endl;
}



return 0;
}

What I've been trying to do is figure out what to put inside the parenthesis where it says (//ATTENTION). I've been working for at least an hour trying to do random things, and one time it worked when I input 4, but not for 5, and it's just been very hard. This is key to building the diamond, try putting in just value and compile to see what happens. I need it to be symmetrical.

I need to know what to put inside the parenthesis please. I'm sorry this is very long but the help would be appreciated thanks.

I also apologize if my code is messy and hard to read.

4

3 回答 3

1
#include <iostream>
#include <sstream>
using namespace std;

int main() {

int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;


for (int i = 0; i < value; i++) {
    //print spaces v v v
    for (int x = 0; x < (value - i - 1); x++) {
        cout << " ";
    }
    //print * v v v
    for (int y = 0; y < (2 * i + 1); y++) {
        cout << "*";
    }
    cout << endl;
}

for (int i = 0; i < value-1; i++) {
//    int number = 0;
//    number+= 2;
//    //print spaces v v v

    for (int x = 0; x < i+1; x++) {
        cout << " ";
    }
    //print * v v v
     for (int y = 0; y < (2*(value-1-i)-1); y++) {
        cout << "*";
        }
    cout << endl;
}



return 0;
}

我希望你能得到这个。此外,在第二个 for 循环中,你通过将循环迭代到value. 但由于金字塔是对称的,所以金字塔中的行数将是2*value-1。所以我在第二个循环中i应该变化到value -1

于 2017-12-09T07:23:34.737 回答
1

int number = 0;number+= 2;

value - value里面for (int x = 0; x < (value - value + i + 1); x++) {

不是必需的。


在括号内,您可以使用

2*(value-i-1)-1

但是,我建议您首先分析问题,然后尝试解决它,而不是尝试随机的事情。例如,让我们考虑偶数和奇数输入的情况,即 2 和 3。

偶数案 (2)

 *
***
***
 *

分析

Row Index    Number of Spaces    Number of Stars
0            1                   1
1            0                   3
2            0                   3
3            1                   1

对于行索引 < 值

  1. 空格数 =value - row index - 1
  2. 星数 =2 * row index + 1

对于行索引 >=value 空格和星号的数量只是简单地颠倒过来。在奇怪的情况下,情况也相似,但有一个小例外。

奇案 (3)

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

分析

Row Index    Number of Spaces    Number of Stars
0            2                   1
1            1                   3
2            0                   5
3            1                   3
4            2                   1

一个小的例外是,在反转时,我们必须忽略行索引 = 值。


现在,如果我们将上述分析放入代码中,我们将得到解决方案

//Define the Print Function
void PrintDiamond(int rowIndex, int value)
{
    //print spaces v v v
    for (int x = 0; x < value - rowIndex -1; x++) {
        cout << " ";
    }
    //print * v v v
    for (int y = 0; y < 2 * rowIndex + 1; y++) {
        cout << "*";
    }
    cout << endl;
}

然后在 main

//Row index < value
for (int i = 0; i < value; i++) {
    PrintDiamond(i,value);
}

//For row index >= value reversing the above case
//value-(value%2)-1 subtracts 1 for even and 2 for odd cases
//ignore the row index = value in odd cases
for (int i = value-(value%2)-1; i >=0; i--) {
    PrintDiamond(i,value);
}
于 2017-12-09T09:27:26.990 回答
1

这段代码应该可以解决问题:

#include <sstream>
using namespace std;

void printSpaces(int howMany) {
    for(int i = 0; i < howMany; i++) cout << " ";
}

void figure(int size) {
    bool oddSize = size % 2 == 1;
    int center = size / 2;
    int spaces = size / 2;
    // If figure is of an odd size adjust center
    if (oddSize) {
        center++;
    } else { // Else if figure is of even size adjust spaces
        spaces--;
    }

    for (int i = 1; i <= center; i++) {
        printSpaces(spaces);
        for(int j = 0; j <  1 + (i - 1) * 2; j++) cout << "*";
        cout << endl;
        spaces--;
    }

    spaces = oddSize ? 1 : 0; // If the figure's size is odd number adjust spaces to 1
    center -= oddSize ? 1 : 0; // Adjust center if it's an odd size figure
    for(int i = center; i >= 1; i--) {
        printSpaces(spaces);
        for(int j = 0; j <  1 + (i - 1) * 2; j++)
           cout << "*";
        cout << endl;
        spaces++;
    }

}

int main() {
    int value = 0;
    while(value < 3) {
        cout << "Please enter in a value (>= 3): ";
        cin >> value;
        cout << endl;
    }
    figure(value);
    return 0;
}
于 2017-12-09T08:35:50.657 回答