24h購物| | PChome| 登入
2009-02-09 14:01:26| 人氣580| 回應0 | 上一篇 | 下一篇

ACM160 Q160: Factors and Factorials

推薦 0 收藏 0 轉貼0 訂閱站台

採用方式
1.建出質數表。
2.去尋找質因數,用陣列當作標記,並找出最大的質因數。
3.重要事項:如果剛好15個質因數,會多換一行,必須注意。

/***********************************************************/

  1. #include<stdio.h>   
  2. #include<stdlib.h>   
  3. #include<math.h>   
  4. main()   
  5. {   
  6.  int math[25]={0};   
  7.  int a,b,c,n,set=1;   
  8.  math[0]=2;   
  9.  for(a=3;a<100;a++)   
  10.   {   
  11.    int flag=0;   
  12.    for(b=2;b<=(int)sqrt(a);b++)   
  13.     if(a%b==0) {flag=1;break;}   
  14.    if(flag==0)   
  15.     {   
  16.      math[set]=a;   
  17.      set++;   
  18.     }    
  19.   }   
  20.  while(scanf("%d",&n)==1&&n!=0)   
  21.   {   
  22.    int temp[25]={0},max=0,maxset;   
  23.    printf("%3d! =",n);   
  24.    for(a=2;a<=n;a++)   
  25.     {   
  26.      int temp1=a;   
  27.      for(b=0;b<set;b++)   
  28.       {   
  29.        while(temp1%math[b]==0)   
  30.         {   
  31.          temp[b]++;   
  32.          temp1=temp1/math[b];   
  33.          if(math[b]>max)   
  34.           {max=math[b];maxset=b;}   
  35.          if(temp1==1) break;    
  36.         }   
  37.       }   
  38.     }   
  39.     int time=0,temp1=maxset+1;   
  40.     for(a=0;a<=maxset;a++)   
  41.      {   
  42.      time++;   
  43.      printf("%3d",temp[a]);   
  44.      if(time==15&&temp1>15)   
  45.       {   
  46.        time=0;   
  47.        temp1=temp1-15;   
  48.        printf("\n      ");   
  49.       }   
  50.      }   
  51.      printf("\n");    
  52.   }    
  53.  return 0;   
  54. } 

台長: 來源不明
人氣(580) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 數位資訊(科技、網路、通訊、家電) | 個人分類: ACM |
此分類下一篇:ACM 369 Q369: Combinations
此分類上一篇:ACM 11063 11063 B2-Sequence

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文