博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DFS——选数问题
阅读量:4216 次
发布时间:2019-05-26

本文共 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/

你可能感兴趣的文章
leetcode26. [Array]Remove Duplicates from Sorted Array
查看>>
leetcode 27. [Array]Remove Element
查看>>
leetcode66.[Array] Plus One
查看>>
leetcode283. [Array]Move Zeroes My Submissions Question
查看>>
leetcode292.[Array] Nim Game
查看>>
推荐系统简述(2)基于近邻推荐方法
查看>>
leetcode171.[math] Excel Sheet Column Number
查看>>
决策树知识点整理
查看>>
支持向量机知识点整理
查看>>
机器学习面试题
查看>>
聚类算法知识点整理
查看>>
深度学习知识点整理
查看>>
Logistic Regression知识点整理
查看>>
Boosting、Bagging和Stacking知识点整理
查看>>
EM算法知识点整理
查看>>
优化算法知识点整理
查看>>
缺失值处理知识点整理
查看>>
剑指Offer题解(Python版)
查看>>
Leetcode 链表知识点总结
查看>>
Leetcode 位运算知识点总结
查看>>