1096.Consecutive Factors(20)

2394 ワード

テーマリンク:http://www.patest.cn/contests/pat-a-practise/1096
タイトル:
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 consecutitive numbebers.Novevevevevevevevevevevevevevevevevevevevevevevevevevevevevetititititytytytytytytytytytytypopopopopopopopopopopopopopopoints.inininininininininininininininininininininininininininininininininininininquence 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 maximnumber of consecutive factors.The n in the second line,print the smalest sequence of the consecutive factors in the format「factor[1]*factor[2]the factor」
Sample Input:
630
Sample Output:
3
5*6*7
分析:
題目はすべての連乗の因数の中で一番長い列を探します。ここはすべての因数に遍歴するのではなく、積に与えられた数の中で探すのです。例えば12、一番長いのは2、3*4で、3*3*4ではなくて、2*3*4です。
参考資料:
http://www.cnblogs.com/asinlzm/p/4460935.html
ACコード:
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<map>
#include<cmath>
using namespace std;
int main(){
 //freopen("F://Temp/input.txt", "r", stdin);
 int N = 0;
 cin >> N;
 if (N == 2 || N == 3 || N == 5)  { printf("1
%d", N); return 0; } int num = (int)sqrt(N), len = 0, factors[1000000] = { 0 }; for (int i = 2; i <= num; i++) if (N%i == 0) factors[len] = i, len++; factors[len] = N, len++; int maxlen = 0, maxfactor = N; int templen = 0, tempfactor = 0; for (int i = 0; i<len; i++){// num = N, tempfactor = factors[i], templen = 0; while (num%tempfactor == 0) templen++, num /= tempfactor, tempfactor++; if (templen>maxlen) maxlen = templen, maxfactor = factors[i];<span style="font-family: ; font-size: 14px; widows: auto;">// </span> } printf("%d
", maxlen); for (int i = 0; i<maxlen; i++) { if (i) printf("*%d", maxfactor);// else printf("%d", maxfactor); maxfactor++; } printf("
"); return 0; }
スクリーンショット:
——アプリ陳小旭