#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int sum_score;
int store[10];
void Output() //打印出具体每次可能性具体的分值
{
for(int j=9;j>=0;--j)
cout<<store[j]<<endl;
++sum_score; //记录可能性的总共的种数
}
void Compute(int score,int num_gunshoot)//采用已经总分数 倒推的思想
{
if(score<0 || num_gunshoot > 10*(num_gunshoot+1)) //num从0开始所以+1 因为是 倒推 所以分数在递减 所以可能为负数
return;
if(num_gunshoot==0)//递归的出口
{
store[num_gunshoot]=score;
Output();
return;
}
for(int i=0;i<11;i++)//可能打中0-10环
{
store[num_gunshoot]=i;
Compute(score-i,num_gunshoot-1);
}
}
void main()//总得分90,共有10次射击机会,必须够十次(靶子最大为10环)
{
Compute(90,9);
cout<<"总的可能总数:"<<sum_score<<endl;
//printf("总的可能总数:\n",sum_score);
}
需要理解的点:
1.就是这个是已知10次,90环,所以我们考虑用倒推的思维,10次可以用 int store[10]来存储显示每一种可能下10次里每次打靶的情况。90环,用来考虑递归的条件。
2.递归条件的退出,一是达成目标中了90环,二是所剩下的射击次数就算全部10环也不能达到90环,又因为变量从0开始,需要需要 +1 在计算。
3. store[num_gunshoot]=i;
Compute(score-i,num_gunshoot-1);
这步也采用倒推的思维,我们不如先思考store[9]可能的值,它是0,1, 2,3,…,10,那么当store[9]=0时,store[8],…,store[0]有多种可能,同理,store[9]=1,…,store[9]=10时也有很多可能性。