L1-006. 连续因子

L1-006. 连续因子

一个正整数N的因子中可能存在若干连续的数字。例如630可以分解为3*5*6*7,其中5、6、7就是3个连续的数字。给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。

输入格式:

输入在一行中给出一个正整数N(1<N<231)。

输出格式:

首先在第1行输出最长连续因子的个数;然后在第2行中按“因子1*因子2*……*因子k”的格式输出最小的连续因子序列,其中因子按递增顺序输出,1不算在内。

输入样例:
630
输出样例:
3
5*6*7

 1 #include<stdio.h>
 2 #include<math.h>
 3 int num=1,tmp,num1=0,tmp1=65580,temp2;;
 4 int x;
 5 int a[1000];
 6 int fun(int N,int i){
 7     int j;
 8     if(i>sqrt(N)){
 9         if(N%i==0 && x==i-1){
10             num++;
11             if(num1<num){
12                 num1=num;
13                 tmp1=tmp;
14             }
15         }
16 
17         return N;
18     }
19     for(j=i;j<=sqrt(N);j++){
20         if(N%j==0){
21             if(temp2){
22                 tmp=j;
23                 temp2=0;
24             }
25             N=N/j;
26             if(x==j-1){
27                 num++;
28     
29             }
30             
31             else{
32                 if(num1<num ){
33                 
34                     num1=num;
35                     tmp1=tmp;
36                 }
37                 tmp=j;
38                 num=1;
39             }
40             
41             x=j;
42 
43             return j*fun(N,j+1);
44         }
45     }
46     
47 }
48 
49 int fun1(int N){
50     int i;
51     for(i=2;i<=sqrt(N);i++){
52         if(N%i==0){
53             return 1;
54         }
55     }
56     return 0;
57     
58 }
59 int main(){
60     int N,i,j,k,temp;
61     scanf("%d",&N);
62     temp=fun1(N);
63     if(temp){
64     for(i=2;i<=sqrt(N);i++){
65         num=1;temp2=1;
66     
67         fun(N,i);
68 
69     }
70     printf("%d\n",num1);
71     for(i=0;i<num1-1;i++){
72         printf("%d*",tmp1);
73         tmp1++;
74     }
75     printf("%d\n",tmp1);
76     }
77     else
78         printf("1\n%d\n",N);
79     return 0;
80 }

 

posted @ 2017-04-25 10:28  Y.yang  阅读(464)  评论(0编辑  收藏  举报