zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 1(C. Nearest vectors)

      题目链接:http://codeforces.com/problemset/problem/598/C

      题意是给你一个数n,下面n行,每行给你横坐标x和纵坐标y(x != 0 && y != 0)。求当两个点与原点形成的角度最小时,是哪两个点。

      先介绍atan2这个函数,atan2(double y,double x) 其中y代表已知点的Y坐标 同理x ,返回值是此点与远点连线与x轴正方向的夹角,这样它就可以处理四个象限的任意情况了,它的值域相应的也就是-180~180了。

      我觉得最好还是用atan2这个函数,求出与x正轴的角度,然后从小到大排序,处理相邻的两个点的角度(后面的减前面的),最后处理第一个和最后一个点角度,最好乘上个1000,精度问题...

      代码如下:

      

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 const int MAXN = 1e5 + 10;
     8 double PI = acos(-1) , M = 10000;
     9 struct data {
    10     double x , y , ang;
    11     int id;
    12 }a[MAXN];
    13 
    14 bool cmp(data a , data b) {
    15     return a.ang < b.ang;
    16 }
    17 //min()函数只能用于整数
    18 double MIN(double x , double y) {  
    19     if(x > y) 
    20         return y;
    21     return x;
    22 }
    23 
    24 int main() 
    25 {
    26     int n;
    27     ios::sync_with_stdio(false);
    28     while(cin >> n) {
    29         for(int i = 0 ; i < n ; i++) {
    30             cin >> a[i].x >> a[i].y;
    31             a[i].id = i + 1;
    32             a[i].ang = atan2(a[i].y , a[i].x) * 180.0 * M / PI;
    33         }   
    34         sort(a , a + n , cmp);
    35         int id1 , id2;  
    36         double Min = 100000000.0 , temp;
    37         for(int i = 1 ; i < n ; i++) {
    38             temp = a[i].ang - a[i - 1].ang;
    39             temp = MIN(temp , 360 * M - temp);
    40             if(Min > temp) {
    41                 id1 = a[i - 1].id;
    42                 id2 = a[i].id;
    43                 Min = temp;
    44             }
    45         }
    46         temp = a[n - 1].ang - a[0].ang;
    47         temp = MIN(temp , 360 * M - temp);
    48         if(temp < Min) {
    49             cout << a[0].id << " " << a[n - 1].id << endl;
    50         } 
    51         else {
    52             cout << id1 << " " << id2 << endl;
    53         }
    54     }
    55 }
  • 相关阅读:
    (转)【web前端培训之前后端的配合(中)】继续昨日的故事
    ural(Timus) 1136. Parliament
    scau Josephus Problem
    ACMICPC Live Archive 6204 Poker End Games
    uva 10391 Compound Words
    ACMICPC Live Archive 3222 Joke with Turtles
    uva 10132 File Fragmentation
    uva 270 Lining Up
    【转】各种字符串哈希函数比较
    uva 10905 Children's Game
  • 原文地址:https://www.cnblogs.com/Recoder/p/4965809.html
Copyright © 2011-2022 走看看