在c ++中打印星号的“三角形” [英] Printing a “triangle” of asterisks, in c++

查看:159
本文介绍了在c ++中打印星号的“三角形”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个打印此类输出的通用代码,意味着用户可以输入任何值+整体。

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

$ b b

for 5



  * 
* *
* * *



这里是我做的。但它打印正好直角三角形。任何帮助。提前感谢。

  #include< iostream> 
using namespace std;

int main()
{
int val;

cout<< 输入数字:;
cin>> val;

int t = val;
int x = val;

for(int r = 1; r <= val; r ++)
{


for(int c = 1; c< = t; c ++)
{
if(c< x)
{
cout< '';
}
else
{
cout< *;
}
}
cout<< endl
x = x-1;
}
return 0;
}


解决方案

在cout,它调整所有的序列。平均值*;这是它。


i want to make a generic code that prints such output, mean user can enter any value +whole.

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

for 5

and

  *
 * *
* * *

for 3.

here what i make. but it prints just right triangle. Any helps. thanks in advance.

#include <iostream>
using namespace std;

int main()
{
int val;

cout << "Enter the number: ";
cin >> val;

int t  = val;
int x = val;

for(int r = 1; r <= val; r++)
{


for(int c  = 1; c <=t ; c++)
{
    if(c < x)
    {
    cout << ' ';
    }
    else
    {
    cout << "*";
    }
}    
cout << endl;
x = x-1;
}
return 0;
}

解决方案

place 'space' after * in cout, it adjust all your sequence. mean cout << "* "; thats it.

这篇关于在c ++中打印星号的“三角形”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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