递归等练习程序
作者:拉格浪日 标签:编程 | 阅读次数: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 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 种 |