【我与一道水题的抗争之路】 哈理工2323 Emirp(反素数)

题目:

http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2323

 

1,打表的姿势不对。。。

2,不会用sprintf和atoi函数实现倒转一个数字

 

题解:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 _Bool check[1000005] = { 0 };
 5 int emirp[1005];
 6 void make_prime(void)
 7 {
 8     int i, j;
 9     for (i = 2; i <= 500000; i++)
10     {
11         for (j = 2 * i; j <= 1000000; j += i)
12         {
13             check[j] = 1;
14         }
15     }
16 }
17 void make_Emirp(void)
18 {
19     int i, j;
20     char string[1000];
21     int count = 0;
22     int p, q,temp;
23     for (i = 13; count <= 1000; i++)
24     {
25         if (!check[i])
26         {
27             sprintf(string, "%d", i);
28             for (p = 0, q = strlen(string) - 1; p < q; p++, q--)
29             {
30                 temp = string[p];
31                 string[p] = string[q];
32                 string[q] = temp;
33             }
34             int result = atoi(string);
35             if (!check[result] && result != i) emirp[count++] = i;
36         }
37     }
38 }
39 int main(void)
40 {
41     int t;
42     int k;
43     scanf("%d", &t);
44     make_prime();
45     make_Emirp();
46     while (t--)
47     {
48         scanf("%d", &k);
49         printf("%d\n", emirp[k - 1]);
50     }
51     return 0;
52 }

 

posted @ 2016-12-10 19:43  codinRay  阅读(278)  评论(0编辑  收藏  举报