POJ 1006 Biorhythms
开始的问题代码,不能过,效率低 #include <iostream> using namespace std; int main() { int p,e,i,d,n=1; while(cin>>p>>e>>i>>d){ if(p!=-1&&e!=-1&&i!=-1&&d!=-1){ for(int k =21252;k>0;k--){ ; if((23-p+k)%23==0&&(28-e+k)%28==0&&(33-i+k)%33==0) cout<<"Case "<<n<<": the next triple peak occurs in "<<k-d<<" days."<<endl; } n++; } else break; } }
改正后:
#include <iostream> using namespace std; int main() { int p,e,i,d,n=1; while(cin>>p>>e>>i>>d){ if(p==-1&&e==-1&&i==-1&&d==-1) break; for(int k =1;;k++) //不能从21252找,因为题目是给定天数的下一次同一天,虽然倒着找出来的不用break还恰巧限制了范围,其实题目给出的如果找到第一次是不会超的 if((k-p)%23==0&&(k-e)%28==0&&(k-i)%33==0&&k>d){ cout<<"Case "<<n++<<": the next triple peak occurs in "<<k-d<<" days."<<endl; break; } } return 0; }
准哥代码:
#include<iostream> using namespace std; int main() { int i,d,e,p,a,n=0; while(cin>>d>>e>>p>>a) { ++n; if(d==-1&&e==-1&&p==-1&&a==-1) break; for(i=1;;i++) if((i-d)%23==0&&(i-e)%28==0&&(i-p)%33==0&&i>a) break; cout<<"Case "<<n<<": "<<"the next triple peak occurs in "<<i-a<<" days."<<endl; } return 0; }感觉准哥的爽快清凉些!