【HDOJ】1405 The Last Practice
简单题,感觉求素数的算法太慢了。以前在SPOJ上交的效率要高很多。注意输出格式,容易PE。
1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 65536 5 6 int array[MAXNUM]; 7 int prime[7000]; 8 int nums[MAXNUM]; 9 10 int main() { 11 int case_n = 0; 12 int n; 13 int i, j, k; 14 15 memset(array, 1, sizeof(array)); 16 k = 0; 17 for (i=2; i<MAXNUM; ++i) { 18 if (array[i]) { 19 prime[k++] = i; 20 for (j=2; j*i<MAXNUM; ++j) 21 array[i*j] = 0; 22 } 23 } 24 25 while (scanf("%d", &n) != EOF && n>0) { 26 if (case_n) 27 printf("\n"); 28 ++case_n; 29 printf("Case %d.\n", case_n); 30 memset(nums, 0, sizeof(nums)); 31 32 for (i=0; i<k && prime[i]<=n; ++i) { 33 while (n%prime[i] == 0) { 34 nums[prime[i]]++; 35 n /= prime[i]; 36 } 37 } 38 39 for (i=2; i<MAXNUM; ++i) 40 if (nums[i]) 41 printf("%d %d ", i, nums[i]); 42 printf("\n"); 43 } 44 45 46 return 0; 47 }