# Demo entry 6783117

1111

Submitted by anonymous on Jan 30, 2019 at 05:40
Language: C++. Code size: 1.0 kB.

```#include <iostream>
#include <cmath>
using namespace std;

struct point
{
double x;
double y;
double z;
double weight; //权重
double distance; //距离插值点的距离
};

const int r = 1; //反距离的幂值（0.5到3均可）

int n = 0; //点的个数
point p[50]; //存放离散点
point q; //插值点

void Distance() //计算未知点到所有点的距离
{
for(int i = 0; i < n; ++i)
p[i].distance = sqrt((q.x-p[i].x)*(q.x-p[i].x) + (q.y-p[i].y) *(q.y-p[i].y));
}

void Weight() //计算权重
{
double f = 0;
int i = 0;
for(i = 0; i < n; ++i)
f += pow(1.0 / p[i].distance, r);
for(i = 0; i < n; ++i)
p[i].weight = pow(1.0 / p[i].distance, r) / f;
}

void Getval() //得到插值点的权重
{
q.weight = 0;
for(int i = 0; i < n; ++i)
q.z += p[i].weight * p[i].z;
}

int main()
{
cout << "请输入已知点的个数" << endl;
cin >> n;
cout << "请输入已知点的坐标" << endl;
for(int i = 0; i < n; ++i)
cin >> p[i].x >> p[i].y >> p[i].z;
cout << "输入目标点的坐标" << endl;
cin >> q.x >> q.y;
Distance();
Weight();
Getval();
cout << q.z << endl;
return 0;
}
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.