PAT Grade --1096 Consecutive Factors (math)

This paper released simultaneously in CSDN: https://blog.csdn.net/weixin_44385565/article/details/91349859

1096 Consecutive Factors (20 分)
 

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:

Each input file contains one test case, which gives the integer N (1<N<).

Output Specification:

For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]*factor[2]*...*factor[k], where the factors are listed in increasing order, and 1 is NOT included.

Sample Input:

630

Sample Output:

3
5*6*7

Title effect: a digital multiplication factor can be written in a number of forms, to find a sequence of consecutive numbers is formed in these factors, so that the maximum number of members of this sequence, up to the number of output membership value and the minimum sequences, multiplying written format.

Ideas: From 2 to traverse directly to the sqrt (N) +1, find divisible continuous product N, the consecutive numbers of this product into tmpAns array, size of the largest set of sequence is the answer.

. 1 #include <the iostream>
 2 #include <Vector>
 . 3 #include <the cmath>
 . 4  the using  namespace STD;
 . 5  int N, X;
 . 6 Vector < int > ANS, tmpAns;
 . 7  int main ()
 . 8  {
 . 9      int I, Product = . 1 ;
 10      Scanf ( " % D " , & N);
 . 11      X = sqrt (N) + . 1 ;
 12 is      / * find the largest sequential number, divisible by the product of successive N * / 
13 is      for (I =2 ; I <= X; I ++ ) {
 14          Product * = I;
 15          tmpAns.push_back (I);
 16          IF (Product <Product% N = N && == 0 && tmpAns.size ()> ans.size () ) // product is less than N and N is divisible and can be up to the current running digital found 
. 17              ANS = tmpAns;
 18 is          the else  IF ! (Product> Product% N = N || 0 ) { // product is greater than or newly added N i such that the product can not be divided by N 
. 19              tmpAns.clear ();
 20 is              IF (! i% N = 0 ) // new i is not divisible added N, initializes the product 
21 is                  product = . 1;
 22 is              the else { // newly added N i is a factor, the value of the product means that more than N, then remove the node from the start in front, so that the product is less than the N 
23 is                  int tmpPro = product, J;
 24                  for (J = 2 ; J <= I; J ++ ) {
 25                      tmpPro = tmpPro / J;
 26 is                      IF (tmpPro <= N && N% tmpPro == 0 ) {
 27                          Product = tmpPro;
 28                          BREAK ;
 29                      }
 30                  }
 31 is                  for (J ++; J < I =; J ++ )
 32                     tmpAns.push_back (J);
 33 is              }
 34 is          }
 35      }
 36      / * N is a prime number itself when the output * / 
37 [      IF (ans.size () == 0 )
 38 is          the printf ( " . 1 \% n-D \ n- " , N);
 39      the else {
 40          the printf ( " % D \ n- " , ans.size ());
 41 is          for ( int I = 0 ; I <ans.size (); I ++ ) {
 42 is              IF (I =! 0 )
 43                 printf("*");
44             printf("%d", ans[i]);
45         }
46     }
47     return 0;
48 }

 

Guess you like

Origin www.cnblogs.com/yinhao-ing/p/10992481.html