PTA Programming Question (C Language)--Continuous Factors

Title: Continuous factors Author of the title: Chen Yue, Zhejiang University

There may be several consecutive numbers in the factors of a positive integer N . For example, 630 can be decomposed into 3×5×6×7, where 5, 6, and 7 are 3 consecutive numbers. Given any positive integer N, you are required to write a program to find the number of longest continuous factors and output the smallest continuous factor sequence.

Input format:

The input gives a positive integer N in a line (1<N<231).

Output format:

First output the number of the longest continuous factors in line 1; then output the smallest continuous factor sequence in the format of 因子1*因子2*……*因子k in line 2, where the factors are output in increasing order , 1 is not included.

Input example:

630

Output sample:

3
5*6*7

This question may seem difficult at first glance, but there are some ideas if you look carefully. First, explain the relevant concepts in the question as follows.

The continuous factor sequence refers to a series of consecutive positive integers a, a+1, a+2...a+n-1 arranged in order from small to large. If their multiplication can divide N, it is called This string of numbers is a continuous factor sequence of N, where the number n of positive integers is called the length of this continuous factor sequence. For a sequence of continuous factors of a positive integer N, we need to know three facts:

(1) N must have a continuous factor sequence. For example, N itself is a continuous factor sequence of length 1.

(2) N must have a longest continuous factor sequence. Because N is finite, its sequence of continuous factors cannot be infinitely long.

(3) The longest continuous factor sequence of N may not be unique. If N has several lengths that are all the longest continuous factor sequences, then the sequence with the smallest starting factor in this factor sequence is called the smallest continuous factor sequence.

This question is to find the smallest among the longest factor sequences of N.

Idea: For a given i, we use the inner loop to find the longest factor sequence starting from i; use the outer loop to let i traverse from 2 to N, if the newly found factor sequence is longer than the previously found one , we replace the current longest continuous factor sequence with the newly found one; since the starting factor of the longest factor sequence we find each time increases from 2, the longest factor sequence found must be the smallest.

Note: The loop variable i in the outer loop traverses the starting factor, so i starts traversing from 2. As for where does i end? If N is a prime number, then we need to allow i to get N, but this will cause a test point to run timeout. If N is not a prime number, we only need to let i get sqrt(N), so it will not time out. So our solution is to let i get sqrt(N). If the minimum factor sequence is not found, it means that N must be a prime number, and just output it as a prime number.

In addition, the length of the array we use to store the factor sequence can be set to 12, because 13! will exceed the upper limit of the given positive integer N, that is to say, for the given N in the question, its longest factor sequence cannot exceed 12.

Code 1:

#include <stdio.h>
#include <math.h> 
int main () {
    int N, M, i, j, lenS = 0, lenT = 0, S[12] = {0},T[12] = {0};
    scanf("%d", &N);
    for (i = 2; i <= sqrt(N); i++) {
        M = 1;
        for (j = 0; j < 12; j++) {
            M *= (i+j);
            if (N%M == 0) {
                T[j] = i+j;
                lenT = j+1;
            } 
            else break;
        }
        if (lenT > lenS) {  // 如果新找的的连续因子序列T比S长,我们就用T替换S
            for (j = 0; j < lenT; j++) {
                S[j] = T[j];
            }
            lenS = lenT;
        }
    }
    if (lenS == 0) {   // 没有找长度大于1的连续因子序列的情况,说明N是素数。
        printf("1\n%d",N);
    } else {
        printf("%d\n", lenS);
        for (i = 0; i < lenS-1; i++) printf("%d*", S[i]);
        printf("%d",S[i]);  
    }
    return 0;
}

Optimization: Because you want to record a continuous factor, you only need to record its actual factor and length at the beginning. There is no need to use an array to record the entire factor sequence. Based on this idea, we have the following optimization

Code 2:

#include <stdio.h>
#include <math.h> 
int main () {
    int N, M, i, j, lenS = 0, lenT, S = 0, T = 0;
    scanf("%d", &N);
    for (i = 2; i <= sqrt(N); i++) {
        M = 1;
        T = i;
        for (j = 0; j < 12; j++) {
            M *= (i+j);
            if (N%M == 0) {
                lenT = j+1;
            } 
            else break;
        }
        if (lenT > lenS) {
            S = T;
            lenS = lenT;
        }
    }
    if (lenS == 0) {
        printf("1\n%d",N);
    } else {
        printf("%d\n", lenS);
        for (i = 0; i < lenS-1; i++) printf("%d*", S+i);
        printf("%d",S+i);  
    }
    return 0;
}

  For more reference codes for PTA questions, you can search for "PTA question brushing assistant" in the wx applet, or scan the QR code below

Guess you like

Origin blog.csdn.net/morn_l/article/details/134263037
Recommended