2015 programming beauty preliminary third question prime number correlation

Source: Internet
Author: User

1. Thinking of solving problems
According to the requirements of the question, the input data first to determine whether there are 22 of the same data, and then the order from small to large, because it is not related to the prime number, the set of any one of the logarithm is not a prime correlation, I use sequential traversal, the set of each number must be inclusive and must be exclusive of two cases of traversal, First the first number of collections must be included, and as a subset of the first element, because it is ordered data, only from the second number of sets to traverse, the number of each of the subset of the addition of a prime-related judgment (need to traverse through), if the subset of each number of prime numbers independent, then the number is included in the subset until all subsequent The size of the statistical subset, and then the second pass, this is the most outer loop from the large collection of the second data, at this time the first data is drained, the second data as a subset of the first element, the subsequent traversal of the same as the front face, the statistical subset size, you can set a variable storage maximum subset, as long as the original subset of Until the last iteration of the collection is completed, the maximum subset is available at this time.
For example: Set a={2,4,8,16,32}, first time, 2 as the first element of a subset, 4/2=2 is prime, excluded, 8/2=4 non prime, included subset, at this point the subset ={2,8}, then 16/8=2, excluded, then 32/8=4,32/2=16, included subset, subset = {2,8,32}, when the subset size is 3, the second time, 4 is the first element of the subset, 8/4=2 excluded. 16/4=4, 16 inclusion, 32/16=2,32 exclusion, subset ={4,16}, third pass, 8 as subset of first element, 16/8=2 exclusion, 32/8=4 inclusion, subset ={8,32}, fourth pass, 16 as subset first, 32/16=2 exclude, subset ={16}, The fifth time, 32 is the first element is also the only one, the largest subset of the sink is 3;

Here is the C + + source code:

#include <iostream>using namespace STD;#include <algorithm>//Determine if there is the same numberBOOLIssame (Long*ps,intN) {intI,j; for(i=0; i<n;i++) for(j=0; j<n;j++) {if(* (Ps+i) ==* (ps+j) &&i!=j)return false; }return true; }//Determine if it is a prime numberBOOLIsPrime (LongN) {intIif(n<2)return false;Else{ for(i=2; i<=n/2; i++)if(n%i==0)              {return false; }      }return true; }intMain () {intT,n,maxsub;Cin>>T; for(intI=0; i<t;i++) {Cin>>N;Long*ps =New Long[N];Long*subps=New Long[N]; maxsub=0; for(intj=0; j<n;j++) {Cin>>* (PS+J); }if(!issame (Ps,n)) {cout<<"There are 22 of the same numbers!" "<<endl;          } sort (ps,ps+n); maxsub=0; for(intI=0; i<n;i++) {intCnt=1; for(intj=i;j<n;j++) {BOOLflag=true; * (subps+0) =* (ps+i); for(intk=cnt-1; k>=0; k--) {if(* (PS+J)%* (subps+k) = =0&& IsPrime ((* (PS+J))/(* (SUBPS+K))) {flag=false; Break; }                                     }if(flag==true) {* (subps+cnt) =* (PS+J);cout<<"* (ps+j)"<<* (PS+J) <<endl;                  cnt++; }              }if(maxsub<cnt) maxsub=cnt; }printf("Case #%d:%d\n", i+1, maxsub-1); }return 0; }

Original topic:
Question three: prime number correlation
Time limit: 2000ms
Single point time limit: 1000ms
Memory Limit: 256MB
Describe
Two numbers A and B (a

2015 programming beauty preliminary third question prime number correlation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.