Problem D: C/C++經典程式訓練7---求某個範圍內的所有素數
阿新 • • 發佈:2018-11-17
Problem D: C/C++經典程式訓練7---求某個範圍內的所有素數
Time Limit: 1 Sec Memory Limit: 64 MB
Description
求小於n的所有素數,按照每行10個顯示出來。Input
輸入整數n(n<10000)。Output
每行10個依次輸出n以內的所有素數。Sample Input
100
Sample Output
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
HINT
#include<stdio.h> #include<math.h> int main() { int n,m; int i,j; int x=0; scanf("%d",&n); if(n<1000) { for(i=2;i<n;i++) { m=sqrt(i); for(j=2;j<=m;j++) if(i%j==0) break; if(j==m+1) { if(x!=0) printf(" "); printf("%d",i); x++; } if(x==0)continue; if(x%10==0) { printf("\n"); x=0; } } } return 0; }