ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++解PTA A1096Conse

2022-01-23 09:03:21  阅读:161  来源: 互联网

标签:factors ll A1096Conse length C++ 因子 PTA ans consecutive


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

几乎所有整数的N的因子,可能存在连续的数字,举个例子,630因子可以是,356*7,5,6,7是三个因子,现在给你一个个整数N,要求你找出最大的连续因素,列出最小的连续因子。

Input Specification:
Each input file contains one test case, which gives the integer N (1<N<2 31).

输入规格,每个输入文件包含一个测试样例,给出一个N大于1小于2^31

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.

输出规格,针对每个测试文件,第一行打印最大的连续因子数,然后第二行打印最小连续因子序列,以这种形式因子1*因子2,所有因子升序,1不包括进去.

Sample Input:

630

Sample Output:

3
5*6*7

核心思路

要完整模拟整个过程,建议数据要长整型。

完整代码

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
    //亮点,长质数
    ll N;
    cin >> N;
    ll i,j,k;
    ll ans_start = N;
    ll ans_length = 1;
    for(i=2;i<=sqrt(N);i++){
        for(j=i,k=N;k%j==0;j++){
            k/=j;
        }
        if(j-i>ans_length||(j-i==ans_length&&i<ans_start)){
            ans_length =j-i;
            ans_start = i;
        }
    }
    cout << ans_length << endl;
    for(i=ans_start;i<ans_start+ans_length;i++){
        if(i>ans_start)cout << "*";
        cout << i;
    }
}


标签:factors,ll,A1096Conse,length,C++,因子,PTA,ans,consecutive
来源: https://blog.csdn.net/m0_37149062/article/details/122647125

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有