L1-006 连续因子

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

630

输出样例:

3
5*6*7

贴代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
    int N,cnt=0,zhtai=1;
    int n=0; 
    char a[1010];
    char temp[1010];
    int sq; 
    scanf("%d",&N);
    sq=(int)sqrt(N)+1; 
    for(int i=sq;i>=2&&zhtai;--i){
        cnt=0;
        if(N%i==0){
            temp[cnt++]=i;
            int t=N/i;
            for(int j=i-1;j>=2;--j)
            {
                if(t%j==0){
                    temp[cnt++]=j;
                    t=t/j;
                    if(j==2) {
                        zhtai=0;
                    }
                }
                else{
                    temp[cnt]=0; break;
                }
            }
            if(cnt>=n){
            strcpy(a,temp);
            n=cnt; 
        }
    }
 }
    if(n==0)printf("%d\n%d",1,N);
    else{
        printf("%d\n",n);
        for(int j=n-1;j>=0;--j){
            printf("%d",a[j]);
            if(j!=0){
                printf("*");
            }
            else{
                printf("\n");
            }
        }
    } 

 return 0;
}

猜你喜欢

转载自blog.csdn.net/wanglin007/article/details/81416796