PAT L1_006连续因子

该博客详细介绍了PAT L1-006编程题目,内容涉及输入输出格式、样例及解题思路。博主通过分析指出,对于给定正整数N,求最长连续因子个数并输出最小连续因子序列。解题关键在于理解因子不会超过N,且质数的最长连续因子为其本身。文章提供了从2开始枚举因子的解题策略。
摘要由CSDN通过智能技术生成

L1-006 连续因子 (20 分)

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

输入格式:

输入在一行中给出一个正整数 N( 1 &lt; N &lt; 2 31 1&lt;N&lt;2^{31} 1<N<231​​ )。

输出格式:

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

输入样例:

630

输出样例:

3
3*5*6

解题思路:

  1. 最长连续因子中不会出现大于 N \sqrt N N 的数
  2. N N N为质数时,最长连续因数为它自己本身
  3. 从2开始,暴力枚举因子数,同时记录最长值

解题代码

#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define clr(s) memset(s, 0, sizeof(s))
#define lowclr(s) memset(s, -1, sizeof(s))
const int inf = 0x3f3f3f3f;
#define LOCAL

int main() 
{
    #ifdef LOCAL
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(false);
    
    ull N;
    while(cin>>N)
    {
    	int maxlen=0,lim=sqrt(N),start;

    	for(int i=2;i<=lim;++i)
    	{
    		int count=0;
    		ull tmp=N;
    		int j=i;

    		while(tmp%j==0)
    		{
    			tmp/=j++;
    			count++;
    		}
    		if(count>maxlen)
    		{
    			maxlen=count;
    			start=i;
    		}
    	}
    	if(maxlen)
    	{
    		cout<<maxlen<<endl;
    		for(int i=start;i<start+maxlen;++i)	{
    			if(i!=start+maxlen-1)	cout<<i<<"*";
    			else				cout<<i<<endl;
    		}
    	}
    	else{
    		cout<<1<<endl;
    		cout<<N<<endl;
    	}
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值