zoukankan      html  css  js  c++  java
  • A1096. 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<N<231).

    Output Specification:

    For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format "factor[1]*factor[2]*...*factor[k]", where the factors are listed in increasing order, and 1 is NOT included.

    Sample Input:

    630
    

    Sample Output:

    3
    5*6*7

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<math.h>
     5 using namespace std;
     6 int main(){
     7     int N, sqr, maxLen = -1, index;
     8     long long P;
     9     scanf("%d", &N);
    10     sqr = (int)sqrt(N * 1.0);
    11     for(int i = 2; i <= sqr + 1; i++){
    12         int j = i;
    13         P = 1;
    14         while(1){
    15             P *= j;
    16             if(P < 0)
    17                 break;
    18             if(N % P != 0)
    19                 break;
    20             if(j - i + 1 > maxLen){
    21                 maxLen = j - i + 1;
    22                 index = i;
    23             }
    24             j++;
    25         }
    26     }
    27     if(maxLen != -1){
    28         printf("%d
    ", maxLen);
    29         printf("%d", index++);
    30     }else{
    31         printf("%d
    %d", 1, N);
    32     }
    33     for(int i = 1; i < maxLen; i++){
    34         printf("*%d", index++);
    35     }
    36     cin >> N;
    37     return 0;
    38 }
    View Code

    总结:

    1、测试630的序列,应该是测试 630 / 3, 630 /(3 * 4), 630 / (3 * 4 * 5),而非测试630 / 3,630 / 4, 630 / 5。

    2、 考虑到无解的可能,比如素数13, index 与maxlen没有进入循环,应单独输出。

    3、有可能sqrt(N)后有误差,应该判断到 sqrt(N + 1)而非 sqrt(N)。

    4、只要有累乘,就应该用long long,并且考虑到溢出为负的可能。

  • 相关阅读:
    整数拆分问题的四种解法
    通配符匹配算法
    grundland去色
    rgb和lab的转换
    .Net library that makes converting between color spaces and comparing colors easy
    彩色图像灰度化论文
    ps图片黑白调整算法
    分数化小数算法
    计算机安全专有名词
    入侵检测
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8520149.html
Copyright © 2011-2022 走看看