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;
}
相关推荐
ceclar12331 分钟前
C++范围操作(2)
开发语言·c++
一个不知名程序员www1 小时前
算法学习入门---vector(C++)
c++·算法
明洞日记1 小时前
【数据结构手册002】动态数组vector - 连续内存的艺术与科学
开发语言·数据结构·c++
福尔摩斯张1 小时前
《C 语言指针从入门到精通:全面笔记 + 实战习题深度解析》(超详细)
linux·运维·服务器·c语言·开发语言·c++·算法
Dream it possible!2 小时前
LeetCode 面试经典 150_二叉搜索树_二叉搜索树的最小绝对差(85_530_C++_简单)
c++·leetcode·面试
麦烤楽鸡翅3 小时前
简单迭代法求单根的近似值
java·c++·python·数据分析·c·数值分析
sulikey5 小时前
C++ 四十年:一段跨越时代的语言旅程
c++·c++40周年
-森屿安年-6 小时前
LeetCode 283. 移动零
开发语言·c++·算法·leetcode
散峰而望6 小时前
C++数组(一)(算法竞赛)
c语言·开发语言·c++·算法·github
FuckPatience9 小时前
C++ 常用类型写法和全称
开发语言·c++