求助wa#3#4#5#6或#3#10#5#6
查看原帖
求助wa#3#4#5#6或#3#10#5#6
347589
Zelotz楼主2022/1/9 23:51

约旦代码:

#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;
#define double long double
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);
	bool FLG = 0;
	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 && a[i][n + 1]) return puts("-1"), 0;
		else FLG = 1;
	}
	if (!FLG) return puts("0"), 0;
	int p = 0;
	for (int i = 1; i <= n; ++i) {
//		++p;
		p = i;
		while ((a[p][i] > -1e-9 && a[p][i] < 1e-9) && p <= n) ++p;
//		puts("");
//		cout << p << endl;
		if (p == n + 1) {
//			cout << "!!!" << i << endl;
			p = i;
//			return 0;
			continue; 
		}
		for (int j = 1; j <= n; ++j) swap(a[i][j], a[p][j]);
		for (int j = 1; j <= n + 1; ++j) if (j ^ i) a[i][j] /= a[i][i];
		a[i][i] = 1;
		for (int j = 1; j <= n; ++j) {
			if (i == j || (a[j][i] > -1e-9 && a[j][i] < 1e-9)) continue;
			for (int k = 1; k <= n + 1; ++k) if (k ^ i) a[j][k] /= a[j][i];
			a[j][i] = 1;
			for (int k = 1; k <= n + 1; ++k) a[j][k] -= a[i][k];
			if (!a[j][j]) continue;
			for (int k = 1; k <= n + 1; ++k) if (k ^ j) a[j][k] /= a[j][j];
			a[j][j] = 1;
		}
	}
	int cnt = 0, cnt2 = 0;
	for (int i = 1; i <= n; ++i) {
		if (a[i][i]) ++cnt;
		if (a[i][i] || a[i][n + 1]) ++cnt2;
	}
	if (cnt < cnt2) return puts("-1"), 0;
	if ((cnt ^ n) && cnt == cnt2) return puts("0"), 0;
	for (int i = 1; i <= n; ++i) printf("x%d=%.2Lf\n", i, a[i][n + 1] >= 0 ? fabs(a[i][n + 1]) : a[i][n + 1]);
	return 0;
}

wa 3456

普通代码:

#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;
#define double long double
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) printf("%.2Lf   ", a[i][j]);
		cout << endl;
	}
	cout << endl;
}
int main() {
	read(n);
	bool FLG = 0;
	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 && a[i][n + 1]) return puts("-1"), 0;
		else FLG = 1;
	}
	if (!FLG) return puts("0"), 0;
	int p = 0;
	for (int i = 1; i <= n; ++i) {
//		++p;
		p = i;
		while ((a[p][i] > -1e-9 && a[p][i] < 1e-9) && p <= n) ++p;
//		puts("");
//		cout << p << endl;
		if (p == n + 1) {
//			cout << "!!!" << i << endl;
			p = i;
//			return 0;
			continue; 
		}
		for (int j = 1; j <= n; ++j) swap(a[i][j], a[p][j]);
//		for (int j = 1; j <= n + 1; ++j) if (j ^ i) a[i][j] /= a[i][i];
//		a[i][i] = 1;
		for (int j = i + 1; j <= n; ++j) {
			if (i == j || (a[j][i] > -1e-9 && a[j][i] < 1e-9)) continue;
			for (int k = 1; k <= n + 1; ++k) if (k ^ i) a[j][k] -= a[i][k] * (a[j][i] / a[i][i]);
			a[j][i] = 0;
//			print(); 
//			for (int k = 1; k <= n + 1; ++k) if (k ^ i) a[j][k] /= a[j][i];
//			a[j][i] = 1;
//			for (int k = 1; k <= n + 1; ++k) a[j][k] -= a[i][k];
//			if (!a[j][j]) continue;
//			for (int k = 1; k <= n + 1; ++k) if (k ^ j) a[j][k] /= a[j][j];
//			a[j][j] = 1;
		}
	}
//	print();
	for (int i = n; i >= 1; --i) {
		for (int j = i - 1; j >= 1; --j) {
			if (a[j][i] > -1e-9 && a[j][i] < 1e-9) continue;
			for (int k = 1; k <= n + 1; ++k) if (k ^ i) a[j][k] -= a[i][k] * (a[j][i] / a[i][i]);
			a[j][i] = 0;
		}
	}
	for (int i = 1; i <= n; ++i) {
		if (a[i][i]) a[i][n + 1] /= a[i][i], a[i][i] = 1;
	}
//	print(); 
	int cnt = 0, cnt2 = 0;
	for (int i = 1; i <= n; ++i) {
		if (a[i][i]) ++cnt;
		if (a[i][i] || a[i][n + 1]) ++cnt2;
	}
	if (cnt < cnt2) return puts("-1"), 0;
	if ((cnt ^ n) && cnt == cnt2) return puts("0"), 0;
	for (int i = 1; i <= n; ++i) printf("x%d=%.2Lf\n", i, a[i][n + 1] >= 0 ? fabs(a[i][n + 1]) : a[i][n + 1]);
	return 0;
}

wa 3 5 6 10

交50遍了 我快去世了

2022/1/9 23:51
加载中...