zoukankan      html  css  js  c++  java
  • JustOj 1994: P1001

    题目描述
            给定一个长度为N(0< n< =10000)的序列,保证每一个序列中的数字a[i]是小于maxlongint的非负整数  ,编程要求求出整个序列中第k大的数字减去第k小的数字的值m,并判断m是否为质数。(0< k< =n)
    输入
    输入格式: 第一行为2个数n,k(含义如上题) 第二行为n个数,表示这个序列
    输出
    输出格式: 如果m为质数则 第一行为'YES'(没有引号) 第二行为这个数m 否则  第一行为'NO' 第二行为这个数m
    样例输入
    5 2
    1 2 3 4 5
    
    样例输出
    YES
    2
    
    提示

    对于第K大的详细解释:
    如果一个序列为1  2  2  2  2  3
    第1大  为3
    第2大  为2
    第3大  为2
    第4大  为2
    第5大  为1
    第K小与上例相反

    另外需要注意的是
    最小的质数是2,如果小于2的话,请直接输出NO

    题解:注意最小的质数是2,如果小于2的话,请直接输出NO
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 using namespace std;
    13 #define lowbit(x) (x&(-x))
    14 #define max(x,y) (x>y?x:y)
    15 #define min(x,y) (x<y?x:y)
    16 #define MAX 100000000000000000
    17 #define MOD 1000000007
    18 #define pi acos(-1.0)
    19 #define ei exp(1)
    20 #define PI 3.141592653589793238462
    21 #define INF 0x3f3f3f3f3f
    22 #define mem(a) (memset(a,0,sizeof(a)))
    23 typedef long long ll;
    24 ll gcd(ll a,ll b){
    25     return b?gcd(b,a%b):a;
    26 }
    27 bool cmp(int x,int y)
    28 {
    29     return x>y;
    30 }
    31 const int N=10005;
    32 const int mod=1e9+7;
    33 ll a[N];
    34 int prim(ll n)
    35 {
    36     int i,flag=1;
    37     for(i=2;i*i<=n;i++){
    38         if(n%i==0){
    39             flag=0;
    40             break;
    41         }
    42     }
    43     if(flag) return 1;
    44     else return 0;
    45 }
    46 int main()
    47 {
    48     std::ios::sync_with_stdio(false);
    49     ll n,m,k;
    50     cin>>n>>k;
    51     for(int i=0;i<n;i++)
    52         cin>>a[i];
    53     sort(a,a+n);
    54     ll tmin=a[k-1];
    55     sort(a,a+n,cmp);
    56     ll tmax=a[k-1];
    57     m=(tmax-tmin);
    58     if(m>=2&&prim(m)) cout<<"YES"<<endl;
    59     else cout<<"NO"<<endl;
    60     cout<<m<<endl;
    61     return 0;
    62 }
  • 相关阅读:
    C#中属性和字段的区别
    利用原生态的(System.Web.Extensions)JavaScriptSerializer将mvc 前台提交到controller序列化复杂对象
    点击图片后放大居中显示
    Python logging模块
    MySQL免安装版 配置
    python 字符串 常用方法
    python 格式化输出
    欢迎使用CSDN-markdown编辑器
    Program received signal SIGSEGV, Segmentation fault.
    error MSB6006: “CL.exe”已退出,代码为 -1073741502。
  • 原文地址:https://www.cnblogs.com/wydxry/p/7268039.html
Copyright © 2011-2022 走看看