解题思路:
用一个函数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 人评分
【绝对值排序】 (C++代码)浏览:672 |
C语言程序设计教程(第三版)课后习题8.5 (C语言代码)浏览:937 |
DNA (C语言描述,蓝桥杯)浏览:1555 |
a+b浏览:433 |
企业奖金发放 (C语言代码)浏览:2325 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:673 |
简单的a+b (C语言代码)浏览:466 |
C语言训练-求PI* (C语言代码)浏览:512 |
Manchester-字符串的输入输出处理浏览:12769 |
整除的尾数 (C语言代码)浏览:504 |