C.射击成绩 | |||||
| |||||
Description | |||||
利用面向对象程序设计完成要求。 设定靶心坐标为(0,0),建立平面直角坐标系。 现有n(n<10)名射击运动员每人射击一次,获得每次射击坐标后,按照成绩由高到低的顺序输出各射击点坐标,射击点与靶心的距离(假设各射击坐标与靶心的距离不相同)。 | |||||
Input | |||||
第1行输入射击运动员的人数n。 以下n行每行输入一名射击运动员的射击点的横纵坐标(X,Y)(均为整型),数据之间由空格分隔。 | |||||
Output | |||||
输出数据n行。每行按照射击成绩由高到低的顺序输出各射击点坐标,及其与靶心的距离,数据之间由空格分隔。 | |||||
Sample Input | |||||
3 3 4 2 2 1 0 | |||||
Sample Output | |||||
1 0 1.00 2 2 2.83 3 4 5.00 | |||||
Hint | |||||
要求:输出射击点与靶心的距离时保留小数位2位。 可参考: #include<iomanip> cout<<setiosflags(ios::fixed)<<setprecision(2)<<dist<<endl;//假定射击点与靶心的距离为dist |
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class Point
{
public:
int x, y;
double d;
void setPoint(int x1,int y1)
{
x = x1, y = y1;
d = sqrt(x * x + y * y);
}
};
int main()
{
int n;
cin >> n;
Point* p = new Point[n];
for (int i = 0; i < n; i++)
{
int x, y;
cin >> x >> y;
p[i].setPoint(x, y);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (p[j].d > p[j + 1].d)
{
Point temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
cout << p[i].x << ' ' << p[i].y<<' ';
cout << setiosflags(ios::fixed) << setprecision(2) << p[i].d << endl;
}
return 0;
}