• PAT甲级——A1096 Consecutive Factors【20】


    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<).

    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 <iostream>
     2 #include <vector>
     3 #include <cmath>
     4 using namespace std;
     5 int N, num = 0, first = -1;
     6 int main()
     7 {
     8     cin >> N;
     9     for (int i = 2; i <= (int)sqrt(N*1.0); ++i)//2~根号N
    10     {
    11         if (N%i == 0)
    12         {
    13             int nn = 0;
    14             for (int j = i; N%j == 0; j*=i+nn)//从i开始的连续数字,确保能连续除下去,而不是除以一个数字
    15                 ++nn;
    16             if (nn > num)//更新最长数字串
    17             {
    18                 first = i;
    19                 num = nn;
    20             }
    21         }
    22     }
    23     if (num == 0)//N就是质数
    24         cout << 1 << endl << N << endl;
    25     else
    26     {
    27         cout << num << endl;
    28         for (int i = 0; i < num; ++i)
    29             cout << first + i << (i == num - 1 ? "" : "*");
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    oralce 11g data guard
    oracle的锁与并发机制
    10 个MySQL数据库备份教程推荐
    Linux环境下用exp备份Oracle数据表并导入的脚本
    使用Oracle 10g的Logminer挖掘日志
    ORACLE查询表最近更改的数据
    如何监控oracle的索引是否使用
    看 淡 一切 生 命 只 是 个 过 程
    Java数组声明、创建、初始化
    Linux建立FTP的方法
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11356292.html
Copyright © 2020-2023  润新知