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,并且考虑到溢出为负的可能。

  • 相关阅读:
    通过USB转TTL串口下载stm32程序
    实验一:基于STM32F1的流水灯实验(库函数)
    opencv 常用头文件介绍
    OpenCV 1.0在VC6下安装与配置(附测试程序)
    在Angularjs使用中遇到的那些坑
    js和ts关于遍历的几个小总结
    angularjs的启动方式
    关于跨域和如何解决跨域问题的小结
    TypeScript(入门)
    截取字符串部分汇总
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8520149.html
Copyright © 2011-2022 走看看