I - 非常可乐
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
Output
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
Sample Input
7 4 3
4 1 3
0 0 0
Sample Output
NO
3
我先用了广搜的思路,不知道下面如何错了,然后我又想到用循环可以很简单的枚举出来,然后我又写了一下,自己想到的和给出的样例都过了,不知道错在哪里,求答者指正,谢谢。
下面是bfs的代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
int n,a,b,cnt;
struct Node{
int left;
int right;
int step;
};
Node node1,node2;
queue<Node>que;
void bfs()
{
que.push(node1);
que.push(node2);
while(!que.empty())
{
Node temp = que.front();
que.pop();
Node temp1,temp2;
temp1.step=temp.step+1;
temp2.step=temp.step+1;
temp1.left=temp.left+a;
temp1.right=temp.right;
temp2.left=temp.left;
temp2.right=temp.right+b;
if(!(temp1.left>n/2||temp1.right>n/2))
{
que.push(temp1);
}
if(!(temp2.left>n/2||temp2.right>n/2))
que.push(temp2);
if(temp1.left==n/2)
{
int lea=n-temp1.left-temp1.right;
while(lea>0)
{
cnt++;
lea-=b;
}
if(cnt>temp1.step)
{
cnt=temp1.step;
}
}
if(temp1.right==n/2)
{
int lea=n-temp1.left-temp1.right;
while(lea>0)
{
cnt++;
lea-=a;
}
if(cnt>temp1.step)
{
cnt=temp1.step;
}
}
if(temp2.left==n/2)
{
int lea=n-temp2.left-temp2.right;
while(lea>0)
{
cnt++;
lea-=b;
}
if(cnt>temp2.step)
{
cnt=temp2.step;
}
}
if(temp2.right==n/2)
{
int lea=n-temp2.left-temp2.right;
while(lea>0)
{
cnt++;
lea-=a;
}
if(cnt>temp2.step)
{
cnt=temp2.step;
}
}
}
}
int main()
{
while(scanf("%d %d %d",&n,&a,&b)&&n)
{
if(n%2)
printf("NO\n");
else
{
cnt=999;
node1.left=a;
node1.right=0;
node1.step=1;
node1.left=0;
node1.right=b;
node1.step=1;
bfs();
if(cnt==999)
printf("NO\n");
else
printf("%d\n",cnt+1);
}
}
}
然后是循环枚举的代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
int n,a,b;
int main()
{
while(scanf("%d %d %d",&n,&a,&b),n||a||b)
{
if(a==b&&n%2==0)
{
printf("1\n");
}
else
{
int cnt=999;
if(n%2==1)
printf("NO\n");
else
{
for(int i=1; i<=110; ++i)
{
for(int j=1; j<=110; ++j)
if((i*a+j*b>=n)&&(i*a==n/2||j*b==n/2))
if(i+j<cnt)
cnt=i+j;
}
if(cnt==999)
printf("NO\n");
else
printf("%d\n",cnt);
}
}
}
}