• nyoj 24 素数距离问题


         

    素数距离问题

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:2
     
    描述
    现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度。如果左右有等距离长度素数,则输出左侧的值及相应距离。
    如果输入的整数本身就是素数,则输出该素数本身,距离输出0
     
    输入
    第一行给出测试数据组数N(0<N<=10000)
    接下来的N行每行有一个整数M(0<M<1000000),
    输出
    每行输出两个整数 A B.
    其中A表示离相应测试数据最近的素数,B表示其间的距离。
    样例输入
    3
    6
    8
    10
    样例输出
    5 1
    7 1
    11 1
    来源
    经典题目
    上传者
    hzyqazasdf
    方法二的打表方法很搓  但是不想改了
    那是很久很久以前写的
    方法一 直接模拟
     1  
     2 #include <iostream>
     3 #include <math.h>
     4 using namespace std;
     5 
     6 #define M 1100000
     7 int a[148933*3];
     8 bool notprim[M];
     9 int ab(int c)
    10 {
    11     if (c<0)
    12         return -c;
    13     else return c;
    14 }
    15 
    16 int main()
    17 {
    18     int j,i;
    19     int k=0;
    20     for (i=2;i<M;i++)
    21     {
    22         if (!notprim[i])
    23         {
    24             a[k++]=i;
    25         }
    26         else continue;
    27       for (j=i*2;j<M;j+=i)
    28       {
    29         notprim[j]=true; 
    30       }       
    31     }
    32     int n,m;
    33     cin>>n;
    34     while (n--)
    35     {
    36         cin>>m;
    37         int b,low;
    38         if (m==1)
    39         {
    40             cout<<"2"<<" "<<"1"<<endl;
    41         }
    42         else
    43         {
    44         for (i=0;a[i]<=m;i++);
    45             b=ab(a[i]-m);
    46             low=ab(a[i-1]-m);
    47           if (b>=low)
    48           {
    49               cout<<a[i-1]<<" "<<low<<endl;
    50           }    
    51           else cout<<a[i]<<" "<<b<<endl;
    52           
    53         }
    54     }
    55     return 0;
    56 }        
    方法二 素数打表
     1  
     2 #include <iostream>
     3 #include <math.h>
     4 using namespace std;
     5 
     6 int juge(int m)
     7 {
     8     int i;
     9     int b=int(sqrt(m)+1);
    10     for (i=2;i<b;i++)
    11     {
    12         if (m%i==0)
    13         {
    14             break;
    15         }
    16     }
    17     if (b==i)
    18     {
    19         return 1;
    20     }
    21     else return 0;
    22 }
    23 
    24 int ab(int c)
    25 {
    26     if (c<0)
    27         return -c;
    28     else return c;
    29 }
    30 
    31 int main()
    32 {
    33     int n,m;
    34     cin>>n;
    35     while (n--)
    36     {
    37         cin>>m;
    38         int t=0;
    39         if (m==1)
    40         {
    41             cout<<"2"<<" "<<"1"<<endl;
    42         }
    43         else        
    44             if(juge(m))
    45                 cout<<m<<" "<<t<<endl;
    46             else 
    47             {    
    48                 int a=m+1,b=m-1;
    49                 while (1)
    50                 {
    51                     if (juge(a))
    52                         break;
    53                     a++;
    54                 }
    55                 while (1)
    56                 {
    57                     if (juge(b))
    58                         break;
    59                     b--;
    60                 }
    61                 if (ab(a-m)>=ab(b-m))
    62                 {
    63                     cout<<b<<" "<<ab(b-m)<<endl;
    64                 }
    65                 else cout<<a<<" "<<ab(a-m)<<endl;
    66             }
    67     }
    68     return 0;
    69 }        


     

  • 相关阅读:
    博主推荐-工作中常用到一些不错的网址整理
    使用ansible部署CDH 5.15.1大数据集群
    ElasticSearch的API介绍
    HTML&CSS基础-CSS Hcak
    运维开发笔记整理-创建django用户
    运维开发笔记整理-数据库同步
    运维开发笔记整理-QueryDict对象
    运维开发笔记整理-template的使用
    运维开发笔记整理-JsonResponse对象
    运维开发笔记整理-Request对象与Response对象
  • 原文地址:https://www.cnblogs.com/wujianwei/p/2636970.html
Copyright © 2020-2023  润新知