大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
7 4 3 4 1 3 0 0 0
NO 3
因为写错了一个字母,看了仨小时,也是醉了
数论的方法:
#include<stdio.h> int gcd(int x,int y) { if(y==0)return x; else return gcd(y,x%y); } int main() { int S,N,M; while(~scanf("%d%d%d",&S,&N,&M)&&(S||N||M)) { if(S%2==1) printf("NO\n"); else if(S%4!=0&&M%2==0&&N%2==0) printf("NO\n"); else { int s=gcd(S,(N,M)); if(S/s%2) printf("NO\n"); else printf("%d\n",S/s-1); } } return 0; }
bfs
#include <queue> #include <stdio.h> #include <string.h> #include <iostream> using namespace std; int step; int S, N, M; int v[105][105]; struct node{ int x, y; int step; }pre,nex; struct step{ int x, y; }sp[105][105]; //void print(int x,int y) { //倒水打印 // printf("%d %d\n",x,y); // if(x==0&&y==0) return; // int dx = sp[x][y].x; // int dy = sp[x][y].y; // print(dx,dy); //} void bfs(int x, int y) { //N,M queue<node> q; pre.x = x; pre.y = y; pre.step = 0; v[x][y] = 1; q.push(pre); while(!q.empty()) { nex = q.front(); q.pop(); if((nex.x == S/2&&nex.y==0) || (nex.y == S/2&&nex.x==0)) { //print(nex.x, nex.y); step = nex.step; return ; } for(int i = 0; i < 6; i++) { if(i == 0) { //S->M if(nex.y != M && v[nex.x][M] == 0) { pre.x = nex.x; pre.y = M; pre.step = nex.step + 1; sp[pre.x][pre.y].x = nex.x; sp[pre.x][pre.y].y = nex.y; v[nex.x][M] = 1; q.push(pre); } } else if(i == 1) { //S->N if(nex.x != N && v[N][nex.y] == 0) { pre.x = N; pre.y = nex.y; pre.step = nex.step + 1; sp[pre.x][pre.y].x = nex.x; sp[pre.x][pre.y].y = nex.y; v[N][nex.y] = 1; q.push(pre); } } else if(i == 2) { // M->S if(nex.y != 0 && v[nex.x][0] == 0) { pre.x = nex.x; pre.y = 0; pre.step = nex.step + 1; sp[pre.x][pre.y].x = nex.x; sp[pre.x][pre.y].y = nex.y; v[nex.x][0] = 1; q.push(pre); } } else if(i == 3) { // N->S if(nex.x != 0 && v[0][nex.y] == 0) { pre.x = 0; pre.y = nex.y; pre.step = nex.step + 1; sp[pre.x][pre.y].x = nex.x; sp[pre.x][pre.y].y = nex.y; v[0][nex.y] = 1; q.push(pre); } } else if(i == 4) { // N->M if(nex.x != 0 && nex.y != M){ if(nex.x >= (M-nex.y) && v[nex.x-(M-nex.y)][M] == 0) { pre.x = nex.x-(M-nex.y); pre.y = M; pre.step = nex.step + 1; v[nex.x-(M-nex.y)][M] = 1; sp[pre.x][pre.y].x = nex.x; sp[pre.x][pre.y].y = nex.y; q.push(pre); } else if(nex.x < (M-nex.y) && v[0][nex.x+nex.y] == 0) { pre.x = 0; pre.y = nex.x + nex.y; pre.step = nex.step + 1; sp[pre.x][pre.y].x = nex.x; sp[pre.x][pre.y].y = nex.y; v[0][nex.x+nex.y] = 1; q.push(pre); } } } else { // M->N if(nex.y != 0 && nex.x != N){ if(nex.y >= (N-nex.x) && v[N][nex.y-(N-nex.x)] == 0) { pre.x = N; pre.y = nex.y-(N-nex.x); pre.step = nex.step + 1; sp[pre.x][pre.y].x = nex.x; sp[pre.x][pre.y].y = nex.y; v[N][nex.y-(N-nex.x)] = 1; q.push(pre); } else if(nex.y < (N-nex.x) && v[nex.x+nex.y][0] == 0) { pre.x = nex.x+nex.y; pre.y = 0; pre.step = nex.step + 1; sp[pre.x][pre.y].x = nex.x; sp[pre.x][pre.y].y = nex.y; v[nex.x+nex.y][0] = 1; q.push(pre); } } } } } return ; } int main() { while(~scanf("%d%d%d",&S,&N,&M) && (S+N+M)) { if(S&1) { printf("NO\n"); continue; } step = -1; memset(v, 0, sizeof(v)); bfs(0, 0); if(step == -1) printf("NO\n"); else printf("%d\n",step); } return 0; }