1. 程式人生 > 實用技巧 >L1-006 連續因子 (20分)

L1-006 連續因子 (20分)

對於30,有兩串分解結果:1*5*6 2*3*5 最長的連續的因子是5*6 2*3,其中要列印的是題目要就較小的那個2*3。

#include<stdio.h>
#include<math.h>
int main()
{
    int n;
    scanf("%d",&n);
    int count=0;
    int m_count=0;
    int start=0;
    int i;
    
    for(i=2;i<=sqrt(n);i++){
        count=0;
        int j=i;
        
int t=n; while(t%j==0){ t=t/j; j++; count++; } if(count>m_count){ m_count=count; start=i; } } if(m_count){ printf("%d\n",m_count); for(i=0;i<m_count;i++){ printf("%d",start+i);
if(i!=m_count-1) printf("*"); } } /* 千萬不要忘記素數這種情況,如果輸入的值是素數,那麼它的因子就是 1 和它本身了, 但是題目要求1不能列印,所以我們最終只需輸出它本身就可以了 */ else printf("%d\n%d",1,n); return 0; }