沐里纷纷


私信TA

用户名:Epoch

访问量:62798

签 名:

我不会算法

等  级
排  名 37
经  验 12816
参赛次数 1
文章发表 172
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

不会算法

解题思路:
用一个函数bool satisfy(int useIndex)可行则削减原材料数量,不可行直接返回false

注意事项:

参考代码:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <stdio.h>

using namespace std;

int materialCount[4];
int useCount[5][4] = { {2,1,0,2},{1,1,1,1},{0,0,2,1},{0,3,0,0},{1,0,0,1} };
int productCount[5];

bool satisfy(int useIndex)
{
	if (useCount[useIndex][0] <= materialCount[0] && useCount[useIndex][1] <= materialCount[1] && useCount[useIndex][2] <= materialCount[2] && useCount[useIndex][3] <= materialCount[3])
	{
		materialCount[0] -= useCount[useIndex][0];
		materialCount[1] -= useCount[useIndex][1];
		materialCount[2] -= useCount[useIndex][2];
		materialCount[3] -= useCount[useIndex][3];
		return true;
	}
	else
		return false;
}

void solve()
{
	for (int i = 0;i < 5;i++)
		while (satisfy(i))
			productCount[i] += 1;
	return;
}

int main(int argc, char** argv)
{
	for (int i = 0; i < 4; i++)
		cin >> materialCount[i];
	solve();
	for (int i = 0; i < 5; i++)
		cout << productCount[i] << endl;
	return 0;
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区