80分求助帖子
查看原帖
80分求助帖子
1118776
2311819_Wym楼主2023/12/5 20:22

有一个测试点没过,是第二个,检查了好久,难道是有特殊情况还是进度问题???

#include <iostream>
#include <iomanip>
using namespace std;
struct a
{
	double have;
	double worth;
}coin[1000];

double store(double value, double weight, int &t);
int n, t;//t设置为全局变量后并没有发生改变,在store中并没有改变。

int main()
{
	double networth = 0;
	cin >> n>>t;
	for (int i = 0; i < n; i++)
	{
		cin >> coin[i].have >> coin[i].worth;
	}
	int b[1000],place;
	double max = 0;
	for (int i = 0; i < n; i++)
	{
		max = 0;
		for (int j = 0; j < n; j++)
		{
			if (max<coin[j].worth/ coin[j].have && b[j] != 1)
			{
				max = coin[j].worth/ coin[j].have;//呀呀呀呀,粗心了,把下标写错了。。!!!!!!!1
				place = j;
			}
		}
		b[place] = 1;
		networth += store(coin[place].worth, coin[place].have, t);
	}
	cout << fixed<<setprecision(2)<<networth;
}

double store(double value,double weight, int &t)//使用取地址符号,这里使用了 引用的知识,实现t的全局改变
{
	if (t == 0) return 0;
	int remain = t - weight;
	
	if (remain < 0) 
	{	
		int mt = t;
		t = 0;//逻辑关系错误,不要写在return后面!
		return value * (mt / weight);
		
	}
	else
	{
		t -= weight;
		return value;
	}
}
2023/12/5 20:22
加载中...