55(2023.12.28)(选数)选不出来

55(2023.12.28)(选数)选不出来_第1张图片

#include
using namespace std;

int test(int n,int k)
{
	int test = 1;
	for (int i = n;i >= n - k;i--)
	{
		test *= i;
	}
	for (int i = k;i >= 1;i--)
	{
		test /= i;
	}
	return test;
}

bool test2(int num)
{
	if (num == 2)return true;
	else if (num > 2)
	{
		for (int i = 2;i * i <= num;i++)
		{
			if (num % i == 0)
			{
				return false;
			}
		}
	}
	return true;
}

int main()
{
	int arr[20] = {};
	int n, k;
	cin >> n >> k;
	int arr2[20] = {};
	int arr3[20] = {};
	int times = 0;
	for (int i = 0;i < n;i++)
	{
		cin >> arr[i];
	}
	int flag = test(n, k);
	int sum;
	int start = n - 1;
	for (int i = 0;i < k;i++)
	{
		arr3[i] = i;
	}
	for (int i = 0;i < k;i++)
	{
		arr2[i] = arr[i];
	}
	int flag2 = k - 1;
	for (int i = 0;i < flag;i++)
	{
		sum = 0;
		for (int j = 0;j < k;j++)
		{
			arr2[j] = arr[arr3[j]];
			//cout << arr2[j] << ' ';
			sum += arr2[j];
		}
		//cout << endl;
		if (test2(sum))times++;
		if (k!=2&&arr3[0] == n - k - 1 && arr3[k - 1] == n - 1)break;
		arr3[k - 1]++;
		for (int j = k - 1;j >= 0;j--)
		{
			if (arr3[j] == n-(k-1-j))
			{
				arr3[j - 1] += 1;
				int hehe = 1;
				for (int z = j;z < k;z++)
				{
					arr3[z] = arr3[j - 1] + hehe;
					hehe += 1;
				}
			}
		}
	}
	cout << times << endl;
	system("pause");
	return 0;
}

 55(2023.12.28)(选数)选不出来_第2张图片

你可能感兴趣的:(算法,数据结构,排序算法)