Stampa tutti i numeri di n cifre la cui somma delle cifre è uguale alla somma data
Dato il numero di cifre n stampa tutti i numeri di n cifre la cui somma delle cifre dà la somma data. La soluzione non dovrebbe considerare gli 0 iniziali come cifre.
Esempi:
Input: N = 2 Sum = 3
Output: 12 21 30
Input: N = 3 Sum = 6
Output: 105 114 123 132 141 150 204
213 222 231 240 303 312 321
330 402 411 420 501 510 600
Input: N = 4 Sum = 3
Output: 1002 1011 1020 1101 1110 1200
2001 2010 2100 3000
C++
UN soluzione semplice sarebbe generare tutti i numeri a N cifre e stampare i numeri la cui somma delle cifre è uguale alla somma data. La complessità di questa soluzione sarebbe esponenziale.
Una soluzione migliore è generare solo quei numeri di N cifre che soddisfano i vincoli specificati. L'idea è quella di utilizzare la ricorsione. Fondamentalmente riempiamo tutte le cifre da 0 a 9 nella posizione corrente e manteniamo la somma delle cifre finora. Quindi ricorsiamo per la somma rimanente e il numero di cifre rimaste. Gestiamo gli 0 iniziali separatamente poiché non vengono conteggiati come cifre.
Di seguito è riportata una semplice implementazione ricorsiva dell'idea di cui sopra:
Java// A C++ recursive program to print all n-digit // numbers whose sum of digits equals to given sum #includeusing namespace std ; // Recursive function to print all n-digit numbers // whose sum of digits equals to given sum // n sum --> value of inputs // out --> output array // index --> index of next digit to be filled in // output array void findNDigitNumsUtil ( int n int sum char * out int index ) { // Base case if ( index > n || sum < 0 ) return ; // If number becomes N-digit if ( index == n ) { // if sum of its digits is equal to given sum // print it if ( sum == 0 ) { out [ index ] = ' ' ; cout < < out < < ' ' ; } return ; } // Traverse through every digit. Note that // here we're considering leading 0's as digits for ( int i = 0 ; i <= 9 ; i ++ ) { // append current digit to number out [ index ] = i + '0' ; // recurse for next digit with reduced sum findNDigitNumsUtil ( n sum - i out index + 1 ); } } // This is mainly a wrapper over findNDigitNumsUtil. // It explicitly handles leading digit void findNDigitNums ( int n int sum ) { // output array to store N-digit numbers char out [ n + 1 ]; // fill 1st position by every digit from 1 to 9 and // calls findNDigitNumsUtil() for remaining positions for ( int i = 1 ; i <= 9 ; i ++ ) { out [ 0 ] = i + '0' ; findNDigitNumsUtil ( n sum - i out 1 ); } } // Driver program int main () { int n = 2 sum = 3 ; findNDigitNums ( n sum ); return 0 ; } Python 3// Java recursive program to print all n-digit // numbers whose sum of digits equals to given sum import java.io.* ; class GFG { // Recursive function to print all n-digit numbers // whose sum of digits equals to given sum // n sum --> value of inputs // out --> output array // index --> index of next digit to be // filled in output array static void findNDigitNumsUtil ( int n int sum char out [] int index ) { // Base case if ( index > n || sum < 0 ) return ; // If number becomes N-digit if ( index == n ) { // if sum of its digits is equal to given sum // print it if ( sum == 0 ) { out [ index ] = ' ' ; System . out . print ( out ); System . out . print ( ' ' ); } return ; } // Traverse through every digit. Note that // here we're considering leading 0's as digits for ( int i = 0 ; i <= 9 ; i ++ ) { // append current digit to number out [ index ] = ( char )( i + '0' ); // recurse for next digit with reduced sum findNDigitNumsUtil ( n sum - i out index + 1 ); } } // This is mainly a wrapper over findNDigitNumsUtil. // It explicitly handles leading digit static void findNDigitNums ( int n int sum ) { // output array to store N-digit numbers char [] out = new char [ n + 1 ] ; // fill 1st position by every digit from 1 to 9 and // calls findNDigitNumsUtil() for remaining positions for ( int i = 1 ; i <= 9 ; i ++ ) { out [ 0 ] = ( char )( i + '0' ); findNDigitNumsUtil ( n sum - i out 1 ); } } // driver program to test above function public static void main ( String [] args ) { int n = 2 sum = 3 ; findNDigitNums ( n sum ); } } // This code is contributed by Pramod Kumar# Python 3 recursive program to print # all n-digit numbers whose sum of # digits equals to given sum # Recursive function to print all # n-digit numbers whose sum of # digits equals to given sum # n sum --> value of inputs # out --> output array # index --> index of next digit to be # filled in output array def findNDigitNumsUtil ( n sum out index ): # Base case if ( index > n or sum < 0 ): return f = '' # If number becomes N-digit if ( index == n ): # if sum of its digits is equal # to given sum print it if ( sum == 0 ): out [ index ] = '