题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5009

题目意思:给出两个mass:x 和 y,问如何将其中一个 mass 一分为二(当然分完之后它们的和要等于原来的mass,或x 或 y),使得利用这三个mass 可称的数量最大。输出这个最大数量。

网上参考别人用STL中的set来写,太厉害了!!!考虑到set对于重复的元素只存储一个,那么当三个mass组合的过程中有重复的,它都会自动舍弃有重复的,不需要用if来判断!另外,也有可能其中两个mass是相等的,这时如果各放一边,就只会称到0,即不能称到任何物体!所以预先把0插入,这就是代码中为什么最后要返回  st.size()-1 的原因!!

方法一:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
using namespace std; set<int> st;
int cal(int a, int b, int c)
{
st.clear(); // 每一次都要清空集合里的元素
st.insert(); // 考虑到左右两边各有一个相同mass的情况,这时称不了物体!
st.insert(a); // 只选一个mass称物体
st.insert(b);
st.insert(c); st.insert(a+b); // 只选两个mass称另外一边的物体
st.insert(abs(a-b));
st.insert(b+c);
st.insert(abs(b-c));
st.insert(a+c);
st.insert(abs(a-c)); st.insert(abs(a+b-c)); // 两个mass在一边,另一边放第三个mass和代称物
st.insert(abs(a+c-b));
st.insert(abs(b+c-a)); st.insert(a+b+c); // 三个mass在一边,另一边称物体
return st.size()-;
} int main()
{
int T, x, y, sum;
while (scanf("%d", &T) != EOF)
{
while (T--)
{
sum = ;
scanf("%d%d", &x, &y);
for (int i = ; i <= x; i++)
{
int t1 = i;
int t2 = x-i;
int t3 = y;
sum = max(sum, cal(t1, t2, t3));
}
for (int i = ; i <= y; i++)
{
int t1 = i;
int t2 = y-i;
int t3 = x;
sum = max(sum, cal(t1, t2, t3));
}
printf("%d\n", sum);
}
}
return ;
}

方法二:用到dfs

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; int cnt, num[];
int vis[]; void dfs(int count, int now)
{
if (!vis[now] && now > )
{
vis[now] = ;
cnt++;
}
if (count == )
return;
dfs(count+, now+num[count]);
dfs(count+, now-num[count]);
dfs(count+, now);
} int main()
{
int T, x, y, sum;
while (scanf("%d", &T) != EOF)
{
while (T--)
{
sum = ;
scanf("%d%d", &x, &y);
for (int i = ; i <= x; i++)
{
cnt = ;
memset(vis, , sizeof(vis));
num[] = i;
num[] = x-i;
num[] = y;
dfs(, );
sum = max(sum, cnt);
}
for (int i = ; i <= y; i++)
{
cnt = ;
memset(vis, , sizeof(vis));
num[] = i;
num[] = y-i;
num[] = x;
dfs(, );
sum = max(sum, cnt);
}
printf("%d\n", sum);
}
}
return ;
}

ZOJ 3706 Break Standard Weight 解题报告的更多相关文章

  1. zoj 3706 Break Standard Weight(dp)

    Break Standard Weight Time Limit: 2 Seconds                                     Memory Limit: 65536 ...

  2. [ACM_水题] ZOJ 3706 [Break Standard Weight 砝码拆分,可称质量种类,暴力]

    The balance was the first mass measuring instrument invented. In its traditional form, it consists o ...

  3. zoj 3706 Break Standard Weight

    /*题意:将两个砝码中的其中一个分成两块,三块组合最多有几种情况(可以只有一块,或者两块). 组合情况 i j m 三块砝码 (i+j)-m=m-(i+j) i+j i-j=j-i  i j m (i ...

  4. [ZOJ 3076] Break Standard Weight

    题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5009 题意:给你两个数字,可以把其中一个拆成两个数字,计算这三个数字 ...

  5. Break Standard Weight (ZOJ 3706)

    Problem The balance was the first mass measuring instrument invented. In its traditional form, it co ...

  6. zoj 2313 Chinese Girls' Amusement 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1313 题目意思:有 N 个人(编号依次为1~N)围成一个圆圈,要求求 ...

  7. 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...

  8. 【LeetCode】528. Random Pick with Weight 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-pi ...

  9. zoj 1109 Language of FatMouse 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109 题目意思:给出一个mouse-english词典,问对于输入的m ...

随机推荐

  1. Yii 之Session使用

    public function actionIndex(){ $session = \YII::$app->session; //判断session是否开启 if(!$session->i ...

  2. Linux 系统的常用命令之 rm ,rm -rf , rm -f 以及rm 命令的其他参数命令

    1.rm -rf * 删除当前目录下的所有文件,这个命令很危险,应避免使用. 所删除的文件,一般都不能恢复! 2.rm -f 其中的,f参数 (f --force ) 忽略不存在的文件,不显示任何信息 ...

  3. luogu P2085 最小函数值

    题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个 ...

  4. CS Academy #32 G

    题意: 分析: 考虑如何求方案数 dp[i][j]表示i个数字的和为j的方案数,这是个经典问题,转移有两种,一个是填一个数字1,一个是整体加1 然后这个问题并不是求方案数,而是求对应的权值和 我们很容 ...

  5. golang几种post请求方式

    get请求 get请求可以直接http.Get方法,非常简单. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 func httpGet() {     resp, err := h ...

  6. nuxt.js 加百度统计

    Mark一下: 在 Nuxt.js应用中使用Google统计分析服务,或者百度统计分析服务,推荐在 plugins 目录下创建 plugins/ga.js 文件.统计统计分析我们可以获取网站pv,uv ...

  7. MySQL的字符编码体系(一)——数据存储编码

    安装MySQL好多次了,每次都会纠结于数据库的字符编码配置,所以我决定这一次彻底把它理清. MySQL的字符编码结构比較细,它慷慨向分为两个部分:数据存储编码和传输数据编码.本篇讨论数据存储编码部分, ...

  8. JavaScript - 正则表达式解惑

    正则表达式手册: http://tool.oschina.net/uploads/apidocs/jquery/regexp.html 正则表达式测试地址: http://tool.chinaz.co ...

  9. mysql 同样内容的字段合并为一条的方法

    从两个表中内联取出的数据,当中category_name字段有同样内容,想将具有同样内容的字段进行合并,将amount字段进行加法运算,变成下表中的内容 url=http%3A%2F%2Fdev.my ...

  10. weex 项目开发(一) weex create project 与 weex init project 的区别

    开发环境配置:http://www.cnblogs.com/crazycode2/p/7822961.html 1. weex create project  与  weex init project ...