........

来源:互联网 发布:德国牧羊犬 知乎 编辑:程序博客网 时间:2024/05/03 12:54
//苹果
int KindNumOfApple(unsigned int m,unsigned int n){      if(m==1||n==1)   return 1;   if(m<n)   return m;   if(m>n)   return KindNumOfApple(m,n-1)+KindNumOfApple(m-n,n);   if(m==n)   return 1+KindNumOfApple(m,n-1);}
//及格分
<pre name="code" class="cpp">int PassScore( int *InPut){  assert(InPut!=NULL);  int i,j,tmp;  int Score;  for(i=0;i<9;i++){  for(j=0;j<9-i;j++)  {    if(InPut[j]>InPut[j+1]){tmp=InPut[j];   InPut[j]=InPut[j+1];   InPut[j+1]=tmp;}  }  }  if(*InPut>60)  return 60;  Score=InPut[4]/10*10;  return Score;}
//字符串压缩
<pre name="code" class="cpp">void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr){  assert((pInputStr!=NULL)&&(lInputLen>0)); // int strCount[26]={0};  int i,j=0,tmp=0;  for(i=0;i<lInputLen;i++)  {  tmp=1;    while(pInputStr[i]==pInputStr[i+1]){   tmp++;   i++;}if(tmp>1){pOutputStr[j++]=tmp+'0';}pOutputStr[j++]=pInputStr[i];  }  pOutputStr[j]='\0';}
//约瑟夫
<pre name="code" class="cpp">typedef struct _Node{   int  key;   struct _Node *pNext;}Node;typedef struct _Node* ptrNode;struct _Node* CreatLink(int n,Node* pHead){  struct _Node* tmp=(ptrNode)malloc(sizeof(Node));  struct _Node* pnext;  int i;  if(pHead==NULL)  {     ptrNode tmp=(ptrNode)malloc(sizeof(Node)); tmp->key=1; tmp->pNext=NULL; pHead=tmp; pnext=tmp;  }  for( i=2;i<=n;i++)  {     ptrNode tmp=(ptrNode)malloc(sizeof(Node)); tmp->key=i; tmp->pNext=NULL; pnext->pNext=tmp; pnext=pnext->pNext;  }  pnext->pNext=pHead;   return pHead;}int theEndNum(int n,int m,int k){ Node* Head=NULL; Node* ptmp=Head; Node* pdel; int temp;Head=CreatLink(n,Head); while(k--){   ptmp=ptmp->pNext;}while(ptmp->pNext!=ptmp){  temp=m;   while(--temp)   {     ptmp=ptmp->pNext;   }   pdel=ptmp->pNext;   ptmp->key=ptmp->pNext->key;   ptmp->pNext=ptmp->pNext->pNext;   free(pdel);}return ptmp->key;}

//和尚挑水
<pre name="code" class="cpp">#include<iostream>  using namespace std;    const int SETED=100;    // 每行代表一个和尚  // ready[i][j]=1表示第i个和尚第j天有空。  int ready[7][7]={        0, 1, 0, 1, 0, 0, 0,       1, 0, 0, 0, 0, 1, 0,       0, 0, 1, 0, 0, 0, 1,       0, 0, 0, 0, 1, 0, 0,       1, 0, 0, 1, 0, 1, 0,       0, 1, 0, 0, 1, 0, 0,       0, 0, 1, 0, 0, 1, 1   };    int res[7][7]={0};    void show_result(){       static int count=1;      printf("第%d种分配方案:\n",count);      ++count;      for(int i=0;i<7;++i){          for(int j=0;j<7;++j){              if(ready[i][j]>10){                  cout<<"1 ";              }else{                  cout<<0<<" ";              }          }          cout<<endl;      }      cout<<endl<<endl;  }    //冲突检测函数,每个和尚只需要判断其选择的日期内没有其他和尚就行。  //即位置上方没有冲突  int conflict(int row, int column){        for(int i=1;i<7;++i){          //上          if(row-i>=0 &&ready[row-i][column]>10)              return 1;      }      return 0;  }    void result(int row){      for(int column=0;column<7;++column){            if( 1!=ready[row][column] )              continue;                    ready[row][column]+=SETED;          if(!conflict(row,column)){              if(row<6)                  result(row+1);              else                  show_result();          }          ready[row][column]-=SETED;//回溯的本意        }  }    int main(){      result(0);      return 0;  }  



                                             
0 0
原创粉丝点击