洛谷 P1432 倒水问题
P1432 倒水问题
A一直往B里倒 和 B一直往A里倒 取一个步骤较小的方案
输出涨姿势了
1 #include<bits/stdc++.h> 2 using namespace std; 3 int ca,cb,n,l1,l2; 4 5 int main() 6 { 7 while(scanf("%d%d%d",&ca,&cb,&n)!=EOF) 8 { 9 l1=0,l2=0; 10 string s1,s2; 11 for(int fa=0,fb=0;fb!=n;l1++) 12 { 13 if(fa==0) fa=ca,s1+="fill A\n"; 14 else if(fb==cb) fb=0,s1+="empty B\n"; 15 else{ 16 fa-=cb-fb; fb=cb; 17 if(fa<0) 18 { 19 fb+=fa; 20 fa=0; 21 } 22 s1+="pour A B\n"; 23 } 24 } 25 for(int fa=0,fb=0;fb!=n;l2++) 26 { 27 if(fb==0) fb=cb,s2+="fill B\n"; 28 else if(fa==ca) fa=0,s2+="empty A\n"; 29 else{ 30 fb-=ca-fa; fa=ca; 31 if(fb<0) 32 { 33 fa+=fb; 34 fb=0; 35 } 36 s2+="pour B A\n"; 37 } 38 } 39 if(l1<=l2) 40 printf("%s",s1.c_str()); 41 else printf("%s",s2.c_str()); 42 printf("success\n"); 43 } 44 return 0; 45 }