void print_combination(int n) { //Initialize the stack at the first time if (stack == NULL) { stack = malloc(sizeof(int)*n); } if(n <= 0 && top >=2) { int i = 0; for(i=0; i<top; i++) { printf("%d, ", stack[i]); } printf("\n"); return; } else if(n <=0) { return; } stack[top++] = n; print_combination(n-1); top--; print_combination(n-1); }
int main(int argc, char const *argv[]) { int chars[4]; int visited[4]; int i = 0; for(;i<4;i++){ chars[i] = i+1; visited[i] = i+1; } print_combination(chars, 4, 2, visited, 0); return 0; }
/* how many combinations can be, the combination length will be m example: chars: [1,2,3,4] len: 4 step: 2 print result: 12,13,14,23,24,34 */ void print_combination(int* chars, int len, int m, int* visitedIdx, int visitedLen){ int i=0; if(m == 0){ for(i=0;i<visitedLen;i++){ printf("%d", chars[visitedIdx[i]]); } printf("\n"); return; } int smallest = visitedLen == 0 ? 0 : visitedIdx[visitedLen-1] + 1; for(i=smallest;i<len;i++){ visitedIdx[visitedLen] = i; print_combination(chars, len, m-1, visitedIdx, visitedLen+1); } }
void Print( const Value* value, std::ostream& result ) { // TODO: switch on typeid? if( !value ) { return; } const IntegerValue* integervalue = dynamic_cast<const IntegerValue*>( value ); if( integervalue ) { print_integer( integervalue, result ); return; } const DecimalValue* decimalvalue = dynamic_cast<const DecimalValue*>( value ); if( decimalvalue ) { print_decimal( decimalvalue, result ); return; } const StringValue* stringvalue = dynamic_cast<const StringValue*>( value ); if( stringvalue ) { print_string( stringvalue, result ); return; } const TrueValue* truevalue = dynamic_cast<const TrueValue*>( value ); if( truevalue ) { print_true( truevalue, result ); return; } const FalseValue* falsevalue = dynamic_cast<const FalseValue*>( value ); if( falsevalue ) { print_false( falsevalue, result ); return; } const NilValue* nilvalue = dynamic_cast<const NilValue*>( value ); if( nilvalue ) { print_nil( nilvalue, result ); return; } const CombinationValue* combovalue = dynamic_cast< const CombinationValue*>( value ); if( combovalue ) { print_combination( combovalue, result ); return; } const SymbolValue* symbolvalue = dynamic_cast< const SymbolValue*>( value ); if( symbolvalue ) { print_symbol( symbolvalue, result ); return; } const NativeFunctionValue* fnvalue = dynamic_cast< const NativeFunctionValue*>( value ); if( fnvalue ) { print_built_in_procedure( fnvalue, result ); return; } const CompoundProcedureValue* procvalue = dynamic_cast< const CompoundProcedureValue*>( value ); if( procvalue ) { print_compound_procedure( procvalue, result ); return; } const PairValue* pairvalue = dynamic_cast< const PairValue*>( value ); if( pairvalue ) { print_pair( pairvalue, result ); return; } result << "<<UNPRINTABLE TYPE " << typeid(value).name() << ">>"; }
int main() { print_combination(16); free(stack); }