zoukankan      html  css  js  c++  java
  • L1-006. 连续因子

    一个正整数N的因子中可能存在若干连续的数字。例如630可以分解为3*5*6*7,其中5、6、7就是3个连续的数字。给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。

    输入格式:

    输入在一行中给出一个正整数N(1<N<231)。

    输出格式:

    首先在第1行输出最长连续因子的个数;然后在第2行中按“因子1*因子2*……*因子k”的格式输出最小的连续因子序列,其中因子按递增顺序输出,1不算在内。

    输入样例:

    630
    

    输出样例:

    3
    5*6*7

    枚举因子  然后判断。

    36 的结果是  2*3

    12 的结果是  2*3

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/7/14 9:17:25
    File Name     :L1-006.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 40100
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    ll n,lx,rx;
    vector<ll>v;
    int main()
    {
        #ifndef ONLINE_JUDGE
        //freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        while(cin>>n){
            v.clear();
            v.push_back(n);
            for(ll i=2;i*i<=n;i++){
                if(n%i==0){
                    if(i*i==n)v.push_back(i);
                    else v.push_back(i),v.push_back(n/i);
                }
            }
            sort(v.begin(),v.end());
            int m=v.size();
            int x,y,t;
            int ans=0;
            for(int i=0;i<m;i++){
                x=i+1;
                y=i-1;
                t=i;
                ll k=v[i];
                while(y>0&&(n%(k*v[y])==0)){
                    k*=v[y];
                    if(v[y]==v[t]-1)t=y,y--;
                    else break;
                }
                t=i;
                while(x<m&&(n%(k*v[x])==0)){
                    k*=v[x];
                    if(v[x]==v[t]+1)t=x,x++;
                    else break;
                }
                if(x-y-1>ans&&(n%k==0)){
                    ans=x-y-1;
                    lx=y;rx=x;
                }
            }
            printf("%d
    ",ans);
            for(int i=lx+1;i<rx;i++){
                printf("%lld%c",v[i],i==rx-1?10:'*');
            }
        }
        return 0;
    }
  • 相关阅读:
    jq insertBefore 的返回值
    微信公众号-定位之地址逆解析&导航
    微信JS-SDK
    Vue
    ES6-函数的扩展
    ES6-数组的扩展
    JSP
    JS
    HTML+CSS
    jdbc操作数据库
  • 原文地址:https://www.cnblogs.com/pk28/p/5669781.html
Copyright © 2011-2022 走看看