P4035 [JSOI2008] Spherical Space Generator (Gaussian Elimination)

topic description

There is a spherical space generator that can generate a solid sphere in n-dimensional space. Now, you are trapped in this n-dimensional sphere, you only know the coordinates of n+1 points on the sphere, you need to determine the coordinates of the center of this n-dimensional sphere as quickly as possible, so as to destroy this spherical space to generate device.

input format

The first row is an integer (1≤N≤10). The next n+1 lines, each line has n real numbers, representing the n-dimensional coordinates of a point on the sphere. Each real number is accurate to 6 decimal places, and its absolute value does not exceed 20000.

output format

There is one and only one line, giving the n-dimensional coordinates of the center of the sphere (n real numbers) in sequence, and a space separates the two real numbers. Each real number is accurate to 3 decimal places. The data is guaranteed to have a solution. Your answer must be exactly the same as the standard output to be scored.

Parse:

Let the center of the sphere be the same distance from the center of the sphere to the points. Subtracting two adjacent equations shifts the unknown to the left.

There are n unknown items and n equations that can be solved to get the position of the center of the circle.

#include<bits/stdc++.h>
using namespace std;
inline double read() {
	char ss = getchar();
	int f = 1;
	double x = 0;
	while (ss < '0' || ss>'9') {
		if (ss == '-') {
			f = -1;
		}
		ss = getchar();
	}
	while (ss >= '0' && ss <= '9') {
		x = x * 10 + ss - '0';
		ss = getchar();
	}
	return x * f;
}

double a[20][20], c[20][20];  // c储存系数 b是每一个方程的值
int n;
double eps = 1e-7;

int main() {
	n = read();
	for (int i = 1; i <= n + 1; i++) {
		for (int j = 1; j <= n; j++) {
			scanf("%lf", &a[i][j]);
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			c[i][j] = 2 * (a[i][j] - a[i + 1][j]);
			c[i][n+1] += a[i][j] * a[i][j] - a[i + 1][j] * a[i + 1][j];// 累加 平方 
		}
	}
		//高斯消元
		for (int i = 1; i <= n; i++) {
			int max = i;
			for (int j = i+1; j <= n; j++) {
				if (fabs(c[j][i]) > fabs(c[max][i])) max = j; //选最大的避免选到0
			}

			//因为这个一定有解所以没有判断是否有解的步骤
			// 交换
			for (int j = 1; j <= n+1; j++) {
				swap(c[i][j], c[max][j]);
			}
			 // c[i][n+1]是等式的值

			//消元

			for (int j = n+1; j >= 1; j--) {
				c[i][j] = c[i][j] / c[i][i]; //对角线化1
			}
			

			for (int j = 1; j <= n; j++) {
				if (j != i) {
					double temp = c[j][i] / c[i][i]; // 乘以系第i列化为0
					for (int k = 1; k <= n+1; k++) {
						c[j][k] -= c[i][k] * temp;
					}
				}
			}
		}
		
		for (int i = 1; i <= n; i++) {
			printf("%0.3lf ",c[i][n+1]);
		}
	return 0;
}

Guess you like

Origin blog.csdn.net/zhi6fui/article/details/129476867