POJ 2142:The Balance
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 4781 | Accepted: 2092 |
Description
on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights.
You are asked to help her by calculating how many weights are required.

Input
a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases.
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
- You can measure dmg using x many amg weights and y many bmg weights.
- The total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous condition.
- The total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.
No extra characters (e.g. extra spaces) should appear in the output.
Sample Input
700 300 200
500 200 300
500 200 500
275 110 330
275 110 385
648 375 4002
3 1 10000
0 0 0
Sample Output
1 3
1 1
1 0
0 3
1 1
49 74
3333 1
题意是给出了a,b,d的重量。问使用a、b怎么测出d的重量,假设是能够测出的前提下。输出|x|+|y|的最小值。
a*x+b*y=d
之后求一下x的最小值时y的值。再求一遍y的最小值时x的值。两两比较即可。
代码:
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; int xx,yy,yue;
int a,b,d;
vector <int> x_value;
vector <int> y_value; void ex_gcd(int a,int b, int &xx,int &yy)
{
if(b==0)
{
xx=1;
yy=0;
yue=a;
}
else
{
ex_gcd(b,a%b,xx,yy); int t=xx;
xx=yy;
yy=t-(a/b)*yy; }
} void cal()
{
int i;
int min=abs(x_value[0])+abs(y_value[0]);
int min_x=abs(x_value[0]),min_y=abs(y_value[0]); for(i=1;i<2;i++)
{
if(abs(x_value[i])+abs(y_value[i])==min)
{
if((abs(x_value[i])*a+abs(y_value[i])*b)<(min_x*a+min_y*b))
{
min_x=abs(x_value[i]);
min_y=abs(y_value[i]);
}
}
if(abs(x_value[i])+abs(y_value[i])<min)
{
min=abs(x_value[i])+abs(y_value[i]);
min_x=abs(x_value[i]);
min_y=abs(y_value[i]);
}
}
cout<<min_x<<" "<<min_y<<endl;
} int main()
{
while(cin>>a>>b>>d)
{
if(a==0 && b==0 && d==0)
break;
ex_gcd(a,b,xx,yy); x_value.clear();
y_value.clear(); xx=xx*(d/yue);
yy=yy*(d/yue); int r=a/yue;
yy=(yy%r+r)%r;
int xx0,yy0=yy;
xx0=(d-yy*b)/a;
x_value.push_back(xx0);
y_value.push_back(yy); r=b/yue;
xx=(xx%r+r)%r;
x_value.push_back(xx);
yy=(d-xx*a)/b;
y_value.push_back(yy);
cal();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2142:The Balance的更多相关文章
- POJ.2142 The Balance (拓展欧几里得)
POJ.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个, ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- poj 2142 The Balance
The Balance http://poj.org/problem?id=2142 Time Limit: 5000MS Memory Limit: 65536K Descripti ...
- POJ 2142 The Balance(exgcd)
嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...
- The Balance POJ 2142 扩展欧几里得
Description Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of ...
- POJ 1837:Balance 天平DP。。。
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11878 Accepted: 7417 Descript ...
- POJ 2142 The Balance【扩展欧几里德】
题意:有两种类型的砝码,每种的砝码质量a和b给你,现在要求称出质量为c的物品,要求a的数量x和b的数量y最小,以及x+y的值最小. 用扩展欧几里德求ax+by=c,求出ax+by=1的一组通解,求出当 ...
- POJ 2142 The Balance (解不定方程,找最小值)
这题实际解不定方程:ax+by=c只不过题目要求我们解出的x和y 满足|x|+|y|最小,当|x|+|y|相同时,满足|ax|+|by|最小.首先用扩展欧几里德,很容易得出x和y的解.一开始不妨令a& ...
随机推荐
- Codeforces 1260 ABC
DEF 题对于 wyh 来说过于毒瘤,十分不可做. A. Heating Description: 给定\(a,b\),将\(b\)分成至少\(a\)个正整数,使这些正整数的平方和最小. Soluti ...
- Android Studio中 no module 问题,解决方法
等它执行完以后就好了 或者根据提示手动下载缺失的.
- 2019上海爱奇艺大数据Java实习生-面试记录
目录 一轮 电话面试 二轮 代码笔试 三轮 技术面试 总结 附:电话面试问题点解惑 补充:面试未通过 一轮 电话面试 2019.04.28 16:21 [w]:面试官,[m]:我,下面的内容来自电话录 ...
- 2016 年 31 款轻量高效的开源 JavaScript 插件和库
目前有很多网站设计师和开发者喜欢使用由JavaScript开发的插件和库,但同时面临一个苦恼的问题:它们中的大多数实在是太累赘而且常常降低网站的性能.其实,其中也有不少轻量级的插件和库,它们不仅轻巧有 ...
- rundll32 常用命令
Rundll32 常用命令列表(1) 下面是具体的Rundll32 的命令行列表: 添加删除程序 RunDll32.exe shell32.dll,Control_RunDLL appwiz.cpl, ...
- Xcode下载途径
Xcode除了能在Appstore直接下载外,还可以用开发者账号登陆开发者中心[https://developer.apple.com/download/]下载对应的资源. 在开发者中心下载的好处是, ...
- 完全取代VC上原有的view
如果需要在这个VC上放置一个subviewA,作用相当于取代self.view,那么最好不要使用 [self.view addSubView: subviewA]; 而要使用 self.view = ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:qq好友推荐算法
实验目的 初步认识图计算的知识点 复习mapreduce的知识点,复习自定义排序分组的方法 学会设计mapreduce程序解决实际问题 实验原理 QQ好友推荐算法是所有推荐算法中思路最简单的,我们利用 ...
- java中常用的数据结构--Collection接口及其子类
java中有几种常用的数据结构,主要分为Collection和map两个主要接口(接口只提供方法,并不提供实现),而程序中最终使用的数据结构是继承自这些接口的数据结构类. 一.集合和数组的区别 二.C ...
- 「Luogu P3072 [USACO13FEB]周长Perimeter」
USACO的题目,感觉还是挺神奇的. 前置芝士 DFS(BFS)遍历:用来搜索(因为DFS好写,本文以DFS为准还不是因为作者懒) STL中的set(map)的基本用法:数据很大,不能直接存. 具体做 ...