1218:取石子游戏
1 #include<iostream> 2 using namespace std; 3 4 int main(){ 5 int a,b; 6 while(cin>>a>>b){ 7 if(!a&&!b)break; 8 int ans=0; 9 int t=max(a,b); 10 b=min(a,b); 11 a=t; 12 while(a%b){ 13 if(a/b>1)break; 14 a-=b; 15 swap(a,b); 16 ans++; 17 } 18 if(ans%2)cout<<"lose\n"; 19 else cout<<"win\n"; 20 } 21 return 0; 22 }
或者也可以用递归函数的方式做。
1 #include<iostream> 2 using namespace std; 3 4 int solve(int a,int b,int ans){ 5 if(a%b==0||a/b>1)return ans; 6 return solve(b,a-b,ans+1); 7 } 8 int main(){ 9 int a,b; 10 while(cin>>a>>b){ 11 if(!a&&!b)break; 12 if(solve(max(a,b),min(a,b),0)%2)cout<<"lose\n"; 13 else cout<<"win\n"; 14 } 15 return 0; 16 }