From 0 to 24 repeatable choice algorithm topic: 10 equal the sum of the Numbers how many kind of permutation and combination of 24? Written in Java, online, etc
CodePudding user response:
It is also a dynamic programming?
CodePudding user response:
From big to small, pruning traversal
CodePudding user response:
Can save the intermediate results, that's the same with the dynamic programming
CodePudding user response:
With depth-first traversal + pruning, the following code
Public class Test { /* * * the number of desirable */ Private static int MAX_LEN=10; /* * A maximum of * optional */ Private static int MAX_VALUE=https://bbs.csdn.net/topics/24; /* * * expected value */ Private static int VALUE=https://bbs.csdn.net/topics/24; /* * * the results */ Private static long RESULT; /* * * save results to validate */ //private static int [] results=new int [MAX_LEN]; Public static void main (String [] args) { Next (0, 0); System. The out. Println (RESULT); } Public static void next (int sum, int currentLen) { If (currentLen + 1==MAX_LEN) { If (VALUE - sum & gt; 0 { RESULT++; //the results [currentLen]=VALUE - sum; //print (); } return; } for (int i=0; i //the results [currentLen]=I; Long currentResult=the RESULT; Next (sum + I, currentLen + 1); If (currentResult==RESULT) { return; } } } /* private static void print () { For (int the result: the results) { System. The out. Print (result + ""); } System.out.println(); } */ } CodePudding user response:
refer to the 4th floor ending requiem reply: with depth-first traversal + pruning, the following code But repeated selection: To: 0 0 0 0 0 0 0 0 0 24, Can also be used for: 0 0 0 0 0 0 0 0 1 23, Permutation and combination ah: 0 0 0 0 0 0 0 0 1 23, can also be used for 1 0 0 0 0 0 0 0 0 23, can also be used to 0 1 0 0 0 0 0 0 0 23, This is a math problem, don't need to code traversal of what, CodePudding user response:
reference 5 floor GKatHere reply: Quote: refer to the 4th floor ending requiem reply: With depth-first traversal + pruning, the following code But repeated selection: To: 0 0 0 0 0 0 0 0 0 24, Can also be used for: 0 0 0 0 0 0 0 0 1 23, Permutation and combination ah: 0 0 0 0 0 0 0 0 1 23, can also be used for 1 0 0 0 0 0 0 0 0 23, can also be used to 0 1 0 0 0 0 0 0 0 23, This is a math problem, don't need to code traversal of what, My algorithm cannot duplicate values CodePudding user response:
The answer is 240 CodePudding user response:
The answer is wrong should be 10 of 24 square I consider so A function F (x) from 0 to 24 repeatable choice for 10 x equal the sum of the Numbers of solution species F1=10 (choose a 1 out of 10 position so 10) Language is f2=f1 again pulled a combination of all such as 1 0 0... Choose a location and then ten position + 1 the sum of the Numbers is equal to 2 so species should be 10 * 10 Language is the f3=all combinations of f2 again drew a then ten position + 1 choose a position that number is equal to the sum of 3 so species should be hair f (2) * 10 . Fn=10 ^ n CodePudding user response:
refer to the 6th floor ending requiem reply: my algorithm can't repeat it values The result is wrong, /* * * the number of desirable */ The static const int MAX_LEN=10; /* * A maximum of * optional */ The static const int MAX_VALUE=https://bbs.csdn.net/topics/24; /* * * expected value */ The static const int VALUE=https://bbs.csdn.net/topics/24; /* * * the results */ The static long RESULT=0; //the number of iterations Static int RN_=0; /* * * save results to validate */ //static int [] results=new int [MAX_LEN]; Void next_ (int sum, int currentLen) { If (currentLen + 1==MAX_LEN) { If (VALUE - sum & gt; 0 { RESULT++; //the results [currentLen]=VALUE - sum; //print (); } RN_ + +; return; } for (int i=0; i //the results [currentLen]=I; Long currentResult=the RESULT; Next_ (sum + I, currentLen + 1); If (currentResult==RESULT) { return; } } } //violence traversal Int the rp () { Int s=0; # define N_N (_n) for (int n_ # # _n=0; N_ # # _n & lt;=MAX_VALUE; N_ + + # # _n) # define N_s (_n, _p) int sum_ # # _n=sum_ # # # # _p + n_ _n; # define B_s (_n) if (sum_ # # _n VALUE)=={s++; RN_ + +; break; } N_N (0) { Int sum_0=n_0; B_s (0); N_N (1) { N_s (1, 0); B_s (1); N_N (2) { N_s (2, 1); B_s (2); N_N (3) { N_s (3, 2); B_s (3); N_N (4) { N_s (4, 3); B_s (4); N_N (5) { N_s (5, 4); B_s (5); N_N (6) { N_s (6, 5); B_s (6); N_N (7) { N_s (7, 6); B_s (7); N_N (8) { N_s (8, 7); B_s (8); N_N (9) { N_s (9, 8); B_s (9); RN_ + +; } } } } } } } } } }; return s; } //mathematical reasoning //factorial Int fact (int n) { Int r=1; For (int m=1; M & lt;=n; M++) R *=m; return r; } //the specified array arrangement calculation //attention repeat number //should be: all the populations of different cases total number factorial/overlapping factorial Int st (int * v, int n) { Int * nb=new int [n]. nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull