给定两个正整数n和k,打印所有长度为k的递增序列,使每个序列中的元素来自前n个自然数。
null
例如:
Input: k = 2, n = 3Output: 1 2 1 3 2 3 Input: k = 5, n = 5Output: 1 2 3 4 5Input: k = 3, n = 5Output: 1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
我们强烈建议尽量减少浏览器,并先自己尝试。 这是一个很好的递归问题。其想法是创建一个长度为k的数组。该数组存储当前序列。对于数组中的每个位置,我们检查前一个元素,并逐个放置所有大于前一个元素的元素。如果没有前一个元素(第一个位置),我们将所有数字从1到n。
以下是上述理念的实施:
C++
// C++ program to print all increasing sequences of // length 'k' such that the elements in every sequence // are from first 'n' natural numbers. #include<iostream> using namespace std; // A utility function to print contents of arr[0..k-1] void printArr( int arr[], int k) { for ( int i=0; i<k; i++) cout << arr[i] << " " ; cout << endl; } // A recursive function to print all increasing sequences // of first n natural numbers. Every sequence should be // length k. The array arr[] is used to store current // sequence. void printSeqUtil( int n, int k, int &len, int arr[]) { // If length of current increasing sequence becomes k, // print it if (len == k) { printArr(arr, k); return ; } // Decide the starting number to put at current position: // If length is 0, then there are no previous elements // in arr[]. So start putting new numbers with 1. // If length is not 0, then start from value of // previous element plus 1. int i = (len == 0)? 1 : arr[len-1] + 1; // Increase length len++; // Put all numbers (which are greater than the previous // element) at new position. while (i<=n) { arr[len-1] = i; printSeqUtil(n, k, len, arr); i++; } // This is important. The variable 'len' is shared among // all function calls in recursion tree. Its value must be // brought back before next iteration of while loop len--; } // This function prints all increasing sequences of // first n natural numbers. The length of every sequence // must be k. This function mainly uses printSeqUtil() void printSeq( int n, int k) { int arr[k]; // An array to store individual sequences int len = 0; // Initial length of current sequence printSeqUtil(n, k, len, arr); } // Driver program to test above functions int main() { int k = 3, n = 7; printSeq(n, k); return 0; } |
JAVA
// Java program to print all // increasing sequences of // length 'k' such that the // elements in every sequence // are from first 'n' // natural numbers. class GFG { // A utility function to print // contents of arr[0..k-1] static void printArr( int [] arr, int k) { for ( int i = 0 ; i < k; i++) System.out.print(arr[i] + " " ); System.out.print( "" ); } // A recursive function to print // all increasing sequences // of first n natural numbers. // Every sequence should be // length k. The array arr[] is // used to store current sequence static void printSeqUtil( int n, int k, int len, int [] arr) { // If length of current increasing // sequence becomes k, print it if (len == k) { printArr(arr, k); return ; } // Decide the starting number // to put at current position: // If length is 0, then there // are no previous elements // in arr[]. So start putting // new numbers with 1. // If length is not 0, // then start from value of // previous element plus 1. int i = (len == 0 ) ? 1 : arr[len - 1 ] + 1 ; // Increase length len++; // Put all numbers (which are // greater than the previous // element) at new position. while (i <= n) { arr[len - 1 ] = i; printSeqUtil(n, k, len, arr); i++; } // This is important. The // variable 'len' is shared among // all function calls in recursion // tree. Its value must be // brought back before next // iteration of while loop len--; } // This function prints all // increasing sequences of // first n natural numbers. // The length of every sequence // must be k. This function // mainly uses printSeqUtil() static void printSeq( int n, int k) { // An array to store // individual sequences int [] arr = new int [k]; // Initial length of // current sequence int len = 0 ; printSeqUtil(n, k, len, arr); } // Driver Code static public void main (String[] args) { int k = 3 , n = 7 ; printSeq(n, k); } } // This code is contributed by Smitha. |
Python3
# Python3 program to print all # increasing sequences of length # 'k' such that the elements in # every sequence are from first # 'n' natural numbers. # A utility function to # print contents of arr[0..k-1] def printArr(arr, k): for i in range (k): print (arr[i], end = " " ); print (); # A recursive function to print # all increasing sequences of # first n natural numbers. Every # sequence should be length k. # The array arr[] is used to # store current sequence. def printSeqUtil(n, k,len1, arr): # If length of current # increasing sequence # becomes k, print it if (len1 = = k): printArr(arr, k); return ; # Decide the starting number # to put at current position: # If length is 0, then there # are no previous elements # in arr[]. So start putting # new numbers with 1. If length # is not 0, then start from value # of previous element plus 1. i = 1 if (len1 = = 0 ) else (arr[len1 - 1 ] + 1 ); # Increase length len1 + = 1 ; # Put all numbers (which are greater # than the previous element) at # new position. while (i < = n): arr[len1 - 1 ] = i; printSeqUtil(n, k, len1, arr); i + = 1 ; # This is important. The variable # 'len' is shared among all function # calls in recursion tree. Its value # must be brought back before next # iteration of while loop len1 - = 1 ; # This function prints all increasing # sequences of first n natural numbers. # The length of every sequence must be # k. This function mainly uses printSeqUtil() def printSeq(n, k): arr = [ 0 ] * k; # An array to store # individual sequences len1 = 0 ; # Initial length of # current sequence printSeqUtil(n, k, len1, arr); # Driver Code k = 3 ; n = 7 ; printSeq(n, k); # This code is contributed by mits |
C#
// C# program to print all // increasing sequences of // length 'k' such that the // elements in every sequence // are from first 'n' // natural numbers. using System; class GFG { // A utility function to print // contents of arr[0..k-1] static void printArr( int [] arr, int k) { for ( int i = 0; i < k; i++) Console.Write(arr[i] + " " ); Console.WriteLine(); } // A recursive function to print // all increasing sequences // of first n natural numbers. // Every sequence should be // length k. The array arr[] is // used to store current sequence static void printSeqUtil( int n, int k, int len, int [] arr) { // If length of current increasing // sequence becomes k, print it if (len == k) { printArr(arr, k); return ; } // Decide the starting number // to put at current position: // If length is 0, then there // are no previous elements // in arr[]. So start putting // new numbers with 1. // If length is not 0, // then start from value of // previous element plus 1. int i = (len == 0) ? 1 : arr[len - 1] + 1; // Increase length len++; // Put all numbers (which are // greater than the previous // element) at new position. while (i <= n) { arr[len - 1] = i; printSeqUtil(n, k, len, arr); i++; } // This is important. The // variable 'len' is shared among // all function calls in recursion // tree. Its value must be // brought back before next // iteration of while loop len--; } // This function prints all // increasing sequences of // first n natural numbers. // The length of every sequence // must be k. This function // mainly uses printSeqUtil() static void printSeq( int n, int k) { // An array to store // individual sequences int [] arr = new int [k]; // Initial length of // current sequence int len = 0; printSeqUtil(n, k, len, arr); } // Driver Code static public void Main () { int k = 3, n = 7; printSeq(n, k); } } // This code is contributed by Ajit. |
PHP
<?php // PHP program to print all // increasing sequences of // length 'k' such that the // elements in every sequence // are from first 'n' natural // numbers. // A utility function to // print contents of arr[0..k-1] function printArr( $arr , $k ) { for ( $i = 0; $i < $k ; $i ++) echo $arr [ $i ], " " ; echo "" ; } // A recursive function to // print all increasing // sequences of first n // natural numbers. Every // sequence should be length // k. The array arr[] is used // to store current sequence. function printSeqUtil( $n , $k , $len , $arr ) { // If length of current // increasing sequence // becomes k, print it if ( $len == $k ) { printArr( $arr , $k ); return ; } // Decide the starting number // to put at current position: // If length is 0, then there // are no previous elements // in arr[]. So start putting // new numbers with 1. If length // is not 0, then start from value // of previous element plus 1. $i = ( $len == 0)? 1 : $arr [ $len - 1] + 1; // Increase length $len ++; // Put all numbers (which are // greater than the previous // element) at new position. while ( $i <= $n ) { $arr [ $len -1] = $i ; printSeqUtil( $n , $k , $len , $arr ); $i ++; } // This is important. The // variable 'len' is shared // among all function calls // in recursion tree. Its // value must be brought back // before next iteration of // while loop $len --; } // This function prints all // increasing sequences of // first n natural numbers. // The length of every sequence // must be k. This function // mainly uses printSeqUtil() function printSeq( $n , $k ) { $arr = array (); // An array to store // individual sequences $len = 0; // Initial length of // current sequence printSeqUtil( $n , $k , $len , $arr ); } // Driver Code $k = 3; $n = 7; printSeq( $n , $k ); // This code is contributed by Ajit ?> |
Javascript
<script> // Javascript program to print all // increasing sequences of // length 'k' such that the // elements in every sequence // are from first 'n' // natural numbers. // A utility function to print // contents of arr[0..k-1] function printArr(arr, k) { for (let i = 0; i < k; i++) document.write(arr[i] + " " ); document.write( "</br>" ); } // A recursive function to print // all increasing sequences // of first n natural numbers. // Every sequence should be // length k. The array arr[] is // used to store current sequence function printSeqUtil(n, k, len, arr) { // If length of current increasing // sequence becomes k, print it if (len == k) { printArr(arr, k); return ; } // Decide the starting number // to put at current position: // If length is 0, then there // are no previous elements // in arr[]. So start putting // new numbers with 1. // If length is not 0, // then start from value of // previous element plus 1. let i = (len == 0) ? 1 : arr[len - 1] + 1; // Increase length len++; // Put all numbers (which are // greater than the previous // element) at new position. while (i <= n) { arr[len - 1] = i; printSeqUtil(n, k, len, arr); i++; } // This is important. The // variable 'len' is shared among // all function calls in recursion // tree. Its value must be // brought back before next // iteration of while loop len--; } // This function prints all // increasing sequences of // first n natural numbers. // The length of every sequence // must be k. This function // mainly uses printSeqUtil() function printSeq(n, k) { // An array to store // individual sequences let arr = new Array(k); // Initial length of // current sequence let len = 0; printSeqUtil(n, k, len, arr); } // Driver code let k = 3, n = 7; printSeq(n, k); // This code is contributed by divyesh072019 </script> |
输出:
1 2 31 2 41 2 51 2 61 2 71 3 41 3 51 3 61 3 71 4 51 4 61 4 71 5 61 5 71 6 72 3 42 3 52 3 62 3 72 4 52 4 62 4 72 5 62 5 72 6 73 4 53 4 63 4 73 5 63 5 73 6 74 5 64 5 74 6 75 6 7
本文由 阿琼 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END