微型蓝球赛问题

来源:互联网 发布:同知是什么官 编辑:程序博客网 时间:2024/06/10 01:59

/*

问题描述: 甲,乙两队进行蓝球比赛,结果甲队以S:T 获胜.(T<S<=10, S,T
 由键盘输入). 比赛中, 甲队得分始终领先(严格大于乙队). 规定以任何方式进一
 球都只得一分. 编程序打印该比赛的每一种可能的不同的得分过程, 以及所有不同
 过程的总数.

*/

  1. #include<iostream>
  2. using namespace std;
  3. bool Process[20];//存储比赛过程,true代表甲队得分,false代表已队得分
  4. int count=0;//存储不同过程的总数
  5. int main(){
  6.  int S,T;
  7.  void Match(int S,int T,int s,int t);//s,t代表当前甲队与已队的得分
  8.  cout<<"输入S,T(1<=T<S<=10)!"<<endl;
  9.  cin>>S>>T;
  10.  Match(S,T,0,0);
  11.  cout<<"不同过程的总数为:"<<count<<endl;
  12.  return 0;
  13. }
  14. void Print(int S,int T){
  15.     for(int i=0;i<S+T;i++){
  16.   if(Process[i]==true)cout<<"甲队进一球!"<<endl;
  17.   else cout<<"已队进一球!"<<endl;
  18.  }
  19.  cout<<"****************"<<endl;
  20.  return;
  21. }
  22. void Match(int S,int T,int s,int t){
  23.  if(s==S && t==T){//比赛结束
  24.   count++;
  25.   Print(S,T);
  26.   return;
  27.  }
  28.  else if(s<S && t<T){//两队都可以得分
  29.   if(s==t){//只能甲队得分
  30.    Process[s+t]=true;
  31.    Match(S,T,s+1,t);
  32.   }
  33.   else{
  34.    Process[s+t]=true;
  35.    Match(S,T,s+1,t);
  36.    Process[s+t]=false;
  37.    Match(S,T,s,t+1);
  38.   }
  39.  }
  40.  else if(s==S){
  41.   Process[s+t]=false;
  42.   Match(S,T,s,t+1);
  43.  }
  44.  else if(t==T){
  45.   Process[s+t]=true;
  46.   Match(S,T,s+1,t);
  47.  }
  48.  return;
  49. }