2009-02-09 14:01:26來源不明
ACM160 Q160: Factors and Factorials
採用方式:
1.建出質數表。
2.去尋找質因數,用陣列當作標記,並找出最大的質因數。
3.重要事項:如果剛好15個質因數,會多換一行,必須注意。
/***********************************************************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- main()
- {
- int math[25]={0};
- int a,b,c,n,set=1;
- math[0]=2;
- for(a=3;a<100;a++)
- {
- int flag=0;
- for(b=2;b<=(int)sqrt(a);b++)
- if(a%b==0) {flag=1;break;}
- if(flag==0)
- {
- math[set]=a;
- set++;
- }
- }
- while(scanf("%d",&n)==1&&n!=0)
- {
- int temp[25]={0},max=0,maxset;
- printf("%3d! =",n);
- for(a=2;a<=n;a++)
- {
- int temp1=a;
- for(b=0;b<set;b++)
- {
- while(temp1%math[b]==0)
- {
- temp[b]++;
- temp1=temp1/math[b];
- if(math[b]>max)
- {max=math[b];maxset=b;}
- if(temp1==1) break;
- }
- }
- }
- int time=0,temp1=maxset+1;
- for(a=0;a<=maxset;a++)
- {
- time++;
- printf("%3d",temp[a]);
- if(time==15&&temp1>15)
- {
- time=0;
- temp1=temp1-15;
- printf("\n ");
- }
- }
- printf("\n");
- }
- return 0;
- }