zoukankan      html  css  js  c++  java
  • NYOJ 141 Squares (数学)

    题目链接

    描述

    A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property. 

    So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates.

    • 输入
      The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.You can assume the number of test cases is less than 20
    • 输出
      For each test case, print on a line the number of squares one can form from the given stars.
    • 样例输入
      4
      1 0
      0 1
      1 1
      0 0
      9
      0 0
      1 0
      2 0
      0 2
      1 2
      2 2
      0 1
      1 1
      2 1
      4
      -2 5
      3 7
      0 0
      5 2
      0
    • 样例输出
      1
      6
      1

    分析:

    题目的意思其实很简单,就是对于给出的一系列二维坐标系中的点,其中的四个点构成一个正方形,这样不同的正方形一共有多少个。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int n;
    struct Node
    {
        int x;
        int y;
    } node[1009];
    
    bool cmp(Node a,Node b)///这些点如果横坐标相同的话,就按照纵坐标从小到大排序;否则就直接按照横坐标从小到大排序
    {
        if(a.x==b.x)
            return a.y<b.y;
        else
            return a.x<b.x;
    }
    
    bool Search(int x,int y)///采用二分查找法,查一下求出的点是否在已有点的序列中
    {
        int left=0;
        int right=n;
        int mid;
        while(left<=right)
        {
            mid=(left+right)/2;
            if(node[mid].x==x&&node[mid].y==y)
                return true;
            else if(node[mid].x==x&&node[mid].y<y||node[mid].x<x)///递归在右区间中找
                left=mid+1;
            else///递归在左区间中找
                right=mid-1;
        }
        return false;
    }
    int main()
    {
        while(~scanf("%d",&n)&&n)
        {
            int ans=0;
            for(int i=0; i<n; i++)
                scanf("%d%d",&node[i].x,&node[i].y);
            sort(node,node+n,cmp);
            int x1,y1,x2,y2;
            for(int i=0; i<n; i++)
                for(int j=i+1; j<n; j++)
                {
                    ///这样找的话相当于找的是这条线上方或左方的图形,不理解的话自己用坐标试试
                    x1=node[j].x-(node[j].y-node[i].y);
                    y1=node[j].y+(node[j].x-node[i].x);
                    if(!Search(x1,y1))  continue;
                    x2=node[i].x-(node[j].y-node[i].y);
                    y2=node[i].y+(node[j].x-node[i].x);
                    if(!Search(x2,y2)) continue;
                    ans++;
                }
            printf("%d
    ",ans/2);///每一个图形都可以由两条边找到,下面和右面的边
        }
    
        return 0;
    }
  • 相关阅读:
    Python TCP服务器
    Python TCP客户端
    链接测试工具xenu link sleuth的使用
    nodejs & nodemailer
    node socket.io web
    簡單的爬蟲 二 ಥ_ಥ 爬一爬 一個博客的每篇文件的標題
    簡單的爬蟲 一 ಥ_ಥ(就是一個頁面)
    兩端對齊
    appframework(jqmobi) 3.0 设置
    sass mapsource --->gulp
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6761626.html
Copyright © 2011-2022 走看看