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;
}
相关推荐
luofeiju几秒前
数字图像处理与OpenCV初探
c++·图像处理·python·opencv·计算机视觉
whoarethenext1 分钟前
使用 C/C++的OpenCV 将多张图片合成为视频
c语言·c++·opencv
weixin_428498491 分钟前
Catch2 开源库介绍与使用指南
c++
freyazzr18 分钟前
TCP/IP 网络编程 | Reactor事件处理模式
开发语言·网络·c++·网络协议·tcp/ip
小刘同学++1 小时前
用 OpenSSL 库实现 3DES(三重DES)加密
c++·算法·ssl
LunaGeeking2 小时前
重要的城市(图论 最短路)
c++·算法·编程·图论·最短路·floyd
君鼎2 小时前
C++内存管理与编译链接
c++
liulun3 小时前
玩转 Skia 的颜色
c++
阿伍.3 小时前
【指针】(适合考研、专升本)
c++·考研·c#
了不起的杰3 小时前
[C++][设计模式] : 单例模式(饿汉和懒汉)
c++·单例模式·设计模式