ZOJ 3706 Break Standard Weight 解题报告
题目链接: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 解题报告的更多相关文章
- zoj 3706 Break Standard Weight(dp)
Break Standard Weight Time Limit: 2 Seconds Memory Limit: 65536 ...
- [ACM_水题] ZOJ 3706 [Break Standard Weight 砝码拆分,可称质量种类,暴力]
The balance was the first mass measuring instrument invented. In its traditional form, it consists o ...
- zoj 3706 Break Standard Weight
/*题意:将两个砝码中的其中一个分成两块,三块组合最多有几种情况(可以只有一块,或者两块). 组合情况 i j m 三块砝码 (i+j)-m=m-(i+j) i+j i-j=j-i i j m (i ...
- [ZOJ 3076] Break Standard Weight
题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5009 题意:给你两个数字,可以把其中一个拆成两个数字,计算这三个数字 ...
- Break Standard Weight (ZOJ 3706)
Problem The balance was the first mass measuring instrument invented. In its traditional form, it co ...
- zoj 2313 Chinese Girls' Amusement 解题报告
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1313 题目意思:有 N 个人(编号依次为1~N)围成一个圆圈,要求求 ...
- 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...
- 【LeetCode】528. Random Pick with Weight 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-pi ...
- zoj 1109 Language of FatMouse 解题报告
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109 题目意思:给出一个mouse-english词典,问对于输入的m ...
随机推荐
- angular中的this指向问题
this是指向当前$scope的. 例如在ng-click的使用中,this是指向当前的$scope而并不是dom元素的. 我们可以使用this的一些方法和属性 我们打印一下this就会发现,this ...
- 深入探究Java中hashCode()和equals()的关系
目录 一.基础:hashCode() 和 equals() 简介 equals() hashCode() 二. 漫谈:初识 hashCode() 与 equals() 之间的关系 三. 解密:深入理解 ...
- IntelliJ IDE 各种插件的安装和使用
插件的安装和使用持续的更新中...........................................................
- 论DATASNAP结合FIREDAC的使用方法
论DATASNAP结合FIREDAC的使用方法 自DELPHI XE5开始引入FIREDAC数据引擎以来,FIREDAC就正式成为了官方的数据引擎.一直到XE10.1 UPDATE1,据笔者观察,FI ...
- Python基础语法08--MySql应用
python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. DB-API 是一个规范. 它定义了一系列 ...
- PAT 1003 Sharing (25)
题目描写叙述 To store English words, one method is to use linked lists and store a word letter by letter. ...
- SQL ORDER BY 关键字
SQL ORDER BY 关键字 ORDER BY 关键字用于对结果集进行排序. SQL ORDER BY 关键字 ORDER BY 关键字用于对结果集按照一个列或者多个列进行排序. ORDER BY ...
- 如何给老婆解释什么是RESTful
如何给老婆解释什么是RESTful Javdroider Hong 知乎专栏<Beautiful Java>的作者,一个热爱足球和健身的上进boy 1,543 人赞了该文章 老婆经常喜欢翻 ...
- addEventListener event
addEventListener 先看个例子: document.getElementById("myBtn").addEventListener("click&qu ...
- Linux性能查看与分析--命令行工具介绍
本文介绍工作中常用的几个linux性能查看命令:top,sar,vmstat,iostat,pidstat等. 1.top top是最常用的linux性能分析工具,它能够实时的显示系统中各个进程的资源 ...