打印以下金字塔图案
给定一个正整数n 。问题是按照下面的示例中所述打印金字塔图案。
例子:
Input : n = 4
Output :
1
3*2
4*5*6
10*9*8*7
Input : n = 5
Output :
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
资料来源: Amdocs 面试经历 |设置 1
方法:对于奇数行,值以升序显示,对于偶数行,值以降序显示。唯一的另一个技巧是如何迭代循环。
算法:
printPattern(int n)
Declare j, k
Initialize k = 0
for i = 1 to n
if i%2 != 0
for j = k+1, j < k+i, j++
print j and "*"
print j and new line
k = ++j
else
k = k+i-1
for j = k, j > k-i+1, j--
print j and "*";
print j and new line
C++
// C++ implementation to print the following
// pyramid pattern
#include
using namespace std;
// function to print the following pyramid pattern
void printPattern(int n)
{
int j, k = 0;
// loop to decide the row number
for (int i=1; i<=n; i++)
{
// if row number is odd
if (i%2 != 0)
{
// print numbers with the '*' sign in
// increasing order
for (j=k+1; jk-i+1; j--)
cout << j << "*";
cout << j << endl;
}
}
}
// Driver program to test above
int main()
{
int n = 5;
printPattern(n);
return 0;
}
Java
// Java implementation to print the
// following pyramid pattern
public class Pyramid_Pattern {
// function to print the following pyramid
// pattern
static void printPattern(int n)
{
int j, k = 0;
// loop to decide the row number
for (int i = 1; i <= n; i++) {
// if row number is odd
if (i % 2 != 0) {
// print numbers with the '*'
// sign in increasing order
for (j = k + 1; j < k + i; j++)
System.out.print(j + "*");
System.out.println(j++);
// update value of 'k'
k = j;
}
// if row number is even
else {
// update value of 'k'
k = k + i - 1;
// print numbers with the '*' in
// decreasing order
for (j = k; j > k - i + 1; j--)
System.out.print(j + "*");
System.out.println(j);
}
}
}
// Driver program to test above
public static void main(String args[])
{
int n = 5;
printPattern(n);
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 implementation to print the
# following pyramid pattern
# function to print the
# following pyramid pattern
def printPattern(n) :
j, k = 0, 0
# loop to decide the row number
for i in range(1, n + 1) :
# if row number is odd
if i % 2 != 0 :
# print numbers with
# the '*' sign in
# increasing order
for j in range(k + 1, k + i) :
print(str(j) + "*",
end = "")
j = k + i
print(j)
j += 1
# update value of 'k'
k = j
# if row number is even
else :
# update value of 'k'
k = k + i - 1
# print numbers with the
# '*' in decreasing order
for j in range(k, k - i + 1, -1) :
print(str(j) + "*", end = "")
j = k - i + 1
print(j)
# Driver Code
if __name__ == "__main__" :
n = 5
# function calling
printPattern(n)
# This code is contributed
# by ANKITRAI1
C#
// C# implementation to print the
// following pyramid pattern
using System;
public class Pyramid_Pattern {
// function to print the following pyramid
// pattern
static void printPattern(int n)
{
int j, k = 0;
// loop to decide the row number
for (int i = 1; i <= n; i++) {
// if row number is odd
if (i % 2 != 0) {
// print numbers with the '*'
// sign in increasing order
for (j = k + 1; j < k + i; j++)
Console.Write(j + "*");
Console.WriteLine(j++);
// update value of 'k'
k = j;
}
// if row number is even
else {
// update value of 'k'
k = k + i - 1;
// print numbers with the '*' in
// decreasing order
for (j = k; j > k - i + 1; j--)
Console.Write(j + "*");
Console.WriteLine(j);
}
}
}
// Driver program to test above
public static void Main()
{
int n = 5;
printPattern(n);
}
}
// This code is contributed by vt_m.
PHP
$k - $i + 1; $j--)
echo $j . "*";
echo $j ."\n" ;
}
}
}
// Driver Code
$n = 5;
printPattern($n);
// This code is contributed by Sam007
?>
Javascript
输出:
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15