题目链接: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. Codevs 2855 游乐园的迷宫

    2855 游乐园的迷宫  时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 迷宫可是每个游乐园必不可少的项目,菜菜当然是要尝试一下啦. ...

  2. Struts2标签-checkbox只读属性设置

    Struts2标签-checkbox只读属性设置 在struts2的checkbox标签中,为实现只读效果,一般使用readonly="true"是达不到效果的,但设置disabl ...

  3. JS基础之BOM对象

    BOM 对象 JavaScript分为 ECMAScript,DOM,BOM. BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执行其 ...

  4. codevs——1690 开关灯

    1690 开关灯 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description YYX家门前的街上有N( ...

  5. Android-一张图理解MVP的用法

    M和V通过P交互,M做了两件事,开启子线程做耗时操作,然后使用原生的Hander方式切回主线程回调结果给P. M做的两件事也可以使用比较流行的rxjava实现: 备注:图片不清晰可以看这里

  6. JNI之—— Eclipse配置C/C++开发环境

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46622173 开发环境:Eclipse3.2.CDT3.1.MinGW5.1 1. ...

  7. NAND FLash基础概念介绍

    一.引脚介绍 引脚名称 引脚功能 CLE 命令锁存功能 ALE 地址锁存功能 /CE 芯片使能 /RE 读使能 /WE 写使能 /WP 写保护 R/B 就绪/忙输出信号 Vcc 电源 Vss 地 N. ...

  8. poj 3169 Layout(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Descriptio ...

  9. Effective C++ 条款五 了解C++默默编写并调用哪些函数

      //申明一个类时,编译器会默认为你提供四个函数. //无参构造函数,析构函数,copy构造函数,copy assignment操作符.     template <typename T> ...

  10. centos或者ubuntu设置ssh免密码登陆

    1. 输入  # ssh-keygen -t rsa -P ""  然后一路回车 2.输入  # cat ~/.ssh/id_rsa.pub >> ~/.ssh/aut ...