http://uva.onlinejudge.org/external/6/616.html
#include<iostream> using namespace std; int no(int c); void print(int p,int n); int main() { int c,p; cin>>c; while(c>0) { p=no(c); print(p,c); cin>>c; } } void print(int p,int n) { if(p>0) { cout<<n<<" coconuts, "<<p<<" people and 1 monkey\n"; } else cout<<n<<" coconuts, no solution\n"; } int no(int c) { int j,a,b,d; for(int i=10;i>1;i--) { if(c%i==1) { b=c; a=1; for(j=1;j<=i;j++) { if (b%i==1) { b=((b-1)/i)*(i-1); } else { a=0; break; } } if ((a==1)&&(b%i==0)) { return i; } else if ((a==0)&&(i==2)) return -1; } } return -1; }
No comments:
Post a Comment