Time limit per test: 2.0 seconds
Memory limit: 256 megabytes
在平面上,确定一个点的位置通常有下面两种表示方法:
当极坐标系中的极点 O 与直角坐标系中的原点 O 重合,极轴 OX 与直角坐标系中的 X 轴的正半轴重合,并且两种坐标系的单位长度相同,那么平面内任意一点 P 的直角坐标与极坐标可以互相转换。
例如:
点 p 直角坐标为:(1,1),则对应的极坐标为:(1.4142,π/4)。
点 p 直角坐标为:(-1,1),则对应的极坐标为:(1.4142,3π/4)。
点 p 直角坐标为:(-1,-1),则对应的极坐标为:(1.4142,5π/4)。
点 p 直角坐标为:(1,-1),则对应的极坐标为:(1.4142,7π/4)。
点 p 直角坐标为:(0,1),则对应的极坐标为:(1,π/2)。
点 p 直角坐标为:(1,0),则对应的极坐标为:(1,0)。
给出 N 个点的直角坐标 (x,y),请计算出这些点对应的极坐标,将这 N 个点按照极角 θ 从小到大排序,如果两个点的极角相同,则将它们按照极径 ρ 由大到小排序。
注意:ρ≥0,极角 0≤θ<2π
Input
第 1 行:整数 T (1≤T≤10) 为问题数。
对于每个问题,按如下格式输入:
第 1 行:输入一个正整数 N(1≤N≤1000),表示点的个数;
接下来 N 行,每行输入两个浮点数 x,y,表示点的直角坐标,两个数之间由一个空格分隔。
Output
对于每个问题,输出一行问题的编号(0 开始编号,格式:case #0: 等)。
接下来 N 行输出排序后的点的极坐标(ρ,θ),每行输出一个点的极坐标,小数点后保留 4 位,极角采用弧度表示。具体格式见 SampleOutput。
注意:ρ≥0,极角 0≤θ<2π
Examples
3 5 1.0 1.0 2.0 2.0 -1.0 1.0 0 1.0 1.0 0 1 0 -1.0 6 1.0 1.0 0 1.0 1.0 0 -1.0 1.0 -1.0 -1.0 1.0 -1.0
case #0: (1.0000,0.0000) (2.8284,0.7854) (1.4142,0.7854) (1.0000,1.5708) (1.4142,2.3562) case #1: (1.0000,4.7124) case #2: (1.0000,0.0000) (1.4142,0.7854) (1.0000,1.5708) (1.4142,2.3562) (1.4142,3.9270) (1.4142,5.4978) 提示: (1)、圆周率π的取值:请使用头文件math.h中的宏定义:M_PI #define M_PI 3.14159265358979323846 (2)、点的坐标(x,y),极径ρ以及极角θ的数据类型用double型。 (3)、C语言提供的计算反正切值的标准库函数:atan2() 头文件:#include 原型为:double atan2(double y,double x); 参数x,y为点的直角坐标(x,y),函数返回值以弧度表示,范围为 (-π,π]。 注意:θ =atan2(y, x),求出的θ取值范围是(-π,π]。 当(x>0, y>0), 0<θ<π/2 当(x<0, y>0), π/2<θ<π 当(x<0, y<0), -π <θ<-π/2 当(x>0, y<0), -π/2<θ <0 当点(x, y)在象限的边界也就是坐标轴上时: 当y=0,x≥0,θ=0 当y=0,x<0,θ=π 当y>0,x=0,θ=+π/2 当y<0, x= 0,θ=-π/2 atan2( )函数的值域为(-π,π],可以通过对负数结果加2π的方法,将函数的结果映射到(0,2π]范围内。
思路很简单,在处理数据的时候需要注意角度的取值范围,当小于0时加上2*PI
这道题中我用了pair类型来储存坐标,这个类型比起二维数组更加直观,以下是转载的pair类型的用法
Pair类型概述
pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下:
pair<int, string> a;
表示a中有两个类型,第一个元素是int型的,第二个元素是string类型的,如果创建pair的时候没有对其进行初始化,则调用默认构造函数对其初始化。
pair<string, string> a("James", "Joy");
也可以像上面一样在定义的时候直接对其初始化。
由于pair类型的使用比较繁琐,因为如果要定义多个形同的pair类型的时候,可以时候typedef简化声明:
typedef pair<string, string> author;
author pro("May", "Lily");
author joye("James", "Joyce");
Pair对象的操作
-
对于pair类,由于它只有两个元素,分别名为first和second,因此直接使用普通的点操作符即可访问其成员
pair<string, string> a("Lily", "Poly");
string name;
name = pair.second;
-
生成新的pair对象
可以使用make_pair对已存在的两个数据构造一个新的pair类型:
int a = 8;
string m = "James";
pair<int, string> newone;
newone = make_pair(a, m);
然后是贴代码
#include <bits/stdc++.h>
#define PI 3.1415926535
using namespace std;
typedef pair <double, double> P;
P mat[1000];
int cmp(const void *a, const void *b)
{
P n1, n2;
n1 = *(P *)a;
n2 = *(P *)b;
if (n1.second < n2.second)
return-1;
else if (n1.second == n2.second)
{
return n2.first - n1.first;
}
else
{
return 1;
}
}
void solve() /* Define function solve() to process one case of the problem */
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> mat[i].first >> mat[i].second;
double n1, n2;
n1 = mat[i].first;
n2 = mat[i].second;
mat[i].first = sqrt(n1*n1 + n2*n2);
if (atan2(n2, n1) < 0)
mat[i].second = atan2(n2, n1) + 2 * PI;
else
mat[i].second = atan2(n2, n1);
}
qsort(mat, n, sizeof(mat[0]), cmp);
for (int i = 0; i < n; i++)
{
printf("(%.4f,%.4f)\n", mat[i].first, mat[i].second);
}
}
void init() /* Define function init() to do global intialization if needed */
{
}
/******************************************************************************/
/* */
/* DON'T MODIFY main() function anyway! */
/* */
/******************************************************************************/
#include <iostream>
void solve(); /* write function solve() to process one case of the problem */
int main()
{
int i, t; init();
std::cin >> t;
for (i = 0; i<t; i++)
{
std::cout << "case #" << i << ":" << std::endl;
solve();
}
return 0;
}