📜  使用单个循环打印三角形星形图案

📅  最后修改于: 2022-05-13 01:57:59.383000             🧑  作者: Mango

使用单个循环打印三角形星形图案

给定一个数字N ,任务是在单循环中打印星形图案。

例子:

Input: N = 9
Output:                    
          * 
         * * 
        * * * 
       * * * * 
      * * * * * 
     * * * * * * 
    * * * * * * * 
   * * * * * * * * 
  * * * * * * * * * 

Input: N = 5
Output:
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 

请参阅文章以在两个循环中打印模式:
Java中的三角形模式

方法:这个想法是将一列分成三个部分,并独立于其他部分解决每个部分。

  • 案例 1:第一个 * 之前的空格,负责打印空格。
  • 情况 2:行中第一个 * 的开头和最后一个 * 的结尾,负责打印交替的空格和 *。
  • 案例 3:如果我们已经完成了 n 行,结束星本质上是告诉打印一个新行或结束程序。
    参考下图

下面是上述方法的实现:

C++
// C++ implementation of printing
// star pattern in single loop
 
#include 
using namespace std;
 
// Function to print the star
// pattern in single loop
void pattern(int n)
{
    int i, k, flag = 1;
   
    // Loop to handle number of rows and
    // columns in this case
    for (i = 1, k = 0; i <= 2 * n - 1; i++) {
        // Handles case 1
        if (i < n - k)
            cout << " ";
 
        // Handles case 2
        else {
            if (flag)
                cout << "*";
            else
                cout << " ";
 
            flag = 1 - flag;
        }
 
        // Condition to check case 3
        if (i == n + k) {
            k++;
            cout << endl;
 
            // Since for nth row we have
            // 2 * n- 1 columns
            if (i == 2 * n - 1)
                break;
 
            // Reinitializing i as 0,
            // for next row
            i = 0;
            flag = 1;
        }
    }
}
 
// Driver Code
int main()
{
    int n = 6;
   
    // Function Call
    pattern(n);
    return 0;
}


Java
// Java implementation of printing
// star pattern in single loop
import java.util.*;
 
class GFG{
 
// Function to print the star
// pattern in single loop
static void pattern(int n)
{
    int i, k, flag = 1;
     
    // Loop to handle number of rows and
    // columns in this case
    for(i = 1, k = 0; i <= 2 * n - 1; i++)
    {
         
        // Handles case 1
        if (i < n - k)
            System.out.print(" ");
 
        // Handles case 2
        else
        {
            if (flag == 1)
                System.out.print("*");
            else
                System.out.print(" ");
 
            flag = 1 - flag;
        }
 
        // Condition to check case 3
        if (i == n + k)
        {
            k++;
            System.out.println();
             
            // Since for nth row we have
            // 2 * n- 1 columns
            if (i == 2 * n - 1)
                break;
 
            // Reinitializing i as 0,
            // for next row
            i = 0;
            flag = 1;
        }
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 6;
     
    // Function Call
    pattern(n);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation of
# printing star pattern in
# single loop
 
# Function to print the star
# pattern in single loop
def pattern(n):
 
    flag = 1
 
    # Loop to handle number
    # of rows and columns
    # in this case
    i = 1
    k = 0
    while i <= 2 * n - 1:
       
        # Handles case 1
        if (i < n - k):
            print(" ", end = "")
 
        # Handles case 2
        else:
            if (flag):
                print("*", end = "")
            else:
                print(" ", end = "")
 
            flag = 1 - flag
 
        # Condition to check case 3
        if (i == n + k):
            k += 1
            print()
 
            # Since for nth row we
            # have 2 * n- 1 columns
            if (i == 2 * n - 1):
                break
 
            # Reinitializing i as 0,
            # for next row
            i = 0
            flag = 1
 
        i += 1
 
# Driver Code
if __name__ == "__main__":
 
    n = 6
 
    # Function Call
    pattern(n)
 
# This code is contributed by Chitranayal


C#
// C# implementation of printing
// star pattern in single loop
using System;
 
class GFG{
 
// Function to print the star
// pattern in single loop
static void pattern(int n)
{
    int i, k, flag = 1;
     
    // Loop to handle number of rows and
    // columns in this case
    for(i = 1, k = 0; i <= 2 * n - 1; i++)
    {
         
        // Handles case 1
        if (i < n - k)
            Console.Write(" ");
 
        // Handles case 2
        else
        {
            if (flag == 1)
                Console.Write("*");
            else
                Console.Write(" ");
 
            flag = 1 - flag;
        }
 
        // Condition to check case 3
        if (i == n + k)
        {
            k++;
            Console.WriteLine();
             
            // Since for nth row we have
            // 2 * n- 1 columns
            if (i == 2 * n - 1)
                break;
 
            // Reinitializing i as 0,
            // for next row
            i = 0;
            flag = 1;
        }
    }
}
 
// Driver code
public static void Main()
{
    int n = 6;
     
    // Function call
    pattern(n);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出
*
    * *
   * * *
  * * * *
 * * * * *
* * * * * *