首页 > 代码库 > PAT Basic Level 1013

PAT Basic Level 1013

 1 #include <stdio.h>
 2 #include <math.h>
 3 int main ()
 4 {
 5     int begin;
 6     int end;
 7     int cnt = 0;
 8     scanf("%d %d",&begin,&end);
 9     int i = 2; //i  作为素数 
10     int j;   //用来检验是否素数 
11     int flag3 = 0; //用来检验该是否是素数 
12     int flag = 0; //行显示5个控制变量 
13     int flag2 = 0; //控制换行后是否还要空格 
14     while(cnt <= end)
15     {
16         for(j = 2;j < sqrt(i);j++)      //不用SQRT方法会超时 
17         {
18             if(i % j == 0)
19             break;
20         }
21         if (j > sqrt(i))
22         {
23             cnt++;
24             flag3 = 1;
25         }
26         if (cnt >= begin&&cnt <= end&&flag3 == 1)
27         {
28             if(flag < 10) 
29             {
30                 if(flag2 == 1)
31                 {
32                     printf(" ");
33                     flag2 = 0;
34                 }
35                 printf("%d",i);
36                 
37                 if(flag != 9&& cnt != end)
38                 {
39                     printf(" ");
40                 }
41                 flag++;
42             }
43             else{
44                 printf("\n%d",i);
45                 flag = 1;
46                 flag2 = 1;
47             }
48             flag3 = 0;
49          } 
50         i++;
51      } 
52      return 0 ;
53     
54 }

 

PAT Basic Level 1013