求助
查看原帖
求助
551088
FincheuwYggdrasil楼主2022/4/15 19:19

题目:

题目描述

妈妈派小明去买米,小明来到米店,发现有各种各样的米,已知n种米的所剩数量(斤)和单价(元/斤),小明共带了s元钱,问他一共最多能买多少米?

输入格式

第一行两个整数n和s,表示米的种数和小明所带s元钱

接下来n行,每行两个整数,分别表示每种米所剩的可买数量和单价

输出格式

一个数,表示小明最多能买多少斤米

样例输入

2 10

3 4

3 2

样例输出

4

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int how,money,less[1000],price[1000],min = 10000,BianHao = 0,LessList[1000],ans = 0;
	bool tr[1000];
	cin >> how >> money;
	for(int i = 0;i < how;i++)
		tr[i] = true;
	for(int i = 0;i < how;i++)
		cin >> less[i] >> price[i];
	for(int i = 0;i < how;i++)
	{
		for(int j = 0;j < how;j++)
		{
			if(price[j] < min && tr[j] == true)
			{
				tr[BianHao] = true;
				min = price[j];
				BianHao = j;
				tr[j] = false;
			}
		} 
		LessList[i] = BianHao;
	}
	for(int i = 0;i < how;i++)
	{
		if(price[LessList[i]] > money)
			break;
		else
		{
			if(money / price[LessList[i]] >= less[LessList[i]])
			{
				money -= price[LessList[i]] * less[LessList[i]];
				ans += less[LessList[i]];
			}
			else
				ans += money / price[LessList[i]];
		}
	}
	cout << ans;
 	return 0;
}


2022/4/15 19:19
加载中...