C++解PTA A1096Conse
2022/1/23 9:04:13
本文主要是介绍C++解PTA A1096Conse,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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; } }
这篇关于C++解PTA A1096Conse的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-17zero-shot-learning-definition-examples-comparison
- 2024-06-06Package Easy(基于 NSIS 的打包exe安装包工具)使用方法-icode9专业技术文章分享
- 2024-06-06基于 casdoor 的 ELK 开源登录认证解决方案: elk-auth-casdoor-icode9专业技术文章分享
- 2024-05-29Elasticsearch慢查询日志配置
- 2024-05-29揭秘华为如此多成功项目的产品关键——Charter模板
- 2024-05-29海外IDC业务拓展的7大挑战
- 2024-05-29InLine Chat功能优化对标Github Copilot,CodeGeeX带来更高效、更直观的编程体验!
- 2024-05-29CodeGeeX 智能编程助手 6 项功能升级,在Visual Studio插件市场霸榜2周!
- 2024-05-29AutoMQ 生态集成 Apache Doris
- 2024-05-292024年IDC行业的深度挖掘:机遇、挑战与未来展望