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

2023-12-29 06:30:05

#include<iostream>
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;
}

?

文章来源:https://blog.csdn.net/weixin_72770922/article/details/135258712
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。