zoukankan      html  css  js  c++  java
  • 2016年杭电计算机研究生复试的笔试题目:2道编程题(2016):1.判断n是否是素数;2.二维平面内距离最近的两个点的距离;

    // N1 判断素数
    #include<stdio.h>
    #include<math.h>
    int main()
    {
    int n;
    int i;
    while (~scanf("%d",&n))
    {
    for ( i = 2; i <= sqrt((double)n); i++)
    {
    if (n%i==0)
    {
    break;
    }
    }
    //一直没有找到因子
    if (i>sqrt((double)n))
    {
    printf("yes");
    }
    else
    {
    printf("no");
    }
    }
    return 0;
    }

    //N2:暴力计算二维平面中最近的两个点
    //2.在一个二维平面内有n个点,每个点坐标为(x,y),求最近的两个点的距离。(暴力求解)
    /*输入:
    5
    1 2
    100 200
    1000 2000
    1000 1
    1 3
    */
    //暴力求解思路:将每个点都存储在结构体数组中,计算出任意两个点之间的距离放在距离数组中,再遍历距离数组求出最小的距离。
    #include<stdio.h>
    #include<math.h>
    #define N 1000
    #define M 1000000
    struct Point
    {
    double x;
    double y;
    };
    Point p[N];
    double dis[M];
    // Calculate the distance of two points.
    double distance(const struct Point *a, const struct Point *b)
    {
    return sqrt((a->x - b->x)*(a->x - b->x) + (a->y - b->y)*(a->y - b->y));
    }
    int main()
    {
    int n;
    int i;
    int j;
    int k;
    double min;
    while (~scanf("%d",&n))
    {
    k = 0;
    for (i = 0; i < n; i++)
    {
    scanf("%lf%lf", &p[i].x, &p[i].y);
    }
    for ( i = 0; i < n; j++)
    {
    for ( j = i+1; j < n-i-1; j++)
    {
    dis[k++] = distance(&p[i],&p[j]);
    }
    }
    min = 0;
    for ( i = 1; i < k; i++)
    {
    if (min>dis[i])
    {
    min = dis[i];
    }
    }
    printf("%.2lf ", min);
    }
    return 0;
    }

    一生有所追!
  • 相关阅读:
    SPOJ
    SPOJ LCS Longest Common Substring(后缀自动机)题解
    HihoCoder1445 后缀自动机二·重复旋律5(后缀自动机 子串种数)
    eclipse 常用设置
    读取Request body方法
    pom.xml实例
    powerdesign连接Oracle&Mysql
    Json常用操作
    Spring MVC POM示例
    FreeMaker常用表达式
  • 原文地址:https://www.cnblogs.com/BlueBlue-Sky/p/8503051.html
Copyright © 2011-2022 走看看