这是数据水吗
查看原帖
这是数据水吗
347589
Zelotz楼主2022/2/6 21:58
#include <bits/stdc++.h>
using namespace std;
#define srand srand(time(NULL))
#define random(x) rand() % (x)
#define il inline
#define ptc putchar
#define reg register
#define mp make_pair
typedef __int128 LL;
typedef long long ll;
typedef pair<int, int> PII;
namespace cyyh {
	template <typename T>
	il void read(T &x) {
		x = 0; T f = 1; char ch;
		while (!isdigit(ch = getchar())) f -= (ch == '-') << 1;
		while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch & 15), ch = getchar(); x *= f;
	}
	template <typename T>
	il void write(T x) {
		if (x < 0) ptc('-'), x = -x;
		if (x > 9) write(x / 10);
		ptc(x % 10 + '0');
	}
	template <typename T>
	il T lowbit(const T &x) {
		return x & -x;
	}
}
using namespace cyyh; 
const int N = 105;
int n;
double a[N][N];
void print() {
	cout << endl;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n + 1; ++j) cout << a[i][j] << ' ';
		cout << endl;
	}
	cout << endl;
}
int main() {
	read(n);
	for (int i = 1; i <= n; ++i) {
		bool flg = 0;
		for (int j = 1; j <= n + 1; ++j) {
			cin >> a[i][j];
			if (a[i][j] && j <= n) flg = 1;
		}
		if (!flg) return puts("0"), 0;
	}
	int p = 0;
	for (int i = 1; i <= n; ++i) {
        for (p = i; p <= n && !a[p][i]; ++p) ;
		if (p == n + 1) return puts("No Solution"), 0;
		for (int j = 1; j <= n + 1; ++j) swap(a[i][j], a[p][j]); // 这里,j<=n或j<=n+1都是ac的
		for (int j = 1; j <= n; ++j) {
			if (i == j || !a[j][i]) continue;
			double t = a[j][i] / a[i][i];
			for (int k = 1; k <= n + 1; ++k) a[j][k] -= a[i][k] * t;
		}
	}
	for (int i = 1; i <= n; ++i) printf("%.2f\n", a[i][n + 1] / a[i][i]);
	return 0;
}

交换两行时,我没有交换两行最后一个元素也可以ac

2022/2/6 21:58
加载中...