1096. Consecutive Factors (20)
来源:互联网 发布:js如何防止重复提交 编辑:程序博客网 时间:2024/05/08 07:08
这道题目由于数据很大,所以选用long long类型,算法比较容易想到,但是下面代码一直运算超时,应该改进一些
#include <iostream>#include <cstdio>#include <vector>using namespace std;vector<long long> result;const long long MAX=10000000;long long factor[MAX],factorLen=1;void FindConsecutive(long long factor[]);long long mult(long long& sta,long long& end);int main(){long long n;cin>>n;for(long long i=2;i<=n;i++){if(n%i==0)factor[factorLen++]=i;}factor[factorLen]='\0';FindConsecutive(factor);if(result.size()==0){result.push_back(factor[1]);//如果没有连续因数,就只能把除了1之外第二大因数压入 result.push_back(factor[1]);//要一直保证里面至少有两个数 } vector<long long>::iterator p=result.end();p--;//由于数组末尾是'\0',所以向前一步</span>long long sec=*p;p--;long long fir=*p;cout<<sec-fir+1<<endl;for(long long i=fir;i<sec;i++)cout<<i<<'*';cout<<sec;return 0;}long long mult(long long& sta,long long& end){long long resulto=1;for(long long i=sta;i<=end;i++)resulto=resulto*i;return resulto;}void FindConsecutive(long long factor[]){long long longestLen=0;for(long long p1=1;p1<=factorLen-1;p1++){bool flag=false;long long p2=factorLen-1;//由于数组末尾是'\0',所以向前一步 while(p1!=p2){if((factor[p1]-factor[p2])==(p1-p2)){//找出最大连续因数 for(long long pTest=p2+1;pTest<=factorLen-1;pTest++){if(mult(factor[p1],factor[p2])==factor[pTest]&&p2-p1+1>longestLen){longestLen=p2-p1+1;//最长连续因数个数更新 result.push_back(factor[p1]);//把满足要求的连续的因数全部压入数组中 result.push_back(factor[p2]);flag=true;break;} }}if(flag)//已经找到在当前p1情况下最大的连续因数序列 break;elsep2--;}}}
0 0
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- 1096. Consecutive Factors (20)
- spark部署
- Qt学习备忘录(1)
- MVC5入门学习
- ionic tabs Android显示在顶部处理
- iOS 如何查看一段代码运行的时间
- 1096. Consecutive Factors (20)
- 字符串的排列、组合
- [前端]VR-异步时间扭曲(ATW)技术
- 排序的时间复杂度
- <libxml/tree.h> file not found
- HttpClient 4.0的使用工具类
- vector学习小节(二)
- 如何在Mac OS X上安装 Ruby运行环境
- Redis学习笔记(四) Spring与Jedis的集成