藏书阁 史将 特技
背景 三国演义 PC版
首页 -> 精彩文章 -> 文章
转播到腾讯微博
递归等练习程序
作者拉格浪日
标签编程
阅读次数:91
1 递归
打靶问题 ,每次打0到10环,打了10次,共得到90分,问有多少种排列方法?
我的代码
#include
#include

using namespace std;

int a[11]={};
int levels=10;
int shots=10;
int totalscores=90;
int curlevel=1;
unsigned int total=0;
void countWays(int level,int curscore,int scores)
{
  int j,k;
 
  if(curscore<0)
  return;
  // printf("\n%d %d",level,curscore);
  if((level==levels)&&(curscore==0))
  {
   a[level]=scores;
  /* for(j=1;j<=levels;j++)
   printf("%d ",a[j]);
   printf("\n");*/
   total++;
   }
   else if(level    a[level]=scores;
   
    for(k=0;k<=shots;k++)
    {
     countWays(level+1,curscore-k,k);
    }
   
   }
   else
   {
   return;
   }
}
int main(int argc, char *argv[])
{
  int i;
  clock_t start,end;
  double opetime;
  start= clock();

  for(i=0;i<11;i++) //scores from 0 to 10
  {
    countWays(curlevel,totalscores-i,i); 
    printf("%d",i);       
  }
  end=clock();
  opetime=(double)(end-start)/CLOCKS_PER_SEC;
  printf("\n %f 秒",opetime);
  printf("\ntotal =%ld 种",total);
  //system("PAUSE");
  return EXIT_SUCCESS;
}
在unix-center fedora 系统上得到结果为
01234567890
273.820000 秒
total=92378 种

©1998-2012 5iSanGuo.com 版权所有.保留所有权利.
浙ICP备06020153号-1 浙公网安备 33010602002334号


浙ICP备06020153号-1