本文共 596 字,大约阅读时间需要 1 分钟。
#include#include using namespace std;const int maxn = 30;int n, k, x, maxSumSqu = -1, A[maxn];vector temp;vector ans;void DFS(int index, int nowK, int sum, int sumSqu){ if(nowK == k && sum == x) { if(sumSqu > maxSumSqu) { maxSumSqu = sumSqu; ans = temp; } return; } if(index > n || nowK > k || sum > x) return; // 岔路口 —— 选 temp.push_back(A[index]); DFS(index + 1, nowK + 1, sum + A[index], sumSqu + A[index] * A[index]); temp.pop_back(); // 岔路口 —— 不选 DFS(index + 1, nowK, sum, sumSqu); }int main(){ // ...... return 0;}
转载地址:http://nhtmi.baihongyu.com/