PAT A 1096 Consecutive Factors

Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:

Each input file contains one test case, which gives the integer N (1

#include<cstdio>
#include<cmath>

typedef long long ll;

int main() {
    ll n;
    scanf("%lld", &n);
    ll sqr = (ll)sqrt(n);
    ll len = -1, digit1 = 0;
    for (ll i = 2; i <= sqr; i++) {//精髓
        ll temp =1 , j = i;
        while (1) {
            temp *= j;
            if (n % temp != 0) break;
            if (j - i + 1 > len) len = j - i + 1, digit1 = i;
            j++;
        }
    }
    if (len == -1) printf("1\n%lld", n);//特判
    else {
        printf("%lld\n", len);
        for (ll i = 0; i < len; i++) {
            printf("%lld", digit1 + i);
            if (i < len - 1) printf("*");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Joah_Ge/article/details/81081899