zoukankan      html  css  js  c++  java
  • PAT1096:Consecutive Factors

    1096. Consecutive Factors (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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

    思路
    因为12! < 2^31 < 13!。
    所以,因子大小不会超过12!,连续因子的长度不会超过12。
    从长度为12开始减小,令res为满足条件的最大因子,穷举所有可能直到满足N % res == 0就行。
    注:穷举的过程中res会溢出,不过不影响结果。。
    代码
    #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
        int N;
        while(cin >> N)
        {
            int maxnum = sqrt(N);
            for(int len = 12;len >= 1;len--)
            {
                for(int start = 2; start <= maxnum;start++)
                {
                   int res = 1;
                   for(int i = start;i - start < len;i++)
                   {
                       res *= i;
                   }
                   if(N % res == 0)
                   {
                       cout << len << endl << start;
                       for(int j = start + 1;j - start < len;j++)
                        cout << "*" << j;
                       return 0;
                   }
                }
            }
            cout << 1 << endl;
            cout << N;
        }
    }
    

      

  • 相关阅读:
    pipenv install
    git删除缓存区中文件
    zsh切换bash bash切换zsh
    Mac下安装allure
    Linux基础命令之tail动态显示日志文件时关键字有颜色、高亮显示
    CentOS添加tailf命令
    阿里云服务器添加安全组,允许url+端口访问
    pytest中用例的识别与运行
    [转] 使用webpack4提升180%编译速度
    [转] 默认分包策略
  • 原文地址:https://www.cnblogs.com/0kk470/p/7881388.html
Copyright © 2011-2022 走看看