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;
}
相关推荐
superman超哥15 分钟前
仓颉内存管理深度探索:引用计数的实现原理与实战
c语言·开发语言·c++·python·仓颉
TechPioneer_lp25 分钟前
27届暑期实习内推:网易美团京东快手等
数据结构·c++·人工智能·笔记·机器学习·面试
小无名呀40 分钟前
RAII 智能指针
c++
Once_day1 小时前
CC++八股文之基础语法(2)
c语言·c++
dawnButterfly1 小时前
C 语言标准、编译器与操作系统的关系
c语言·开发语言·c++
程序员龙一1 小时前
进程、线程、协程通俗讲解与对比
c++·线程·进程·协程
Dream it possible!2 小时前
LeetCode 面试经典 150_回溯_单词搜索(104_79_C++_中等)
c++·leetcode·面试·回溯
superman超哥2 小时前
仓颉语言智能指针深度实战:突破 GC 与所有权的边界
c语言·开发语言·c++·python·仓颉
八月的雨季 最後的冰吻2 小时前
FFmepg-- 39-ffplay源码-ffplay 播放器中视频输出和尺寸变换
c++·音视频
AuroraWanderll2 小时前
类和对象(四):默认成员函数详解与运算符重载(下)
c语言·数据结构·c++·算法·stl