我想做:
*
* *
* * *
* * * *
* * * * *
* * * * * *
但我不知道如何制作间距,我能得到的最接近的是:
#include <iostream>
#include <conio.h>
using namespace std;
int main(int argc, char** argv) {
int x, y;
for (y = 0 ; y <= 5 ; y ++){
for (x = 0 ; x < y ; x++) {
cout<<" * ";
}
printf("\n");
}
getch ();
return 0;
}
答案 0 :(得分:2)
我会帮助你...但仅仅因为它几乎是圣诞节
int x, y;
for (y = 0; y <= 5; y++) {
for (x = 0; x < y; x++) {
for (int i = 0; x == 0 && i < (5 - y); ++i)
cout << ' ';
cout << " *";
}
cout << '\n';
}
答案 1 :(得分:0)
你从6个空格和一个星号开始。 接下来是5个空格和一个星号,然后是1个空格+星号 接下来是4个空格和一个星号,然后是2个空格+星号 ... 等等。你看到了这种模式吗?
#include <iostream>
int main(int argc, char* argv[])
{
for (int height = 6; height > 0; --height)
{
// Leading spaces
for (int i = 1; i < height; ++i)
{
std::cout << ' ';
}
// and the asterix
std::cout << '*';
// then trailing space+asterix
for (int i = height; i < 6; ++i)
{
std::cout << " *";
}
std::cout << std::endl;
}
}
答案 2 :(得分:0)
输出星号后输出一个空格
你在这里。
#include <iostream>
#include <iomanip>
int main()
{
while (true)
{
const char asterisk = '*';
std::cout << "Enter a non-negative number (0 - exit): ";
unsigned int n;
if (not (std::cin >> n) or n == 0) break;
std::cout << '\n';
for ( unsigned int i = 0; i < n; i++ )
{
std::cout << std::setw( n - i + 1 );
for (unsigned int j = 0; j < i + 1; j++)
{
std::cout << asterisk << (j == i ? '\n' : ' ');
}
}
std::cout << std::endl;
}
return 0;
}
程序输出可能看起来像
Enter a non-negative number (0 - exit): 6
*
* *
* * *
* * * *
* * * * *
* * * * * *
Enter a non-negative number (0 - exit): 5
*
* *
* * *
* * * *
* * * * *
Enter a non-negative number (0 - exit): 4
*
* *
* * *
* * * *
Enter a non-negative number (0 - exit): 3
*
* *
* * *
Enter a non-negative number (0 - exit): 2
*
* *
Enter a non-negative number (0 - exit): 1
*
Enter a non-negative number (0 - exit): 0