• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 3365 New Ground (计算几何)

题意:给定点A[0~n-1]和B[0],B[1],A[0]、A[1]映射到B[0]、B[1],求出其余点的映射B[2]~B[n-1]。

析:运用复数类,关键是用模板复数类,一直编译不过,我明明能编译过,交上就不过,只能写一个复数了。。。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const int maxn = 10000 + 5;
template<class T>
class Complex
{
public:
      Complex( ){real=0;imag=0;}
      Complex(T r,T i){real=r;imag=i;}
      Complex complex_add(Complex &c2);
      Complex complex_minus(Complex &c2);
      Complex complex_multiply(Complex &c2);
      Complex complex_divide(Complex &c2);
      T real1();
      T imag1();

public:
    friend istream &operator >>(istream &is, Complex<T> &p){
        cin >> p.real >> p.imag;
        return is;
    }

private:
      T real;
      T imag;
};

template<class T>
Complex<T> Complex<T>::complex_add(Complex<T> &c2)
{
      Complex<T> c;
      c.real=real+c2.real;
      c.imag=imag+c2.imag;
      return c;
}

template <class T>
Complex<T> Complex<T>::complex_minus(Complex <T> &c2)
{
      Complex <T> c;
      c.real=real-c2.real;
      c.imag=imag-c2.imag;
      return c;
}

template <class T>
Complex<T> Complex<T>::complex_multiply(Complex <T> &c2)
{
      Complex <T> c;
      c.real=real*c2.real-imag*c2.imag;
      c.imag=imag*c2.real+real*c2.imag;
      return c;
}

template <class T>
Complex<T> Complex<T>::complex_divide(Complex <T> &c2)
{
      Complex <T> c;
      T d=c2.real*c2.real+c2.imag*c2.imag;
      c.real=(real*c2.real+imag*c2.imag)/d;
      c.imag=(imag*c2.real-real*c2.imag)/d;
      return c;
}

template <class T>
T Complex<T>::real1(){
    return real;
}

template <class T>
T Complex<T>::imag1(){
    return imag;
}



Complex<double> a[maxn], b[2], ans;

int main(){
    int T, n;  cin >> T;
    for(int kase = 1; kase <= T; ++kase){
        scanf("%d", &n);
        double x, y;
        for(int i = 0; i < n; ++i){
            cin >> a[i];

        }
        for(int i = 0; i < 2; ++i){
            cin >> b[i];
        }

        Complex<double> tmp = (b[1].complex_minus(b[0]));
        Complex<double> tmp1 = (a[1].complex_minus(a[0]));
        tmp = tmp.complex_divide(tmp1);
        printf("Case %d:\n", kase);
        for(int i = 0; i < n; ++i){
            ans = (a[i].complex_minus(a[0]));
            ans = ans.complex_multiply(tmp);
            ans = ans.complex_add(b[0]);
            printf("%.2lf %.2lf\n", ans.real1(), ans.imag1());
        }
    }
    return 0;
}

 

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <complex>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const int maxn = 10000 + 5;
complex<double> a[maxn], b[2], ans;

int main(){
    int T, n;  cin >> T;
    for(int kase = 1; kase <= T; ++kase){
        scanf("%d", &n);
        for(int i = 0; i < n; ++i)
            scanf("%lf %lf", &a[i].real(), &a[i].imag());
        scanf("%lf %lf %lf %lf", &b[0].real(), &b[0].imag(), &b[1].real(), &b[1].imag());
        complex<double> tmp = (b[1]-b[0])/(a[1]-a[0]);
        printf("Case %d:\n", kase);
        for(int i = 0; i < n; ++i){
            ans = (a[i]-a[0]) * tmp + b[0];
            printf("%.2lf %.2lf\n", ans.real(), ans.imag());
        }
    }
    return 0;
}

 

posted on 2016-07-28 00:42  dwtfukgv  阅读(274)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3