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& ...
随机推荐
- dp - 活动选择问题
算法目前存在问题,待解决.. 活动选择问题是一类任务调度的问题,目标是选出一个最大的互相兼容的活动集合.例如:学校教室的安排问题,几个班级需要在同一天使用同一间教室,但其中一些班级的使用时间产生冲突, ...
- MyuCMS_V2.1漏洞分析
前言 在CNVD看到一个MyuCMS的一个任意文件删除漏洞.然后去搜了下这个CMS,发现官网公告显示在V2.2.3版本修复了CNVD提供的多处漏洞. 怀着好奇的心里,去CNVD搜了下这个CMS,结果发 ...
- [aac @ ...] more samples than frame size (avcodec_encode_audio2)
在用FFmpeg对音频进行编码的时候报如下错误: [aac @ 000001cfc2717200] more samples than frame size (avcodec_encode_audio ...
- 小程序中data数据的处理方法总结
wxml代码: <view class="container"> <view wx:for="{{list}}" wx:key="t ...
- Python 基础之python运算符
一.运算符 1.算数运算符 + - * / // % ** var1 = 5var2 = 8 #(1) + 加res = var1 + var2print(res) # (2) - 减res = ...
- 中山Day4——普及
生活开始日益平淡了呢...今天130分. 收获:归并排序求逆序对 背包问题(01.完全.多重)(外带滚动数组优化) T1:题目链接(才不会告诉你们下面的代码也是洛谷上弄来的) 思路:动态规划.首先,设 ...
- python之函数名称空间,作用域,嵌套函数
目录 嵌套函数 定义 名称空间的三大类(只存变量名) 名称空间加载顺序 变量名的查找顺序为 作用域 嵌套函数 定义 函数内部定义的函数,无法在函数外部使用内部定义的函数. def f1(): def ...
- 不要在PHP7中踩这些坑
PHP是当今仍然是最流行的Web开发语言,目前在所有使用服务端编程语言的网站中,超过83%的站点在使用PHP.PHP7在性能方面实现跨越式的提升,然后有些坑我们还是要提醒PHPer不要踩. 1. 不要 ...
- Windows驱动开发-IoCompleteRequest
IoCompleteRequest 例程表示调用者的已经完成了对指定I/O请求的所有处理操作,并且向I/O管理器返回指定的IRP报文. //函数原型 VOID IoCompleteRequest( I ...
- 7. 通过JDBC源码来分析线程上下文类加载器以及SPI的使用
目录 1. 什么是全盘负责委托机制 2. 为什么需要有线程上下文类加载器 2.1 使用JDBC的例子,分析为什么双亲委托机制不能实现要求 2.2 线程上下文类加载器的作用 3. 线程上下文类加载器的使 ...