zoukankan      html  css  js  c++  java
  • PAT 1096 Consecutive Factors

    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
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    
    int main(){
        ll n;
        cin >> n;
        int maxlen = 0;
        ll pos = 0;
    
    //    if(n==1){cout << 0 << endl;return 0;}
    //    if(n==2){cout << 1 << endl << 2;return 0;}
    //    if(n==3){cout << 1 << endl << 3;return 0;}
    
    
    
    
    
    
        for(ll i=2;i <= sqrt(n)+1;i++){
            int cnt = 0;
            ll t = i;
            ll nt = n;
            while(nt%t == 0&&nt){
                cnt++;
                nt = nt/t;
                t++;
            }
            if(cnt > maxlen){
                maxlen = cnt;
                pos = i;
            }
        }
        if(maxlen == 0){
            cout << 1 << endl << n;
            return 0;
        }
    
        cout << maxlen << endl;
        for(int i=0;i < maxlen;i++){
            cout << pos+i;
            if(i!=maxlen-1)
                cout << "*";
        }
        return 0;
    }
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    
    int main(){
        ll n;
        cin >> n;
        int maxlen = 0;
        ll pos = 0;
    
    //    if(n==1){cout << 0 << endl;return 0;}
    //    if(n==2){cout << 1 << endl << 2;return 0;}
    //    if(n==3){cout << 1 << endl << 3;return 0;}
    
    
    
    
    
    
        for(ll i=2;i <= sqrt(n)+1;i++){
            int cnt = 0;
            ll t = i;
            ll nt = n;
            while(nt%t == 0&&nt){
                cnt++;
                nt = nt/t;
                t++;
            }
            if(cnt > maxlen){
                maxlen = cnt;
                pos = i;
            }
        }
        if(maxlen == 0){
            cout << 1 << endl << n;
            return 0;
        }
    
        cout << maxlen << endl;
        for(int i=0;i < maxlen;i++){
            cout << pos+i;
            if(i!=maxlen-1)
                cout << "*";
        }
        return 0;
    }

    如果n为质数的情况要特殊判断。

     
  • 相关阅读:
    细数阿里云在使用 Docker 过程中踩过的那些坑
    细数阿里云在使用 Docker 过程中踩过的那些坑
    javascript – 从页面停用浏览器打印选项(页眉,页脚,页边距)?
    jquery children()方法
    jquery 获取输入框的值
    java BigDecimal加减乘除
    window.print()打印时,如何自定义页眉/页脚、页边距
    深入分析:12C ASM Normal冗余中PDB文件块号与AU关系与恢复
    我不是药神,救不了你的穷根
    Install fail! Error: EPERM: operation not permitted
  • 原文地址:https://www.cnblogs.com/cunyusup/p/10794194.html
Copyright © 2011-2022 走看看