zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    C++ 沉思录——Chap6:句柄2
    C++ 沉思录——Chap5:代理类
    C++ 沉思录——Chap4:设计类的核查表
    Linux 网卡驱动相关——01
    FCoE的提出
    想成为嵌入式程序员应知道的0x10个基本问题
    C++ 沉思录——Chap6:句柄
    C++ 沉思录——Chap8:一个面向对象程序范例
    数据库调优积累系列(1):索引
    QTP使用问题集锦
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11356292.html
Copyright © 2011-2022 走看看