算法作业——打靶问题

问题:一个射击运动员打靶,靶一共有 10 环,连开 6 枪打中 45 环的可 能性有多少种? (每一枪的成绩,最少是 0 环,最多是 10 环)

代码:

#include "pch.h"
#include 
using namespace std;
int sum;
int store[10];
void compute(int score, int num)
{
	if (score<0 || score>(num + 1) * 10)
		return;
	if (num == 0)
	{
		store[num] = score;
		for (int i = num; i >= 0; --i)
			++sum;
		return;
	}
	for (int i = 0; i <= 10; ++i)
	{
		store[num] = i;
		compute(score - i, num - 1);
	}
}

int main()
{
	int a, b;
	cin >> a >> b;
	cout << "环数: " << a << endl;
	cout << "次数: " << b << endl;
	compute(a,b-1);
	cout << "总数:" << sum << endl;
	return 0;
}

结果:、

算法作业——打靶问题_第1张图片

你可能感兴趣的:(算法)