Vector angles (operator overloading)

【id:308】【12分】C. 向量角度(运算符重载)
时间限制
1s
内存限制
128MB
题目描述

定义一个二维vector(向量)类,vector有两个成员:x,y。(横坐标、纵坐标值),重载运算符“/”,实现两个向量之间的角度的计算,即如下图所示求出angle的值。并输出输入的所有向量中两个向量相差最大的角度为多少。




输入

第1行:向量数目

第2行开始,依次输入每个向量的x和y值(int类型)。


输出

所有向量中相差最大的角度


样例查看模式 
正常显示
查看格式
输入样例1 <-复制
2
1 1 
1 0
输出样例1
角度相差最大为45度。

formula for finding angle

friend int operator/(vector one, vector two) {

// cout << one.x << " " << one.y << endl;

// cout << two.x << " " << two.y << endl;

double mone = sqrt(one.x * one.x + one.y * one.y);

double mtwo = sqrt(two.x * two.x + two.y * two.y);

double fenzi = one.x * two.x + one.y * two.y;

double res = acos(fenzi / (mone * mtwo))*45.0 / atan(1.0);

// cout << res * 45.0 / atan(1.0) << endl;

return res;

}

#include <iostream>
#include <cstring>
#include <valarray>

using namespace std;


class vector {
public:
    int x;
    int y;

    vector() {}

    vector(int x, int y) : x(x), y(y) {}

    friend int operator/(vector one, vector two) {
        // cout << one.x << " " << one.y << endl;
        // cout << two.x << " " << two.y << endl;
        double mone = sqrt(one.x * one.x + one.y * one.y);
        double mtwo = sqrt(two.x * two.x + two.y * two.y);
        double fenzi = one.x * two.x + one.y * two.y;
        double res = acos(fenzi / (mone * mtwo))*45.0 / atan(1.0);
        // cout << res * 45.0 / atan(1.0) << endl;
        return res;
    }
};

int main() {
    int number;
    cin >> number;
    vector vs[number];

    for (int i = 0; i < number; ++i) {
        int x;
        int y;
        cin >> x >> y;
        vs[i] = *new vector(x, y);
    }
    int maxnumber = 0;
    for (int i = 0; i < number - 1; ++i) {
        for (int j = i + 1; j < number; ++j) {
            if (maxnumber < vs[i] / vs[j])
                maxnumber = vs[i] / vs[j];
        }
    }
    cout << "角度相差最大为" << maxnumber << "度。" << endl;
}

Guess you like

Origin blog.csdn.net/m0_62288512/article/details/131549959