NYOJ--24

原题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=24

分析:向两边分别找到第一个素数,再比较。

素数距离问题

 1 #include<stdio.h>
 2 #include<math.h>
 3 int p(int n)
 4 {
 5      int i,f=1;
 6      if(n==0)return 0;
 7      else if(n==1)return 0;
 8      else if(n==2)return 1;
 9     else
10     {
11          for(i=2;i<=sqrt(n);i++)
12          if(n%i==0)f=0;
13      }
14      return f;
15 }
16 int main()
17 {
18        int n,m,j,count1,k,count2,h;
19        scanf("%d",&n);
20        while(n--)
21        {
22             count1=count2=0;
23             scanf("%d",&m);
24             for(j=0;;j++)
25             {
26                   h=p(m-j);
27                   if(h==1)count1=j;
28                   if(h==1)break;
29             }
30             for(k=0;;k++)
31            {
32                   h=p(m+k);
33                   if(h==1)count2=k;
34                   if(h==1)break;
35             }
36             if(count1<=count2)printf("%d %d\n",m-count1,count1);
37             else printf("%d %d\n",m+count2,count2);
38       }
39       return 0;
40 }        

 

posted @ 2013-05-31 22:03  EtheGreat  阅读(125)  评论(0)    收藏  举报