C ++ programming resolve -P1036 number selected - function

Question 1
Question 2
Problem-solving ideas:
the problem is simple. In the selection of the number, we start from the left side to select a starting point, by combining the right number, to the number k, while, in which this process, the data to the selected accumulated sum. K is the number of simultaneous arrival, and to judge whether the condition is satisfied. It is also desired to meet the situation.

program:

#include<iostream>
#include<cmath>
using namespace std;
int n,k;
int num[20];    //存储数字 
int total;      //种数 
//素数判断 
bool isPrime(int num){
	for(int i = 2;i <= sqrt(num);i++)
		if(num%i == 0)
			return false;
	return true;
}
//查找所有可能性 
void search(int choose,int count,int sum){
	if(count == k){
		if(isPrime(sum))
			total++;
		return;
	}
	//挑数 
	for(int i = choose;i < n;i++){
		search(i+1,count+1,sum+num[i]);
	}
}
int main(){ 
	cin>>n>>k;
	for(int i =0;i < n;i++)
		cin>>num[i];
	search(0,0,0);
	cout<<total;
	return 0;
}

Sample test procedures and results:
Sample test procedures and operating results
The knowledge that we continue to use the function to solve a true problem. For more content, please pay attention: Informatics Olympiad NOIP clearance necessary -16 function (original price 199 price 9.9) - Netease cloud classroom https://study.163.com/course/courseMain.htm?courseId=1209401803&share= 2 & shareId = 480000001918401

Now, with the advent of full artificial intelligence, children's programming is a general trend, C ++ programming informatics is one of the key learning content.

Dr. ape ape children's programming is the wisdom of the classroom classroom knowledge to build a column. Aims answering questions for problems encountered in the study of children's programming, while providing problem-solving ideas, training of computational thinking, to boost growth. Children's programming wisdom ape classroom has been committed to children's educational programming Chinese youth Pratt & Whitney, determined to do to get children interested in learning green programming information science can afford, learn to understand, will learn programming class. Since the line is also supported by many parents of students, and parents in order to repay student, our courses have recently begun a new round of promotions. A minimum fold, so stay tuned!

Hello world

Published 34 original articles · won praise 4 · views 10000 +

Guess you like

Origin blog.csdn.net/xingzhe_666/article/details/102690233