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 }
  • 相关阅读:
    codeforce1029B B. Creating the Contest(简单dp,简单版单调栈)
    spfa算法
    spfa算法
    HDU King (非连通图的差分约束,经典好题)
    HDU King (非连通图的差分约束,经典好题)
    差分约束系统相关证明(存在负环则无解证明)
    Java API 读取HDFS的单文件
    採用邻接矩阵创建图
    旧金山攻略
    一个jeecg整合activiti的学习样例,源代码下载
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11356292.html
Copyright © 2011-2022 走看看