打印三角形图案

原文:

给定一个数字 n,它代表三角形模式的最后一项。任务是打印从 1 到 n 的三角形图案,这样每行就完成了。 三角形图案如下:

   1
  2 3
 4 5 6
7 8 9 10
.
.

例:

input: n = 3
output:
 1
2 3
input: n = 7
output:
   1
  2 3
 4 5 6
7 will not be printed as 
it would result in an incomplete row

进场:

  • 从给定的最后一项 n 中找出完整的行数
    • as: 对于最大高度= 1,最后一项为 1 对于最大高度= 2,最后一项为 3 对于最大高度= 3,最后一项为 6
    • 所以最后一项形成了一个模式:
    • 因此,t2【a(n)= 1 2 3 4…… (n–1) n = n(n 1)/2 即 a(n)是前 n 个自然数之和。
    • 所以在
a(n) = n(n   1) / 2
a(n) represents the last term (as per our problem),
and n represents the max height of the triangle
  • 因此,这可以看作:
last term = height (height   1) / 2
  • 因此,
height = (-1   sqrt(1   8*lastterm)) / 2
  • 找到最大高度后,就可以轻松打印三角形图案了。

以下是上述方法的实现:

c

// c   code for printing the
// triangle pattern using last term n
#include 
using namespace std;
// function to demonstrate printing pattern
void triangle(int n)
{
    // number of spaces
    int k = 2 * n - 2;
    // character to be printed
    int ch = 1;
    // outer loop to handle number of rows
    // n in this case
    for (int i = 0; i < n; i  ) {
        // inner loop to handle number spaces
        // values changing acc. to requirement
        for (int j = 0; j < k; j  )
            cout << " ";
        // decrementing k after each loop
        k = k - 1;
        // inner loop to handle number of columns
        // values changing acc. to outer loop
        for (int j = 0; j <= i; j  ) {
            // printing stars
            cout << ch   << " ";
        }
        // ending line after each row
        cout << endl;
    }
}
// function to find the max height
// or the number of lines
// in the triangle pattern
int maxheight(int n)
{
    return (((int)sqrt(1   8.0 * n)) - 1) / 2;
}
// driver function
int main()
{
    int n = 9;
    triangle(maxheight(n));
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java code for printing the
// triangle pattern using last term n
import java.util.*;
class gfg
{
// function to demonstrate printing pattern
static void triangle(int n)
{
    // number of spaces
    int k = 2 * n - 2;
    // character to be printed
    int ch = 1;
    // outer loop to handle number of rows
    // n in this case
    for (int i = 0; i < n; i  )
    {
        // inner loop to handle number spaces
        // values changing acc. to requirement
        for (int j = 0; j < k; j  )
            system.out.print(" ");
        // decrementing k after each loop
        k = k - 1;
        // inner loop to handle number of columns
        // values changing acc. to outer loop
        for (int j = 0; j <= i; j  )
        {
            // printing stars
            system.out.print(ch     " ");
        }
        // ending line after each row
        system.out.println();
    }
}
// function to find the max height
// or the number of lines
// in the triangle pattern
static int maxheight(int n)
{
    return (((int)math.sqrt(1   8.0 * n)) - 1) / 2;
}
// driver code
public static void main(string[] args)
{
    int n = 9;
    triangle(maxheight(n));
}
}
// this code is contributed by princiraj1992

python 3

# python3 code for printing the
# triangle pattern using last term n
from math import sqrt
# function to demonstrate printing pattern
def triangle(n) :
    # number of spaces
    k = 2 * n - 2;
    # character to be printed
    ch = 1;
    # outer loop to handle number of rows
    # n in this case
    for i in range(n) :
        # inner loop to handle number spaces
        # values changing acc. to requirement
        for j in range(k) :
            print(" ", end = "");
        # decrementing k after each loop
        k = k - 1;
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(i   1) :
            # printing stars
            print(ch, end = " ");
            ch  = 1;
        # ending line after each row
        print()
# function to find the max height
# or the number of lines
# in the triangle pattern
def maxheight(n) :
    ans = (sqrt(1   8.0 * n) - 1) // 2;
    return int(ans);
# driver code
if __name__ == "__main__" :
    n = 9;
    triangle(maxheight(n));
# this code is contributed by ankitrai01

c

// c# code for printing the
// triangle pattern using last term n
using system;
class gfg
{
// function to demonstrate printing pattern
static void triangle(int n)
{
    // number of spaces
    int k = 2 * n - 2;
    // character to be printed
    int ch = 1;
    // outer loop to handle number of rows
    // n in this case
    for (int i = 0; i < n; i  )
    {
        // inner loop to handle number spaces
        // values changing acc. to requirement
        for (int j = 0; j < k; j  )
            console.write(" ");
        // decrementing k after each loop
        k = k - 1;
        // inner loop to handle number of columns
        // values changing acc. to outer loop
        for (int j = 0; j <= i; j  )
        {
            // printing stars
            console.write(ch     " ");
        }
        // ending line after each row
        console.writeline();
    }
}
// function to find the max height
// or the number of lines
// in the triangle pattern
static int maxheight(int n)
{
    return (((int)math.sqrt(1   8.0 * n)) - 1) / 2;
}
// driver code
public static void main(string[] args)
{
    int n = 9;
    triangle(maxheight(n));
}
}
// this code is contributed by princi singh

java 描述语言


output: 

    1 
   2 3 
  4 5 6