L1-006 連續因子 (20分)


題意分析

題目中已經將意思說的很清楚了,就是輸出一個數的最長連續因子的個數,並且輸出是哪幾個因子相乘。可以將題目從這兩個角度進行分析:

  • N為素數時,最長連續因子的個數為1,即它自己。
  • N不為素數時,即N為合數時,暴力模擬即可,將連續的數進行累積,直到累積后的結果不能被N整除為止,這樣就能夠不斷更新最長連續因子的個數,預保留第一個數,就可以在最終輸出是能夠直接輸出這幾個連續因子。

AC代碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
int N;
bool isprime(int n)
{
    if(n == 2)
        return true;
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0)
            return false;
    }
    return true;
}
int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    scanf("%d", &N);
    if(isprime(N))
    {
        cout << 1 << endl;
        cout << N << endl;
    }
    else
    {
        int maxlen = 0, x;
        for(int i = 2; i <= sqrt(N); i++)
        {
            if(N % i == 0)
            {
                int sx = i;
                int j;
                for(j = i + 1; j <= sqrt(N); j++)
                {
                    sx *= j;
                    if(N % sx != 0)
                        break;
                }
                if(maxlen < j - i)
                {
                    maxlen = j - i;
                    x = i;
                }
            }
        }
        cout << maxlen << endl;
        for(int i = x; i <= x + maxlen - 1; i++)
        {
            if(i != x)
                cout << "*";
            cout << i;
        }
        cout << endl;
    }
}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM