The source code for solving the problem of the maximum number of questions under multithreading

Source: Internet
Author: User

#include <iostream> #include <windows.h> #include <time.h>using namespace std;// The array of the Declaration prime table is the global variable int a[10000]={2,0};//prime table initializes the number of int flagnumber=0;//in the list of prime numbers int minnumber=0;int  maxnumber=0;//generally need to declare the function that will be called int simple_count (int a,int b);//Tired Division Int numlist (int a[])/ /build Prime Table {  int flag=0;//determines the position of the current number of stored primes in the array  bool repeat=true;//loop flag bit setting  int j=0;  for (int i=2;i<10000;i++)//will expand the scope of the search by setting the upper limit of I  {    while (repeat)    {   if (J==flag && (i%a[flag]!=0))//To determine whether the last element of the current prime number table is not divisible, the current numbers are pushed into the prime table     {    repeat=false;    flag++;    a[ Flag]=i;   }   else   {    if (i%a[j]==0)// If you can divide out the while statement     {     repeat=false;    }     else//otherwise continue to judge     {     j++;    }   }   }  repeat=true;//i before each test, you must set the value to True   j=0;//the next number of divisor should start from a[0], so J should be 0  } return  flag;//returns the number of calculations  //in fact there may be errors, if this contains the largest number of numbers there is a large prime factor is not detected  //or there is a number because its prime factor is too many, so less calculation, there may be errors  //but on the other hand, if this number really contains the most approximate, then its prime factor must be very small  //therefore, the probability of error must be related to the number of the existence of A and B}dword winapi test ( Lpvoid pparam) { int jump=0; * ((int*) pparam) =jump;     bool  repeat=true; int testnumber=minnumber;//storage contains the maximum number of approximations for inspection (with the use of a tired division check)  int numberflag=0;//set comparison bit  int statist=1;//should set the statistical value to how much need to parse   int b[10000]={0};//initialize the current flag bit to record how many of these same primes, and initialize  int j=0;//saves the current cursor to the array of flag bits  int temp=0;//the vector  for of the cyclic division (int i=jump;i<=maxnumber;i= I+2)  {  //j can not exceed the presence of flag   for ( j=0;i>=a[j];j++)//cannot exceed the bounds of the prime table, but must exclude this number less than 10000, When this number is very small, there is no need to compare it down  &nbsP; {   if (J>flagnumber)//When the range of the out-of-prime list indicates that all elements of the prime table have been traversed    {     break;   }   else   {    temp=i;     while (temp%a[j]==0)//If divisible, flag bit plus 1, remember to remove the number of times that have been added     {      b[j]++;     //Remember that the subscripts of A and B are corresponding      temp=temp/a [j];    }   }  }  //now counts the number of public factors in this count   for (int  k=0;k<=j;k++)   {   if (b[k]!=0)    {     statist=statist* (b[k]+1);       }     }    if (numberflag<statist)//Take one of the largest numbers   {   numberflag=statist;    testnumber=i;     }  statist=1;//Reset   for ( k=0;k<=j; k++)   {   b[k]=0;    } }  cout<< "The maximum of approximately several numbers obtained by the method of using the prime table is:" <<numberflag <<endl; jump=numberflag; return 0;} Int simple_count (INT&NBSP;A,INT&NBSP;B)//When the number between A and B is around 50, you can use class division directly (by calculating the test time to indicate that {//when the value of a is greater than 100000000, each count is calculated, It takes nearly 1s of time to be ignorant: it takes about half an hour to calculate the number of  int number=0;//statistics for 100,000 numbers  int flag=0;//set the comparison number, when the next number is about a number greater than the previous number, Reset   for (int i=a;i<=b;i++)//outer loop from A to B {  for (int j=1;j<=i/2;j=j+1)// Inner loop detection from 2 to self   {      if (i%j==0)    {     number++;   }  }  number++;//itself is also an approximate, need to add in   if (number> Flag)//reset flag bit   {   flag=number;  }  number=0;//just forgot to restore the count value to 0 }  return flag;} Int main () { clock_t start,finish; start=clock ();    handle  Hhandle[2]; int handleresult1,handleresult2;&nbSp;flagnumber=numlist (a);//Get the current Prime number table, and the current prime list of flag+1, indicating that the current primes table starts from 0  cout<<flagnumber<<endl;  cout<<a[1000]<<endl;     int result;//getting the current results  cout< < "Please enter two numbers in order to get the current range";  cout<<endl; cin>>minnumber>>maxnumber; handleresult1= Minnumber; handleresult2=minnumber+1; if (MAXNUMBER-MINNUMBER&LT;50)  {  result=Simple_ Count (Minnumber,maxnumber);  } else {  hhandle[0]=createthread (NULL,0,    test, (void*) (&AMP;HANDLERESULT1), 0,null);   hhandle[1]=createthread (null,0,    Test, (void*) (&AMP;HANDLERESULT2), 0,null);  }    waitformultipleobjects (2,hHandle,TRUE, INFINITE);  finish=clock (); cout<< "THE&NBSP;TIME&NBSP;HAS&NBSP;BEEN&NBSP;USED&NBSP;:" << finish-start<<endl; return 0;}

The source code for solving the problem of the maximum number of questions under multithreading

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.