📜  程序打印半钻石数字星型图案

📅  最后修改于: 2021-05-31 17:00:24             🧑  作者: Mango

给定一个代表行数的数字N。任务是打印一个半菱形的数字-星型图案,如以下示例所示。
注意:N始终是偶数。
例子

Input: N = 4
Output:  2*2
         1
         1
         2*2

Input: N = 6
Output: 3*3*3
        2*2
        1
        1
        2*2
        3*3*3

仔细观察上述模式,可以将其分解为两个不同的三角形:

  • 倒置的直角三角形。
  • 一个正常的直角三角形。

对于第一个三角形图案:

  • 运行两个循环,第一个将处理行数,另一个将处理被打印的’*’和数字。
  • 在循环内部,检查内部循环的迭代是否已达到给定的行数。
  • 如果是,则仅打印数字,否则打印“ *”并同时编号。

对于第二个三角形模式,将采用相同的方法,只是稍有变化,在此将检查内部迭代是否等于0的条件。
下面是上述方法的实现:

C++
// C++ program to print half diamond
// number star pattern
#include 
using namespace std;
 
// Function to print the given pattern
void printPattern(int r)
{
    r = r / 2;
    int count = r;
 
    // loop to print upper half pattern
    for (int i = 0; i < r; i++) {
        for (int j = r; j > i; j--) {
            if (j != r) {
                cout << "*" << count;
            }
            else {
                cout << count;
            }
        }
        count--;
        cout << endl;
    }
    count++;
 
    // loop to print lower half pattern
    for (int i = 0; i < r; i++) {
        for (int j = 0; j <= i; j++) {
            if (j != 0) {
                cout << "*" << count;
            }
            else {
                cout << count;
            }
        }
        count++;
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int n;
     
    n = 6;
     
    printPattern(n);
     
    return 0;
}


Java
// Java program to print half
// diamond number star pattern
class GFG
{
// Function to print
// the given pattern
static void printPattern(int r)
{
    r = r / 2;
    int count = r;
 
    // loop to print
    // upper half pattern
    for (int i = 0; i < r; i++)
    {
        for (int j = r; j > i; j--)
        {
            if (j != r)
            {
                System.out.print("*" + count);
            }
            else
            {
                System.out.print(count);
            }
        }
        count--;
        System.out.print("\n");
    }
    count++;
 
    // loop to print lower
    // half pattern
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            if (j != 0)
            {
                System.out.print("*" + count);
            }
            else
            {
                System.out.print(count);
            }
        }
        count++;
        System.out.print("\n");
    }
}
 
// Driver code
public static void main(String [] args)
{
    int n;
     
    n = 6;
     
    printPattern(n);
}
}
 
// This code is contributed
// by Smitha


Python 3
# Python 3 program to
# print half diamond
# number star pattern
 
# Function to print the
# given pattern
def printPattern(r) :
 
    r = r // 2
    count = r
 
    # loop to print upper
    # half pattern
    for i in range(r) :
        for j in range(r, i, -1) :
            if j != r :
                print("*" + str(count),
                             end = "")
            else :
                print(count, end = "")
 
        count -= 1
        print()
 
    count += 1
 
    # loop to print lower
    # half pattern
    for i in range(r) :
        for j in range(i + 1):
            if j != 0 :
                print("*" + str(count),
                             end = "")
            else :
                print(count, end = "")
 
        count += 1
        print()
 
# Driver Code
if __name__ == "__main__" :
 
    n = 6
 
    printPattern(n)
             
# This code is contributed
# by ANKITRAI1


C#
// C++ program to print half diamond
// number star pattern
 
using System;
 
public class GFG
{
// Function to print the given pattern
static void printPattern(int r)
{
    r = r / 2;
    int count = r;
  
    // loop to print upper half pattern
    for (int i = 0; i < r; i++) {
        for (int j = r; j > i; j--) {
            if (j != r) {
                Console.Write("*" + count);
            }
            else {
                Console.Write( count);
            }
        }
        count--;
        Console.WriteLine();
    }
    count++;
  
    // loop to print lower half pattern
    for (int i = 0; i < r; i++) {
        for (int j = 0; j <= i; j++) {
            if (j != 0) {
                Console.Write("*" + count);
            }
            else {
                Console.Write( count);
            }
        }
        count++;
        Console.WriteLine();
    }
}
  
// Driver code
public static void Main()
{
    int n;
      
    n = 6;
      
    printPattern(n);
      
}
}


PHP
 $i; $j--)
        {
            if ($j != $r)
            {
                echo "*" . $count;
            }
            else
            {
                echo $count;
            }
        }
        $count--;
        echo "\n";
    }
    $count++;
 
    // loop to print lower half pattern
    for ($i = 0; $i < $r; $i++)
    {
        for ($j = 0; $j <= $i; $j++)
        {
            if ($j != 0)
            {
                echo "*" . $count;
            }
            else
            {
                echo $count;
            }
        }
        $count++;
        echo "\n";
    }
}
 
// Driver code
$n = 6;
 
printPattern($n);
 
// This code is contributed
// by Arnab Kundu
?>


Javascript


输出:
3*3*3
2*2
1
1
2*2
3*3*3
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”