这是道01背包题   ,尽管背包不会  ,可是还是看出来了,递推公式写啊写没写出来,后来一同学说是dfs。这我就開始了A了,

题意是给你n个重量是Wn的石头  让你放到两个包里面。看他们两个差值最小,而且输出这个差值。

dfs代码

#include <stdio.h>
int sum;
int h,T;
int a[100];
void dfs (int x,int y)
{
if(x==T)
{
if(y>h) h=y ;
return ;
}
if(h==sum/2) return ;
if(y+a[x]<=sum/2)//等于号一定要有。这样调了好久好久,由于他们有可能相等。 dfs(x+1,y+a[x]);
dfs(x+1,y);
}
int main()
{
while(scanf("%d",&T)==1)
{
sum=0;h=0;
for(int i=0;i<T;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
// printf("%d\n",sum);
dfs(0,0);
// printf("%d\n",h);
printf("%d\n",sum-2*h);
}
}

dp代码

#include <cstring>
#include <cstdio>
const int maxx = 2000010;
int dp[maxx],a[25],n;
int abs(int x)
{
return (x>0)?x:-x;
}
int maxxx(int a,int b)
{
return (a>b)? a:b;
}
int main()
{
while(scanf("%d",&n)==1)
{
if(n==1)
{
scanf("%d",&a[0]);
printf("%d",a[0]);
continue;
}
int sum = 0,temp,ans;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum += a[i];
dp[i] = 0;
}
temp = sum/2;
dp[0] = 0;
for(int i=1;i<=n;i++)
for(int j=temp;j>=a[i];j--)
dp[j] = maxxx(dp[j-a[i]]+a[i],dp[j]);//求在【0-temp】最大的数就好了
for(int i=temp;i>=0;i--)
if(dp[i]!=0)
{
ans = dp[i];
break;
}
printf("%d\n",abs(sum-2*ans));
}
return 0;
}

ural 1005 Stone Pile的更多相关文章

  1. URAL 1180. Stone Game (博弈 + 规律)

    1180. Stone Game Time limit: 1.0 second Memory limit: 64 MB Two Nikifors play a funny game. There is ...

  2. ural 1180 Stone Game

    http://acm.timus.ru/problem.aspx?space=1&num=1180 #include <cstdio> #include <cstring&g ...

  3. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  4. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  5. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  6. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  7. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  8. poj 动态规划的主题列表和总结

    此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...

  9. 别人整理的dp题目

    动态规划 动态规划 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322, 14 ...

随机推荐

  1. ROS-导航功能-Gazebo

    前言:仿真的整体思路,先启动仿真环境,再启动导航功能. 前提:已下载并编译了相关功能包集,如还未下载,可通过git下载:https://github.com/huchunxu/ros_explorin ...

  2. POJ 2286 The Rotation Game IDA*

    (再一次感谢学长幻灯片) ID A* 随便自己yy了一下. 额嗯 思路什么的都没有问题 就是改不对.. 无奈地删代码...边删边交. 删啊删 哎呦 AC了 ... ... ... 找删的那一段 . o ...

  3. js数据管理的思考

    最近要做一个农场项目,涉及到很多js数据管理的需求,这里也做下总结,不断的总结,再修正内容,也是快速进步的方法. 数据管理几个方面考虑: * 设置(更新)字段值 * 检索,根据id, index, 属 ...

  4. asp.net MVC 路由注册

    1.命名空间的优先级 在路由注册时指定的命名空间比当前 ControllerBuilder 的默认命名空间具有更高的匹配优先级,但是对于这两个集合中的所有命名空间却具有相同的匹配优先级.换句话说,用于 ...

  5. hibernate_03_session详解

    获得session对象有两种方法: 1)openSession 2)getCurrentSession 如果使用的是getCurrentSession需要在hibernate.cfg.xml文件中进行 ...

  6. OpenCV视频进度播放控制

    本来打算把进度条嵌入MFC的PIC空间里面,结果显示进度条消失,看来还是不要这个样子了. 全局变量区域: //2.初始化进度条的位置 int G_slider_position = 0; CvCapt ...

  7. 【JSP】上传图片到数据库中

    第一步:建立数据库 create table test_img(id number(4),name varchar(20),img long raw); 第二步:(NewImg.html) <h ...

  8. 08--MOOC--C/C++ 根据年月日计算星期几

    计算任何一天是星期几的几种算法 一:常用公式 W = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D Y是年份数,D是这一天在这一年中的累积天数,也 ...

  9. Java中Json的用法

    1. JSONObject json = new JSONObject(); json.put("list", "test"); msg=json.toStri ...

  10. pytorch基础(4)-----搭建模型网络的两种方法

    方法一:采用torch.nn.Module模块 import torch import torch.nn.functional as F #法1 class Net(torch.nn.Module): ...