关于本地long double出错的奇怪问题
查看原帖
关于本地long double出错的奇怪问题
490694
Compound_Interest楼主2024/3/6 16:49
#include<bits/stdc++.h>
using namespace std;
const int maxn=300;
const long double eps=1e-10;
long double a[maxn][maxn];
int n,m;
int main(){
	while(~scanf("%d%d",&n,&m)){
		for(int i=0;i<=m+1;i++)
			for(int j=0;j<=n+1;j++)
				a[i][j]=0;
		for(int i=1;i<=n;i++) scanf("%Lf",&a[m+1][i]);
		for(int i=1;i<=n;i++) a[m+1][i]=-a[m+1][i];
		for(int i=1;i<=m;i++){
			for(int j=1;j<=n;j++) scanf("%Lf",&a[i][j]);
			scanf("%Lf",&a[i][n+1]);
		}
		while(1){
			int chnb=-1;
			for(int i=1;i<=n;i++)
				if(a[m+1][i]<eps){
					chnb=i;
					break;
				}
			if(chnb==-1){
				break;
			}
			int chb=-1;
			long double mn=1e9;
			for(int i=1;i<=m;i++)
				if(a[i][n+1]/a[i][chnb]>eps&&mn>a[i][n+1]/a[i][chnb])
					mn=a[i][n+i]/a[i][chnb],chb=i;
			if(chb==-1){
				break;
			}
			a[chb][chnb]=1.0/a[chb][chnb];
			for(int j=1;j<=n+1;j++)
				if(j!=chnb)
					a[chb][j]*=a[chb][chnb];
			for(int i=1;i<=m+1;i++)
				for(int j=1;j<=n+1;j++)
					if(i!=chb&&j!=chnb)
						a[i][j]-=a[chb][j]*a[i][chnb];
			for(int i=1;i<=m+1;i++)
				if(i!=chb)
					a[i][chnb]=-a[i][chnb]*a[chb][chnb];
		}
		printf("Nasa can spend %d taka.\n",(int)ceil(a[m+1][n+1]*m));
	}
	return 0;
}
3 3
1 0.67 1.67
1 2 1 430
3 0 2 460
1 4 0 420

uva10498线性规划模板

本地输出1,在线IDE输出正确结果1354,但是本地改成double就可以输出和在线ide同样的正确结果。

2024/3/6 16:49
加载中...