给定一个数字为’num’,行数为’num_of_lines’,其中’num’表示图案必须从中开始的起始编号,’num_of_lines’表示必须打印的行数。现在,根据以上信息,打印一个图案,如下所示。 例如:
null
Input: num = 7, num_of_lines = 4Output: 7 14 15 28 29 30 31 56 57 58 59 60 61 62 63Input: num = 3, num_of_lines = 3Output: 3 6 7 12 13 14 15
观察:
- 第一列的元素是该列中前一个元素的倍数。
- 每行中的元素数是前一行中元素数的两倍。
- 此外,要生成行中的下一个元素,请将1添加到该行的上一个元素。
方法: 因此,从0到num_of_line-1开始一个循环,以考虑要打印的行数,并在第一个循环中从0到limit-1开始另一个循环,limit将被初始化为1,其值呈指数增长。现在在循环中,只需将数字增加1,即可打印该行的下一个数字。
C++
// C++ program to print the // given numeric pattern #include <bits/stdc++.h> using namespace std; // Function to print th epattern void printPattern ( int num, int numOfLines ) { int n = num, num2, x = 1, limit = 1; // No. of rows to be printed for ( int i = 0; i < numOfLines; i++) { // No. of elements to be printed in each row for ( int j = 0; j < limit; j++) { if (j == 0) num2 = num; // Print the element cout << num2++ << " " ; } num *= 2; limit = num / n; cout << endl; } } // Drivers code int main() { int num = 3; int numOfLines = 3; printPattern(num, numOfLines); return 0; } |
JAVA
// Java program to print the // given numeric pattern class solution_1 { // Function to print // the pattern static void printPattern ( int num, int numOfLines) { int n = num, num2 = 0 , x = 1 , limit = 1 ; // No. of rows to // be printed for ( int i = 0 ; i < numOfLines; i++) { // No. of elements to be // printed in each row for ( int j = 0 ; j < limit; j++) { if (j == 0 ) num2 = num; // Print the element System.out.print(num2++ + " " ); } num *= 2 ; limit = num / n; System.out.println(); } } // Driver code public static void main(String args[]) { int num = 3 ; int numOfLines = 3 ; printPattern(num, numOfLines); } } // This code is contributed // by Arnab Kundu |
Python 3
# Python 3 program to print # the given numeric pattern # Function to print th epattern def printPattern (num, numOfLines ): n = num limit = 1 # No. of rows to be printed for i in range ( 0 , numOfLines): # No. of elements to be # printed in each row for j in range (limit): if j = = 0 : num2 = num # Print the element print (num2, end = " " ) num2 + = 1 num * = 2 limit = num / / n print () # Driver code if __name__ = = "__main__" : num = 3 numOfLines = 3 printPattern(num, numOfLines) # This code is contributed # by ChitraNayal |
C#
// C# program to print the // given numeric pattern using System; class GFG { // Function to print // the pattern static void printPattern( int num, int numOfLines) { int n = num, num2 = 0, limit = 1; // No. of rows to // be printed for ( int i = 0; i < numOfLines; i++) { // No. of elements to be // printed in each row for ( int j = 0; j < limit; j++) { if (j == 0) num2 = num; // Print the element Console.Write(num2++ + " " ); } num *= 2; limit = num / n; Console.Write( "" ); } } // Driver code public static void Main() { int num = 3; int numOfLines = 3; printPattern(num, numOfLines); } } // This code is contributed by Smitha |
PHP
<?php // PHP program to print the // given numeric pattern // Function to print th epattern function printPattern( $num , $numOfLines ) { $n = $num ; $limit = 1; // No. of rows to be printed for ( $i = 0; $i < $numOfLines ; $i ++) { // No. of elements to be // printed in each row for ( $j = 0; $j < $limit ; $j ++) { if ( $j == 0) $num2 = $num ; // Print the element echo $num2 ++ . " " ; } $num *= 2; $limit = $num / $n ; echo "" ; } } // Driver code $num = 3; $numOfLines = 3; printPattern( $num , $numOfLines ); // This code is contributed // by ChitraNayal ?> |
Javascript
<script> // JavaScript program to print the // given numeric pattern // Function to print th epattern function printPattern(num, numOfLines) { var n = num, num2, x = 1, limit = 1; // No. of rows to be printed for ( var i = 0; i < numOfLines; i++) { // No. of elements to be // printed in each row for ( var j = 0; j < limit; j++) { if (j == 0) num2 = num; // Print the element document.write(num2++ + " " ); } num *= 2; limit = num / n; document.write( "<br>" ); } } // Drivers code var num = 3; var numOfLines = 3; printPattern(num, numOfLines); </script> |
输出:
3 6 7 12 13 14 15
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END