Dowemo

Number of combinations

Time limit: 3000 Ms | memory limit: 65535 Kb difficulty: 3 Describes all combinations of the number of R ( 0 <R <= n ) from the number 1, 2. N ( 0 <n <10 ). Enter input n, R. Outputs all combinations in a specific order.
A specific order: the values in each combination are sorted from large to large, arranged by inverse dictionary. Sample input

5 3





Sample output

543


542


541


532


531


521


432


431


421


321










#include <stdio.h>


#include <string.h>


#include <math.h>


int flag[10];


int n, r;



void dfs(int place, int rule, int num)//非全排列,保持高位始终大于低位


{


 if (place == 0)


 {


 printf("%dn", num);


 }


 for (int i = rule; i> 0; i--)


 {


 if (!flag[i])


 {


 flag[i] = 1;


 dfs(place - 1, i - 1, num + i * pow(10, place - 1));


 flag[i] = 0;


 }


 }


 return ;


}



int main()


{


 while (~scanf("%d %d", &n, &r))


 {


 memset(flag, 0, sizeof(flag));



 dfs(r, n, 0);


 }


 return 0;


}










Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs