Special_Tony @ 2024-03-03 14:58:37
rt,50pts,每个点都错在误判成了无解,包括楼上鸽子的hack我也误判了:
# include <bits/stdc++.h>
# define ffor(i,name) \
for (auto i = name.begin (); i != name.end (); ++ i)
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const double eps = 1e-3;
int n, sum, m;
double a[105][105], x[105], mul;
int main () {
// ios::sync_with_stdio (0);
//
// cin.tie (0);
//
// cout.tie (0);
cin >> n;
m = n + 1;
for (int i = 1; i < m; ++ i)
for (int j = 1; j <= m; ++ j)
cin >> a[i][j];
for (int i = 1; i < n; ++ i)
for (int j = i + 1; j < m; ++ j) {
mul = a[j][i] / a[i][i];
for (int k = i + 1; k <= m; ++ k)
a[j][k] -= mul * a[i][k];
}
for (int i = n; i; -- i) {
for (int j = i + 1; j < m; ++ j)
a[i][m] -= a[i][j] * x[j];
if (fabs (a[i][i]) <= eps) {
if (fabs (a[i][m]) <= eps)
putchar ('0');
else
puts ("-1");
return 0;
}
x[i] = a[i][m] / a[i][i];
}
for (int i = 1; i <= n; ++ i)
printf ("x%d=%.2lf\n",i, x[i]);
return 0;
}
by __zhuruirong__ @ 2024-03-03 19:54:04
@sz_mane 无解不是输出 No Solution
吗?
by Special_Tony @ 2024-03-03 19:56:20
@zhuruirong ?建议再读下题
by __zhuruirong__ @ 2024-03-03 19:58:59
@sz_mane 看错题了
by __zhuruirong__ @ 2024-03-03 19:59:51
参考一下吧
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 110;
const double eps = 1e-10;
int n;
double a[N][N], b[N];
void work() {
int l = 1;
for(int i = 1; i <= n; i++) {
for(int j = l + 1; j <= n; j++)
if(abs(a[j][i]) > abs(a[l][i])) {
for(int k = 1; k <= n; k++)
swap(a[j][k], a[l][k]);
swap(b[j], b[l]);
}
if(abs(a[l][i]) < eps)
continue;
for(int j = 1; j <= n; j++)
if(j != l and abs(a[j][i]) >= eps) {
double num = a[j][i] / a[l][i];
for(int k = 1; k <= n; k++)
a[j][k] -= a[l][k] * num;
b[j] -= b[l] * num;
}
l++;
}
for(int i = l; i <= n; i++)
if(abs(b[i]) >= eps) {
puts("-1");
return;
}
if(l <= n)
puts("0");
else
for(int i = 1; i <= n; i++) {
double ans = b[i] / a[i][i];
printf("x%d=%.2lf\n", i, (abs(ans) <= eps ? 0.0 : ans));
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++)
cin >> a[i][j];
cin >> b[i];
}
work();
return 0;
}
by Special_Tony @ 2024-03-03 20:03:12
@zhuruirong 改了一下,但还是过不了鸽子的数据
# include <bits/stdc++.h>
# define ffor(i,name) \
for (auto i = name.begin (); i != name.end (); ++ i)
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const double eps = 1e-3;
int n, sum, m;
double a[55][55], x[55], mul;
int main () {
// ios::sync_with_stdio (0);
//
// cin.tie (0);
//
// cout.tie (0);
cin >> n;
m = n + 1;
for (int i = 1; i < m; ++ i)
for (int j = 1; j <= m; ++ j)
cin >> a[i][j];
for (int i = 1; i < n; ++ i)
for (int j = i + 1; j < m; ++ j)
if (fabs (a[i][i]) > eps) {
mul = a[j][i] / a[i][i];
for (int k = i + 1; k <= m; ++ k)
a[j][k] -= mul * a[i][k];
}
for (int i = n; i; -- i) {
for (int j = i + 1; j < m; ++ j)
a[i][m] -= a[i][j] * x[j];
if (fabs (a[i][i]) <= eps) {
if (fabs (a[i][m]) <= eps)
putchar ('0');
else
puts ("-1");
return 0;
}
x[i] = a[i][m] / a[i][i];
}
for (int i = 1; i < m; ++ i)
printf ("x%d=%.2lf\n", i, x[i]);
return 0;
}
/*
3
1 1 3 4
2 2 3 1
2 1 1 1
*/
by zichen3004 @ 2024-03-03 20:20:50
为什么你的高斯消元这么短。。
by Special_Tony @ 2024-03-10 10:29:27
@zichen3004 所以咋改