Huawei machine test Online Training prime factor

Subject description:

Function: enter a positive integer, according to the number of all prime factors of the order of small to large outputs it (the prime factors is 180, such as 22,335)

Finally, a number also have space behind

answer:

This silly question, there is no need to consider whether the issue is of prime numbers.

I take into account that if 46 the number of non-quality in addition to how to deal with besides. But in fact the dividend from 2 increments up, if they can not be divisible increase, to see the result of the division can continue divisible.

The 180/2 = 90.2 Fixed

90/2 = 45,2 Fixed

No 45/2, 2 + 1 = 3

45/3 = 15.3 Fixed

. . . . And so on

 

8 was added to the dividend such assumption, it must not be divisible by 8, there is a factor of two after 8. If divisible by 8, then there will be more in addition to a few times before 2, the dividend will not be directly added to the 8.

Code 1:

 1 #include <iostream>
 2 using namespace std;
 3  
 4 int main()
 5 {
 6     long num;
 7      
 8     while(cin >> num){
 9         if(num == 1){
10             cout << num << endl;
11             continue;
12         }
13          
14         for(int i = 2; i <= num; ++i){
15            if(num%i == 0){              
16                 NUM = NUM / I;
 . 17                 COUT I << << "  " ;
 18 is                 i--; // encounters a prime number, then the number of the mass may be divided by a plurality of times 
. 19             }          
 20 is          }
 21 is          COUT << endl;       
 22 is      }   
 23 is       
24      return  0 ;
 25 }

 

 

You may also improve complexity, where the dividend for the cycle to be sqrt (num), i.e. square root arithmetic num far. Because if i dividend increased root num, num already described then impossible num factor is less than the root. So take the square root square root of num num num is already equal to the. Once the num number i is greater than the root, has the i ^ 2 must be greater than the num, num then no longer a factor may have other (less than num own factor).

The time complexity of O (N) changed to O (LOGN)

Code 2:

 1 #include <iostream>
 2 #include <math.h>
 3 using namespace std;
 4  
 5 int main()
 6 {
 7     long num;
 8      
 9     while(cin >> num){
10         if(num == 1){
11             cout << num << endl;
12             continue;
13         }
14          
15         for(int i = 2; i <= sqrt(num); ++i){
16            if(% NUM I == 0 ) {              
 . 17                 NUM = NUM / I;
 18 is                 COUT I << << "  " ;
 . 19                 i--; // encounters a prime number, then the number of the mass may be divided by a plurality of times 
20             }          
 21          }
 22 is          IF (NUM> . 1 ) {
 23 is              COUT NUM << << "  " ;
 24          }
 25          COUT << endl;       
 26 is      }   
 27       
28      return  0 ;
 29 }

 

Guess you like

Origin www.cnblogs.com/FdWzy/p/12315613.html