L1-006 连续因子

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

输入格式:

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

输出格式:

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

输入样例:

630

输出样例:

3

567

版本一

答案正确,但是运行会超时

c 复制代码
#include<stdio.h>

int main() {
    int N, divisors[100], divisor = 0;
    scanf("%d", &N);
    for (int tmp = 2; tmp <= N; tmp++) {
        if (N % tmp == 0) {
            divisors[divisor] = tmp;
            divisor++;
        }
    }
    int start, length = 0;
    for (int tmp = 0, start_n, length_n; tmp < divisor; tmp++) {
        start_n = divisors[tmp];
        length_n = 1;
        int temp, product=divisors[tmp]*divisors[tmp+1];
        for (temp = tmp+1;temp<divisor&&divisors[temp]-1==divisors[temp-1]&&N%product==0;temp++){
            length_n++;
            product*=divisors[temp+1];
        }
        if(length_n>length){
            start=start_n;
            length=length_n;
        }
        if(temp==divisor)
            break;
    }
    printf("%d\n", length);
    for (int tmp = 0; tmp < length - 1; tmp++)
        printf("%d*", start + tmp);
    if(length!=0)
        printf("%d\n", start + length - 1);
    return 0;
}

版本二

答案正确,同时运行不会超时

c 复制代码
#include<stdio.h>
#include <math.h>

int main() {
    int N, divisors[100], divisor = 0, continuous[100], big_divisors[100];
    scanf("%d", &N);
    for (int tmp = 2; tmp <= pow(N,0.5); tmp++) {
        if (N % tmp == 0) {
            divisors[divisor] = tmp;
            big_divisors[divisor]=N/tmp;
            divisor++;
        }
    }
    for(int tmp=divisors[divisor-1]*divisors[divisor-1]==N?divisor-2:divisor-1;tmp>=0;tmp--){
        divisors[divisor]=big_divisors[tmp];
        divisor++;
    }
    divisors[divisor]=N;
    divisor++;
    int start, length = 1;
    for(int tmp=0;tmp<divisor;){
        for(int temp=tmp+1;divisors[temp-1]==divisors[temp]-1;temp++)
            length++;
        for(int temp=length;temp>0;temp--)
            continuous[tmp+length-temp]=temp;
        tmp+=length;
        length=1;
    }
    length = 0;
    for (int tmp = 0, start_n, length_n; tmp < divisor; tmp++) {
        if(continuous[tmp]<length)
            continue;
        start_n = divisors[tmp];
        length_n = 1;
        int temp, product=divisors[tmp]*divisors[tmp+1];
        for (temp = tmp+1;temp<divisor&&divisors[temp]-1==divisors[temp-1]&&N%product==0;temp++){
            length_n++;
            product*=divisors[temp+1];
        }
        if(length_n>length){
            start=start_n;
            length=length_n;
        }
        if(temp==divisor)
            break;
    }
    printf("%d\n", length);
    for (int tmp = 0; tmp < length - 1; tmp++)
        printf("%d*", start + tmp);
    if(length!=0)
        printf("%d\n", start + length - 1);
    return 0;
}
相关推荐
码农豆豆22 分钟前
4.C++中程序中的命名空间
开发语言·c++
Joker1008523 分钟前
C++初阶学习——探索STL奥秘——标准库中的priority_queue与模拟实现
c++
怀九日26 分钟前
C++(学习)2024.9.19
开发语言·c++·学习·重构·对象·
KookeeyLena839 分钟前
如何限制任何爬虫爬取网站的图片
开发语言·c++·爬虫
m_Molly1 小时前
vs2022配置opencv==4.9.0(C++)
c++·opencv
charon87781 小时前
Unreal Engine 5 C++: 编辑器工具编写入门(中文解释)
c++·ue5·编辑器·游戏引擎·虚幻
Ddddddd_1581 小时前
C++ | Leetcode C++题解之第421题数组中两个数的最大异或值
c++·leetcode·题解
末央&1 小时前
【C++】内存管理
java·开发语言·c++
YRr YRr2 小时前
在Windows上安装WSL2和Ubuntu 20.04以搭建C++开发环境的详细指南
c++·windows·ubuntu·wsl2
Farewell_me2 小时前
C++模拟实现list:list、list类的初始化和尾插、list的迭代器的基本实现、list的完整实现、测试、整个list类等的介绍
c++·list