POJ-2755-神奇的口袋

一、题目

2755:神奇的口袋

时间限制: 10000ms 内存限制: 65536kB
描述
有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。John可以从这些物品中选择一些,如果选出的物体的总体积是40,那么利用这个神奇的口袋,John就可以得到这些物品。现在的问题是,John有多少种不同的选择物品的方式。
输入
输入的第一行是正整数n (1 <= n <= 20),表示不同的物品的数目。接下来的n行,每行有一个1到40之间的正整数,分别给出a1,a2……an的值。
输出
输出不同的选择物品的方式的数目。
样例输入
3
20
20
20
样例输出
3


二、分析

   尝试每种可能情况找出符合条件的
1、使用全局变量,递归法求解
2、动态规划法

三、AC源代码

 1、递归求解:

 1 #include <stdio.h>
2 int n,ans=0,type[20];
3 bool choose[20];
4
5 void dfs(int);
6 int sum();
7
8 int main()
9 {
10 int i;
11 scanf("%d",&n);
12 for(i=0;i<n;i++)
13 scanf("%d",&type[i]);
14 dfs(0);
15 printf("%d\n",ans);
16 return 0;
17 }
18
19 void dfs(int b)
20 {
21 if(b==n)
22 {
23 if(sum()==40)
24 ans++;
25 return;
26 }
27 else
28 {
29 choose[b]=false;
30 dfs(b+1);
31 choose[b]=true;
32 dfs(b+1);
33 }
34 }
35
36 int sum()
37 {
38 int i,s=0;
39 for(i=0;i<n;i++)
40 if(choose[i])
41 s+=type[i];
42 return s;
43 }

2、动态规划:

 1 #include<stdio.h>
2 #include<string.h>
3 int main()
4 {
5 int n,i;
6 int num[50],a[30];
7 scanf("%d",&n);
8 for(i=0;i<n;i++)
9 scanf("%d",&a[i]);
10 memset(num,0,sizeof(num));
11 for(i=0;i<n;i++)
12 {
13 int k=40-a[i];
14 while(k>=0)
15 {
16 if(num[k]>0)
17 num[k+a[i]]+=num[k];
18 k--;
19 }
20 num[a[i]]++;
21 }
22 printf("%d\n",num[40]);
23 return 0;
24 }





转载于:https://www.cnblogs.com/deadacm/archive/2011/12/18/2292066.html

手把手教你用Habitat-Lab YAML配置文件,5分钟定制你的第一个Embodied AI智能体(含代码片段) 本文详细介绍了如何使用Habitat-Lab的YAML配置文件快速定制Embodied AI智能体。从基础配置到传感器系统进阶设置,再到训练参数优化和调试技巧,手把手教你5分钟内完成第一个智能体的配置(含代码片段)。特别解析了CONFIG_KEYS等关键配置项,帮助开发者高效掌握Habitat-Lab框架。 阅读详情

相关推荐

OpenCV图像拼接实战:用SIFT+透视变换打造无缝全景图(附完整代码)

本文详细介绍了使用OpenCV和SIFT算法实现图像拼接的全过程,包括特征匹配、透视变换和高级融合技术。通过实战代码演示,帮助开发者掌握无缝全景图生成的核心技术,适用于旅行摄影、无人机航拍等多种场景。

eee77的博客 1007

OpenJudge 2.7 1757 神奇的口袋

前i个物品容积和为j的方法数 = 前i-1个物品容积和为j-a[i]的方法数 + 前i-1个物品容积和为j的方法数(即算上当前容积a[i]和不算当前容积)a[i]为第i个物品的容积,dp[i][j]表示为前i个物品容积和为j的方法数,dp[0][0]----dp[n][0]均初始化为1。列出方程:dp[i][j] = dp[i-1][j-a[i]] + dp[i-1][j]考虑到j-a[i]可能小于零,因此应该加入判定条件。

m0_74257019的博客 271

【开源~磁悬浮】零代码,纯硬件闭环:从霍尔到H桥的磁悬浮稳定术

本文详细介绍了如何不依赖任何代码,仅通过模拟和数字电路的组合,实现一个完整的磁悬浮闭环控制系统。从霍尔传感器检测位置,到运放电路进行信号调理与模拟PID控制,再到H桥驱动电磁铁执行纠偏,全程使用基础电子元器件搭建。文章深入剖析了系统原理、电路设计、调试技巧与实践经验,展现了纯硬件实现自动控制的独特魅力与挑战。

perl8的博客 632

百练 2755 神奇的口袋

描述 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。John可以从这些物品中选择一些,如果选出的物体的总体积是40,那么利用这个神奇的口袋,John就可以得到这些物品。现在的问题是,John有多少种不同的选择物品的方式。 输入 输入的第一行是正整数n (1 <= n <...

qq_32955607的博客 633

Ubuntu18.04环境搭建与疑难解析:从零复现YOLO-6D姿态估计算法

本文详细介绍了在Ubuntu18.04环境下从零开始复现YOLO-6D姿态估计算法的完整流程,包括系统配置、Conda环境搭建、PyTorch安装、依赖项管理、源代码获取、数据集准备以及训练验证过程。针对Singleshotpose算法的特殊需求,提供了详细的疑难解析和性能优化技巧,帮助开发者高效完成算法复现和应用部署。

weixin_30369041的博客 458

HSmartWindowControl vs HWindowControl:Halcon老鸟教你如何根据项目需求选对控件(附性能对比)

本文深度对比Halcon图像处理中的HSmartWindowControl与HWindowControl控件,从性能、内存占用和交互体验等维度提供选型建议。HSmartWindowControl在硬件加速渲染、智能内存管理方面表现优异,特别适合医疗影像和工业检测等高性能需求场景,而HWindowControl则更适合兼容旧系统和资源受限环境。

weixin_30765505的博客 473

题目1114:神奇的口袋

题目源地址:http://ac.jobdu.com/problem.php?pid=1114 C++代码: #include int buf[21]; int ans; bool mark[21]; int n; void dfs(int i,int sum) { if(sum==0) { ans++; return ; } if(i>=n||sum>40) return;

886

【sap2000】部分模型通过.s2k文件合并模型generating a global model through partial models by .s2k text file

现在需要通过选项File → Import → SAP2000 .s2k Text File → Add to existing model(如果使用其中一个部分模型作为基础)或New model(如果使用新文件)添加部分模型,并使用以下导入选项。模型A(阶段1/5) Model A (Phase 1/5)模型B(阶段2/5) Model B (Phase 2/5)模型C(阶段3/5) Model C (Phase 3/5)模型E(阶段5/5) Model E (Phase 5/5)

hmywillstronger的博客 1764

神奇的口袋(递归动规)

题目描述 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。John可以从这些物品中选择一些,如果选出的物体的总体积是40,那么利用这个神奇的口袋,John就可以得到这些物品。现在的问题是,John有多少种不同的选择物品的方式。 输入 输入的第一行是正整数n (1 <= n &l...

qq_42818329的博客 453

SDR 和 DDR 的区别

SDR 和 DDR 的差别,最明显提现在传输速率和频率上。DDR SDRAM与SDRAM的基本结构是相似的,最根本的区别在于DDR 支持在一个时钟周期内传输两次数据,,SDR 只能传输一次数据。

串起点滴 1963

医学图像的对抗攻击:模型安全性与鲁棒性分析

医学图像的对抗攻击:模型安全性与鲁棒性分析 ,人工智能,计算机视觉,大模型,AI,本文围绕医学图像的对抗攻击展开,分析了其对AI模型的威胁。介绍了对抗样本定义、攻击原理,以及FGSM、BIM、PGD等常见攻击方法,还给出了相关代码示例。阐述了攻击对模型诊断准确性和可信度的影响,提出对抗训练、输入预处理、模型集成等提升模型安全性与鲁棒性的方法,探讨了评估指标、数据集和流程,分析了实际应用中的挑战与应对策略,最后展望了未来发展趋势。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 18万+

poj 2755 神奇的口袋(dp)

poj 2755 神奇的口袋(dp)

PKU_ZZY的博客 1430

PDN分析实战:电源完整性设计与Altium PDN Analyzer全流程指南

电源完整性(PI)是嵌入式硬件系统稳定运行的基础能力,其核心在于控制电流密度与电压降两大物理量。基于欧姆定律与热-电耦合原理,PDN分析将抽象的电源噪声问题转化为可量化、可定位的几何与电气参数优化过程。该技术显著提升系统抗干扰能力、降低复位与ADC漂移等隐性故障率,并直接支撑EMC合规性设计。典型应用场景包括电机驱动板、Wi-Fi模块、STM32/ESP32等资源受限型MCU供电网络验证。本文以Altium Designer为工具平台,结合IPC-2152标准与真实工程案例,详解从功率预算、模型构建到瓶颈定

weixin_28949937的博客 229

【算法练习】动态规划 百练 poj2755:神奇的口袋 (同poj4004 数字组合

题目链接:http://bailian.openjudge.cn/practice/2755 自己blog链接:https://blog.csdn.net/weixin_40760678/article/details/97376773 这个题和那个数字组合基本是一个题,就看一眼就看出来,写的时候出现了一点小问题,就是二维dp数组拍成一维的时候应该从后向前赋值,一开始写忘记了这个!!一定注意!...

一蓑烟雨任平生 363

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Poj百练 2755: 神奇的口袋(分类:动态规划)

一道简单的线性规划问题,其实一切能够思考出递推关系的问题都可以用动态规划的思路来解决~ 直接上代码,子问题和转移方程都写在注释里了! 这道题还可以用滚动数组来优化一下,因为比较懒。。所以没做,但是也可以过了 //Poj百练2755神奇的口袋 #include &lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; ...

Suprit's blog 1033

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

#dp中阶

POJ1661 Help Jimmy https://blog.csdn.net/jdplus/article/details/19919531

Rainfoo的博客 183

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

百练2755 神奇的口袋 【深搜】or【动规】or【普通递归】or【递推】

总Time Limit:  10000ms  Memory Limit:  65536kB 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。John可以从这些物品中选择一些,如果选出的物体的总体积是40,那么利用这个神奇的口袋,John就可以得到这些物品。现在

长风的专栏 1691

神奇的口袋(百练2755)

描述 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。John可以从这些物品中选择一些,如果选出的物体的总体积是40,那么利用这个神奇的口袋,John就可以得到这些物品。现在的问题是,John有多少种不同的选择物品的方式。 输入 输入的第一行是正整数n (1 1,a2……an的

如若 601

百练 2755:神奇的口袋

一道比较基础的递归题,递归的回溯问题自己还需要理解,递归的算法O(2^n)好像也能过?大概是由于数据规模不够大吧。。 题目链接 #include &lt;iostream&gt; using namespace std; int n; int a[105]; int count = 0; void search(int value,int x) { // printf("value = %d...

CuriousLiu的博客 357

poj 神奇的口袋

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34060299的博客 199

百炼(2755)dp

2755:神奇的口袋 ViewSubmitStatisticsHintClarify 总Time Limit: 10000ms Memory Limit: 65536kB Description有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。Jo

u014236804的专栏 643

动态规划(2) 神奇口袋 0-1背包 滑雪 最佳加法表达式

神奇的口袋(百练2755)  有一个神奇的口袋,总的容积是40,用这个口袋可以变出一 些物品,这些物品的总体积必须是40。  John现在有n(1≤n ≤ 20)个想要得到的物品,每个物品 的体积分别是a 1 ,a 2 ……a n 。John可以从这些物品中选择一 些,如果选出的物体的总体积是40,那么利用这个神奇的口 袋,John就可以得到这些物品。现在的问题是,John有多少 ...

衎的博客 396

[PKU暑课笔记] 动态规划(三) 最佳加法表达式 百练2755 POJ3624背包问题

ヾ(●´∀`●) OpenJudge 最佳加法表达式 http://cxsjsxmooc.openjudge.cn/2017t2springhw5/3/ 思路: 假定数字串长度是n,添完加号后,表达式的最后一个加号添加在第 i 个数字后面, 那么整个表达式的最小值,等于在前 i 个数字中插入 m – 1个加号所能形成的最小值,加上第 i + 1到第 n个数字所组成的数的值(i从1开始算)

海蛎丝Ssss 863

动态规划dp(2)

最长公共子序列LCS(POJ1458) describe 给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致。 Sample Input abcfbc abfcab programming contest abcd mnp Sample Output 4 2 0 分析 对于两个序列s1 s2,我们可以...

One of thre_tigers的博客 423

背包问题

背包问题

Ema1997的博客 408
上一篇: UVA 10081 Tight Words
下一篇: Linux学习时遇到的问题5
weixin_34087503
博客等级 码龄11年 4560粉丝 161原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值