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;
}
相关推荐
tan180°1 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
彭祥.2 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
lzb_kkk3 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
胖大和尚4 小时前
clang 编译器怎么查看在编译过程中做了哪些优化
c++·clang
钱彬 (Qian Bin)5 小时前
一文掌握Qt Quick数字图像处理项目开发(基于Qt 6.9 C++和QML,代码开源)
c++·开源·qml·qt quick·qt6.9·数字图像处理项目·美观界面
双叶8366 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
源代码•宸6 小时前
C++高频知识点(二)
开发语言·c++·经验分享
jyan_敬言8 小时前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
liulilittle8 小时前
SNIProxy 轻量级匿名CDN代理架构与实现
开发语言·网络·c++·网关·架构·cdn·通信
tan77º9 小时前
【Linux网络编程】Socket - UDP
linux·服务器·网络·c++·udp