1096. Consecutive factors (--PAT) practise

Source: Internet
Author: User

Topic Information

1096. Consecutive factors (20)

Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Among all the factors of a positive an integer N, there may exist several consecutive numbers. For example, 630 can is factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you is supposed to find the maximum number of consecutive factors, and list the smallest Sequen Ce of the consecutive factors.

Input Specification:

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

Thinking of solving problems

Discussion on the two cases of odd and even

AC Code
#include <algorithm>#include <set>#include <queue>#include <map>#include <stdio.h>#include <iostream>#include <string>#include <memory.h>#include <limits.h>using namespace STD;voidDfsfactor (Long LongNLong LongPrenum, vector<int>&factor, vector<vector<int>>&ans,int&factorsize) {if(n% (Prenum +1) ==0) {Factor.push_back (Prenum +1); Dfsfactor (n/(Prenum +1), Prenum +1, factor, ans, factorsize); }Else if(!factor.empty () && factor.size () > Factorsize)        {factorsize = Factor.size ();    Ans.push_back (factor); }}BOOLcmpConst  vector<int>&a,Const  vector<int>&AMP;B) {if(A.size () > B.size ())return true;Else if(a.size () = = B.size () && a[0] < b[0])return true;Else return false;}intMainvoid){intNCin>> N;if(n%2==1)        {inttemp = (int)((Double)sqrt(n) +1); for(inti =2; I <= temp; ++i) {if(n%i = =0)                {cout<<"1"<< Endl;cout<< i << Endl;return 0; }            }cout<<"1"<< Endl;cout<< n << Endl; }Else{ vector<int>Maxfactor = {Int_max, n, (int)((Double)sqrt(n) +1),1290,215, the, *, +, -,Ten,0};//3~13             vector<int>Factor0); vector<vector<int>>Ans0);intFactorsize =0; for(Long Longi =1; I <= min (n, maxfactor[factorsize +1]); i++) {if(n% (i +1) ==0) {Dfsfactor (n, I, factor, ans, factorsize);                Factor.clear (); }} sort (Ans.begin (), Ans.end (), CMP);cout<< ans[0].size () << Endl; for(inti =0; I < ans[0].size (); i++) {cout<< ans[0][i];if(I! = ans[0].size ()-1)cout<<"*"; }cout<< Endl; }return 0;}

1096. Consecutive factors (--PAT) practise

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.