ZOJ water jugs problem
2014-01-06 16:54
204 查看
Solution with loop:
It seems to be the right solution, but ZOJ gave "Time limit Exceeded" error; I searched on the interenet about this problem, someone suggests that it may result from "cout/cin" in c++, so I tried printf in C, but it still generates error. Then I tried Iteration method:
Finally, it is accepted!!!!!
#include <iostream> using namespace std; int main(){ int m,n,k,r; while(std::cin>>m>>n>>k){ r=n-m; cout<<"fill B"<<endl; cout<<"pour B A"<<endl; while(r!=k){ if(r<m){ r=n-(m-r); cout<<"empty A"<<endl<<"pour B A"<<endl<<"fill B"<<endl<<"pour B A"<<endl; }else{ r=r-m; cout<<"empty A"<<endl<<"pour B A"<<endl; } } cout<<"success"<<endl; } }
It seems to be the right solution, but ZOJ gave "Time limit Exceeded" error; I searched on the interenet about this problem, someone suggests that it may result from "cout/cin" in c++, so I tried printf in C, but it still generates error. Then I tried Iteration method:
#include <iostream> using namespace std; void operation(int m,int n,int mnow,int nnow,int k){ if(nnow==k) return; if(nnow>(m-mnow)){ nnow=nnow-(m-mnow); cout<<"pour B A"<<endl; if(nnow==k) return; cout<<"empty A"<<endl; mnow=0; }else{ mnow=nnow; nnow=n; cout<<"pour B A"<<endl<<"fill B"<<endl; } operation(m,n,mnow,nnow,k); } int main(){ int m,n,k,mnow,nnow; while(cin>>m>>n>>k){ mnow=0; nnow=n; cout<<"fill B"<<endl; operation(m,n,mnow,nnow,k); cout<<"success"<<endl; } }
Finally, it is accepted!!!!!
相关文章推荐
- ZOJ Problem Set - 1512 (Water Treatment Plants)
- ZOJ Problem Set - 1005 Jugs
- ZOJ Problem Set - 1005 Jugs
- ZOJ Problem Set - 3632 Watermelon Full of Water
- ZOJ Problem Set - 1005 Jugs
- ZOJ Problem Set - 2830 Champion of the Swordsmanship
- ZOJ Problem Set - 1078 Palindrom Numbers
- ZOJ Problem Set - 1205 Martian Addition
- Watermelon Full of Water - ZOJ 3632 dp+线段树
- ZOJ Problem Set - 1240 IBM Minus One
- Argus(ZOJ Problem Set - 2212)(优先队列)
- ZOJ 1005 Jugs
- leetcode 365. Water and Jug Problem
- zoj 1005 Jugs
- ZOJ 3913 Bob wants to pour water (计算几何+二分)
- zoj 3913 Bob wants to pour water(二分)
- ZOJ Problem Set - 1159 487-3279
- ZOJ Problem Set - 1970 All in All
- ZOJ 3326 - An Awful Problem
- hdu 5832 A water problem【大数取膜】