| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 1643 | Accepted: 695 |
Description
You are asked to help her by calculating how many weights are required.
Input
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
Source
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
int exgcd(int a,int b,int &x,int &y)
{
int d,t;
if(b==0)
{
x=1;
y=0;
return a;
}
d=exgcd(b,(a%b+b)%b,x,y);
t=x;
x=y;
y=t-(a/b)*y;
return d;
}
int main()
{
int a,b,d,enda,endb;
while(scanf("%d%d%d",&a,&b,&d)!=EOF)
{
if(a==0&&b==0&&d==0) break;
int f=0;
if(a<b) swap(a,b),f=1;//转换,必须知道A,B哪个大,因为第一个要求就是要让砝码数量尽可能少
int x,y;
int dd=exgcd(a,b,x,y);
x=x*(d/dd);
y=y*(d/dd);
int sa=a/dd,sb=b/dd;
while(y<=0) x-=sb,y+=sa;//转换,为了使砝码数量最小
int sum=1<<31-1,max=1<<31-1;
while(y>-sa)
{
if(abs(x)+abs(y)<sum)
{
sum=abs(x)+abs(y);
max=a*abs(x)+b*abs(y);//总质量最小
enda=x;
endb=y;
}
else if(abs(x)+abs(y)==sum)
{
if(a*abs(x)+b*abs(y)<max)
{
max=a*abs(x)+b*abs(y);
enda=x;
endb=y;
}
}
y-=sa;
x+=sb;
}
if(f) swap(enda,endb);
printf("%d %d/n",abs(enda),abs(endb));
}
return 0;
}
本文介绍了一种解决特定药品剂量测量问题的算法。通过使用两种不同重量的砝码,在确保最少砝码数量的前提下,实现精确测量指定重量的目标。文章详细阐述了输入输出格式及示例,并提供了完整的代码实现。
812

被折叠的 条评论
为什么被折叠?



