2010 Chung-Hsing Face test
Programming Solution:
Enter two integers n and M, take a few numbers from the sequence 1,2,3.......N,
making it and equal to m requires that all possible combinations of them be listed.
Recursive method of 21 questions
[Email protected] July && Yansha
July, yansha,updated.
#include <list>
#include <iostream>
using namespace Std;
list<int>list1;
void find_factor (int sum, int n)
{
Recursive exit
if (n <= 0 | | sum <= 0)
Return
Outputs the results found
if (sum = = N)
{
Invert list
List1.reverse ();
for (List<int>::iterator iter = List1.begin (); ITER! = List1.end (); iter++)
cout << *iter << "+";
cout << n << Endl;
List1.reverse ();
}
List1.push_front (n); A typical 01 knapsack problem
Find_factor (Sum-n, n-1); Put n,n-1 number to fill sum-n
List1.pop_front ();
Find_factor (sum, n-1); Do not put n,n-1 number to fill sum
}
int main ()
{
int sum, n;
cout << "Please enter the number you want equals sum:" << Endl;
CIN >> sum;
cout << "Please enter the n that you want to value from the 1.....N series:" << Endl;
CIN >> N;
cout << "All possible sequences, as follows:" << Endl;
Find_factor (Sum,n);
return 0;
}
Logic Analysis:
1, compared to Microsoft, Google, Baidu these companies, ZTE's face test is slightly more than the tease, not to say the difference in difficulty, but ZTE's topic always appear nondescript. The subject is actually the combination of the number of studies, for such problems, the usual means are recursive, and our goal is to find the recursive type.
2, the problem is essentially 0/1 knapsack problem, for each n, we use greedy strategy, first investigate whether to take N, if take n, then the sub-problem becomes find (n-1,m-n), and if you discard N, sub-problem is find (n-1,m). So far, we have used DP thought to find recursion (many times, so-called dynamic programming, greed is just an idea away).
3, then, how to formulate the solution of the decision strategy? We know that recursion requires boundary conditions, and for the knapsack problem, there are only two boundary conditions, if n<1 or m<1, then it is equivalent to "overflow", can not combo out m, and the other may be in the remaining n is exactly satisfied with the m==n, that is, the backpack is just filled with a full, Outputs a set of solution units. Besides, there is nothing else.
C Source:
#include <stdio.h> #include <stdlib.h> #include <string.h>int length;void findcombination (int n,int m , int *flag) {if (N < 1 | | m < 1) return;if (n > m) n = m;if (n = = m) {flag[n-1] = 1;for (int i=0;i<length;i++) {if (flag [i] = = 1) printf ("%d\t", i+1);} printf ("\ n"); flag[n-1] = 0;} Flag[n-1] = 1;findcombination (N-1,m-n,flag); flag[n-1] = 0;findcombination (N-1,m,flag);} int main () {int n, m;scanf ("%d%d", &n,&m), length = N;int *flag = (int*) malloc (sizeof (int) *length); Findcombination (N,m,flag); free (flag); return 0;}
Note: We set the flag backpack to indicate whether the corresponding N+1 is selected, 1 is selected, 0 is unchecked, and a set of solutions is output whenever m==n is satisfied. The place where a program is prone to logic bugs is the use of length (the reader can think about why the global variable length is needed instead of using n directly instead of a for loop).
[algorithm] to find and to set the number of values