1957: 百鸡问题
时间限制: 1 Sec 内存限制: 32 MB
献花: 66 解决: 30
[献花][花圈][TK题库]
题目描述
用小于等于n元去买100只鸡,大鸡5元/只,小鸡3元/只,还有1/3元每只的一种小鸡,分别记为x只,y只,z只。编程求解x,y,z所有可能解。
输入
测试数据有多组,输入n。
输出
对于每组输入,请输出x,y,z所有可行解,按照x,y,z依次增大的顺序输出。
样例输入
45
样例输出
x=0,y=0,z=100
x=0,y=1,z=99
x=0,y=2,z=98
x=0,y=3,z=97
x=0,y=4,z=96
x=1,y=0,z=99
x=1,y=1,z=98
x=1,y=2,z=97
x=2,y=0,z=98
暴力破解
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <ctime>
using namespace std;
int main()
{
#ifdef _DEBUG
//freopen("data.txt", "r+", stdin);
fstream cin("data.txt");
#endif // _DEBUG
int n;
while (cin >> n)
{
for (int x = 0; 5 * x <= n; ++x)
{
if (x > 100)break;
for (int y = 0; 3 * y + 5 * x <= n; ++y)
{
if (x + y > 100)break;
int tz = (n - 5 * x - 3 * y) * 3;
int maxz = tz + x + y > 100 ? 100 - x - y : tz;
if (x + y + maxz == 100)
cout << "x=" << x << ",y=" << y << ",z=" << maxz << endl;
}
}
}
#ifdef _DEBUG
cin.close();
#ifndef _CODEBLOCKS
system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG
return 0;
}
/**************************************************************
Problem: 1957
User: Sharwen
Language: C++
Result: 升仙
Time:373 ms
Memory:1704 kb
****************************************************************/