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& ...
随机推荐
- 「Violet」蒲公英
「Violet」蒲公英 传送门 区间众数,强制在线. 分块经典题. 像这题一样预处理,然后就直接爆搞,复杂度 \(O(n \sqrt n)\) 参考代码: #include <algorithm ...
- Manacher 算法学习笔记
算法用处: 解决最长回文子串的问题(朴素型). 算法复杂度 我们不妨先看看其他暴力解法的复杂度: \(O(n^3)\) 枚举子串的左右边界,然后再暴力判断是否回文,对答案取 \(max\) . \(O ...
- JPG加入RAR文件原理详解
在水木看到有人上传了一张图片,说如果将其后缀改为rar,解压后会有别的文件,试了一下,果然如此.用十六进制的编辑器看了看,发现的确有理. 先是,文件头部是以JPG格式起始的,如下: ......JFI ...
- ClientDataSet.locate报错问题
数据集循环之后如果使用locate定位,需要首先将数据集first
- 吴裕雄--天生自然HADOOP操作实验学习笔记:hadoop框架认识以及基本操作
实验目的 了解Hadoop的概念和原理 学习HDFS架构原理 熟悉mapreduce框架 熟悉mapred和yarn命令 实验原理 1.hadoop和hadoop生态系统 hadoop的思想来源是Go ...
- Liunx 如何查看80端口被哪个程序所占用
场景:启服务时一直报80端口被占用 解决方: 1.首先查看下 80 端口的使用情况 netstat -anp|grep 80 查看80端口被被占用的PID 2.根据这个PID 来查看被哪个程序在使用 ...
- stringstream常见用法介绍
1 概述 <sstream> 定义了三个类:istringstream.ostringstream 和 stringstream,分别用来进行流的输入.输出和输入输出操作.本文以 stri ...
- Linux命令:history命令历史的管理及用法
bash可以保存的过去曾经执行过的命令.当某个用户登录到shell中,会读取该用户家目录中的~/.bash_history文件,并将历史命令列表保存到内存中.当用户退出当前shell时,会将内存中的历 ...
- NSString 常见数据类型转换:转NSInteger , NSDate(互转)
1. NSString转NSInteger, 转int (float, double类似 ) 1.1正常情况 , NSString所包含内容确能转化为int的类型 NSString *sNumber ...
- php:数据库封装类
<?phpclass DBDA{ public $host="localhost"; public $uid="root"; publi ...