取书问题

1746阅读 0评论2011-09-15 yinyuemi
分类:C/C++

  1. 如果桌子上有999本书,你和另外一个人一起轮流拿,你的目的是拿到最好一本书,规则是两个人轮流拿,每人每次最少拿一本,最多拿三本.问你如何拿到最后一本书.

  2. #include <iostream>

  3. using namespace std;

  4. int fun(int & i,int & j){
  5.     int n;
  6.     int arr[5]={2,3,4,5,6};
  7.     for(n=1;n<5;n++){
  8.         if(!(i%arr[n])&&arr[n]>j){
  9.             return arr[n];
  10.             break;
  11.         }
  12.     }
  13. }

  14. int main (){

  15.     int total=999;
  16.     int i;
  17.     int j;
  18.     int k;
  19.     while(i<=999-4){
  20.         
  21.         cin>>j;
  22.         k=fun(i,j);
  23.         cout<<"p1: "<<k-j<<" p2: "<<j<<endl;
  24.         i+=k;
  25.     }
  26.     cin>>j;
  27.     if(j==1)cout<<"p1: "<<4-j<<" p2: "<<j<<endl;
  28.     else if(j==2)cout<<"p1: "<<4-j<<" p2: "<<j<<endl;
  29.     else cout<<"p1: "<<4-j<<" p2: "<<j<<endl;
  30. }

上一篇:字符串输出问题
下一篇:找了个比较全面的保留了gcc详解