題意分析

題目中已經將意思說的很清楚了,就是輸出一個數的最長連續因子的個數,并且輸出是哪幾個因子相乘,可以將題目從這兩個角度進行分析:
- 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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/107646.html
標籤:其他
上一篇:差分模板
下一篇:798. 差分矩陣 (二維差分)
