zoukankan      html  css  js  c++  java
  • YTU 2611: A代码完善--向量的运算

    2611: A代码完善--向量的运算

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 256  解决: 168

    题目描述

    注:本题只需要提交填写部分的代码,请按照C++方式提交。

    对于二维空间中的向量,实现向量的减法和取负运算。如向量A(x1,y1)和B(x2,y2), 
    则 A-B 定义为 (x1-x2,y1-y2) , -A 定义为 (-x1,-y1) 。

    #include <stdio.h>
    #include <iostream>
    using namespace std;
    class Vector
    {
    private :
        int x,y;
    public:
        void setValue(int x,int y)
        {
            this->x=x;
            this->y=y;
        }
        void output()
        {
            cout<<"x="<<x<<",y="<<y<<endl;
        }
        Vector operator-();
        friend Vector operator- (Vector &v1,Vector &v2);
    };

    int  main()
    {
        Vector A,B,C;
        int x,y;
        cin>>x>>y;
        A.setValue(x,y);
        cin>>x>>y;
        B.setValue(x,y);
        C = A - B;
        C.output();
        C = -C;
        C.output();
        return 0;
    }
    /*
     请在该部分补充缺少的代码

    */

    输入

    两个向量

    输出

    向量减法和向量取负运算后的结果

    样例输入

    10 20 15 25

    样例输出

    x=-5,y=-5
    x=5,y=5

    迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

    #include <stdio.h>
    #include <iostream>
    using namespace std;
    class Vector
    {
    private :
        int x,y;
    public:
        void setValue(int x,int y)
        {
            this->x=x;
            this->y=y;
        }
        void output()
        {
            cout<<"x="<<x<<",y="<<y<<endl;
        }
        Vector operator-();
        friend Vector operator- (Vector &v1,Vector &v2);
    };
    int  main()
    {
        Vector A,B,C;
        int x,y;
        cin>>x>>y;
        A.setValue(x,y);
        cin>>x>>y;
        B.setValue(x,y);
        C = A - B;
        C.output();
        C = -C;
        C.output();
        return 0;
    }
    Vector Vector::operator-()
    {
        Vector a;
        x=-x;
        y=-y;
        a.x=x,a.y=y;
        return a;
    }
    Vector operator- (Vector &v1,Vector &v2)
    {
        Vector a;
        a.x=v1.x-v2.x;
        a.y=v1.y-v2.y;
        return a;
    }

  • 相关阅读:
    [dfs+水] hdu 4462 Scaring the Birds
    [Leetcode]-containsNearbyDuplicate
    測试流程的规范性与重要性
    柯里化函数之Javascript
    android AChartEngine源代码
    Android高斯模糊
    linux64位系统中g++4.4.7下使用wcsncpy函数有造成段错误的BUG(其它g++版本号未知)
    便利贴女孩_百度百科
    法爱格官方旗舰店
    法爱格2014 春夏新款欧美纯色修身高腰无袖吊带V领 拼接性感 连衣裙 黑色 M【图片 价格 品牌 报价】-京东
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989528.html
Copyright © 2011-2022 走看看