解题思路:
用一个函数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语言程序设计教程(第三版)课后习题6.5 (C++代码)浏览:701 |
倒杨辉三角形 (C语言代码)浏览:3892 |
点我有惊喜!你懂得!浏览:3876 |
C语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:511 |
汽水瓶 (C语言代码)浏览:698 |
C语言训练-数字母 (C语言代码)浏览:582 |
C语言训练-计算t=1+1/2+1/3+...+1/n (C语言代码)浏览:859 |
C语言训练-排序问题<1> (C语言代码)浏览:599 |
求圆的面积 (C语言代码)浏览:1267 |
数组输出 (C语言代码)错误???浏览:559 |