原文:
给定一个整数 n ,任务是打印给定的图案。 例:
input: 3
output:
1 2 4
3 5 7
6 8 9
input: 4
output:
1 2 4 7
3 5 8 11
6 9 12 14
10 13 15 16
进场:
- 创建一个大小为 n x n 的矩阵,该矩阵将在打印前存储图案。
- 将元素存储在图案的上部三角形中。正如所观察到的,当您向下移动对角线时,行索引增加 1,列索引减少 1。
- 一旦上三角形完成,则以与上三角形类似的方式存储下三角形的元素,即当您沿对角线向下移动时,行索引增加 1,列索引减少 1。
以下是上述方法的实现:
c
// c program to print the required pattern
#include
using namespace std;
// function to print the required pattern
void printpattern(int n)
{
// arr[][] will store the pattern matrix
int arr[n][n], k, i, j, p = 1, f;
// store the values for upper triangle
// of the pattern
for (k = 0; k < n; k ) {
j = k;
i = 0;
while (j >= 0) {
arr[i][j] = p;
p ;
i = i 1;
j = j - 1;
}
}
// store the values for lower triangle
// of the pattern
for (k = 1; k < n; k ) {
i = k;
j = n - 1;
f = k;
while (j >= f) {
arr[i][j] = p;
p ;
i = i 1;
j = j - 1;
}
}
// print the pattern
for (i = 0; i < n; i ) {
for (j = 0; j < n; j ) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
// driver code
int main()
{
int n = 3;
printpattern(n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print the required pattern
public class gfg{
// function to print the required pattern
static void printpattern(int n)
{
// arr[][] will store the pattern matrix
int arr[][] = new int[n][n] ;
int k, i, j, p = 1, f ;
// store the values for upper triangle
// of the pattern
for (k = 0; k < n; k ) {
j = k;
i = 0;
while (j >= 0) {
arr[i][j] = p;
p ;
i = i 1;
j = j - 1;
}
}
// store the values for lower triangle
// of the pattern
for (k = 1; k < n; k ) {
i = k;
j = n - 1;
f = k;
while (j >= f) {
arr[i][j] = p;
p ;
i = i 1;
j = j - 1;
}
}
// print the pattern
for (i = 0; i < n; i ) {
for (j = 0; j < n; j ) {
system.out.print(arr[i][j] " ") ;
}
system.out.println() ;
}
}
// driver code
public static void main(string []args)
{
int n = 3;
printpattern(n);
}
// this code is contributed by ryuga
}
python 3
# python 3 program to print the
# required pattern
# function to print the required pattern
def printpattern(n):
# arr[][] will store the pattern matrix
arr = [[0 for i in range(n)]
for j in range(n)]
p = 1
# store the values for upper
# triangle of the pattern
for k in range(n):
j = k
i = 0
while (j >= 0):
arr[i][j] = p
p = 1
i = i 1
j = j - 1
# store the values for lower triangle
# of the pattern
for k in range(1, n, 1):
i = k
j = n - 1
f = k
while (j >= f):
arr[i][j] = p
p = 1
i = i 1
j = j - 1
# print the pattern
for i in range(0, n, 1):
for j in range(0, n, 1):
print(arr[i][j], end = " ")
print("\n", end = "")
# driver code
if __name__ == '__main__':
n = 3
printpattern(n)
# this code is contributed by
# sanjit_prasad
c
// c# program to print the required pattern
using system;
public class gfg{
// function to print the required pattern
static void printpattern(int n)
{
// arr[][] will store the pattern matrix
int [,]arr = new int[n,n] ;
int k, i, j, p = 1, f ;
// store the values for upper triangle
// of the pattern
for (k = 0; k < n; k ) {
j = k;
i = 0;
while (j >= 0) {
arr[i,j] = p;
p ;
i = i 1;
j = j - 1;
}
}
// store the values for lower triangle
// of the pattern
for (k = 1; k < n; k ) {
i = k;
j = n - 1;
f = k;
while (j >= f) {
arr[i,j] = p;
p ;
i = i 1;
j = j - 1;
}
}
// print the pattern
for (i = 0; i < n; i ) {
for (j = 0; j < n; j ) {
console.write(arr[i,j] " ") ;
}
console.writeline() ;
}
}
// driver code
public static void main()
{
int n = 3;
printpattern(n);
}
// this code is contributed by inder_verma..
}
服务器端编程语言(professional hypertext preprocessor 的缩写)
= 0)
{
$arr[$i][$j] = $p;
$p ;
$i = $i 1;
$j = $j - 1;
}
}
// store the values for lower
// triangle of the pattern
for ($k = 1; $k < $n; $k )
{
$i = $k;
$j = $n - 1;
$f = $k;
while ($j >= $f)
{
$arr[$i][$j] = $p;
$p ;
$i = $i 1;
$j = $j - 1;
}
}
// print the pattern
for ($i = 0; $i < $n; $i )
{
for ($j = 0; $j < $n; $j )
{
echo($arr[$i][$j] . " ");
}
echo("\n");
}
}
// driver code
$n = 3;
printpattern($n);
// this code is contributed
// by mukul singh
?>
java 描述语言
output:
1 2 4
3 5 7
6 8 9
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处