当前位置:网站首页>Openjudge noi 2.1 1752: chicken and rabbit in the same cage
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
2022-07-07 08:10:00 【Jun Yi_ noip】
【 Topic link 】
OpenJudge NOI 2.1 1752: Chicken and rabbit in the same cage
【 Topic test site 】
1. enumeration
【 Their thinking 】
solution 1: enumeration
With chicken x only , The rabbit y only , It is known that there is a One foot , So there are
2 x + 4 y = a 2x+4y=a 2x+4y=a
Chicken has at least 0 only , When all feet are chicken feet , Most chickens , Yes a 2 \frac{a}{2} 2a only , therefore 0 ≤ x ≤ a 2 0\le x \le \frac{a}{2} 0≤x≤2a
Rabbits have at least 0 only , At most ⌊ a 4 ⌋ \lfloor \frac{a}{4} \rfloor ⌊4a⌋ only , therefore 0 ≤ y ≤ ⌊ a 4 ⌋ 0\le y \le \lfloor \frac{a}{4} \rfloor 0≤y≤⌊4a⌋
Summed up in :
- enumerable object :x, y
- Enumeration range : 0 ≤ x ≤ a 2 0\le x \le \frac{a}{2} 0≤x≤2a, 0 ≤ y ≤ ⌊ a 4 ⌋ 0\le y \le \lfloor \frac{a}{4} \rfloor 0≤y≤⌊4a⌋
- Enumeration condition : 2 x + 4 y = a 2x+4y=a 2x+4y=a
Select all that meet the conditions x And y in ,x+y Maximum and minimum values of .
solution 2: Looking for a regular
If the number of input feet is odd , Then there is no answer that meets the requirements .
When there are as many chickens as possible , Animals should be the most . When there are as many rabbits as possible , Animals should be the least .
Chicken up to a 2 \frac{a}{2} 2a, Then the animals are at most a 2 \frac{a}{2} 2a
Rabbits are at most ⌊ a 4 ⌋ \lfloor \frac{a}{4} \rfloor ⌊4a⌋, be left over a % 4 a\%4 a%4 Every foot is a chicken , Yes a % 4 2 \frac{a\%4}{2} 2a%4 only , At this time, the animals are at least , Yes ⌊ a 4 ⌋ + a % 4 2 \lfloor \frac{a}{4} \rfloor+\frac{a\%4}{2} ⌊4a⌋+2a%4 only .
【 Solution code 】
solution 1: enumeration
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int main()
{
int a, mx = 0, mn = INF;
cin >> a;
for(int x = 0; x <= a/2; ++x)
for(int y = 0; y <= a/4; ++y)
{
if(2*x+4*y == a)
{
// Update the maximum and minimum values
mx = max(mx, x+y);
mn = min(mn, x+y);
}
}
if(mx == 0 && mn == INF)// If not found
cout << "0 0";
else
cout << mn << ' ' << mx;
return 0;
}
solution 2: Looking for a regular
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cin >> a;
if(a % 2 == 1)
cout << "0 0";
else
cout << a/4+a%4/2 << ' ' << a/2;
return 0;
}
边栏推荐
- Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
- Find the mode in the binary search tree (use medium order traversal as an ordered array)
- Qinglong panel -- finishing usable scripts
- 太真实了,原来自己一直没有富裕起来是有原因的
- Niu Mei's mathematical problem --- combinatorial number
- Leetcode 90: subset II
- Leetcode medium question my schedule I
- Make LIVELINK's initial pose consistent with that of the mobile capture actor
- Real time monitoring of dog walking and rope pulling AI recognition helps smart city
- [matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
猜你喜欢
Excel import function of jeesite form page
Leetcode 90: subset II
调用 pytorch API完成线性回归
jeeSite 表单页面的Excel 导入功能
青龙面板-今日头条
Niu Mei's mathematical problem --- combinatorial number
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
Ansible
Make LIVELINK's initial pose consistent with that of the mobile capture actor
JS复制图片到剪切板 读取剪切板
随机推荐
Blob object introduction
Recursive method constructs binary tree from middle order and post order traversal sequence
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
电池、电机技术受到很大关注,反而电控技术却很少被提及?
uniapp 移动端强制更新功能
buureservewp(2)
Merging binary trees by recursion
Leetcode 40: combined sum II
Niu Mei's mathematical problem --- combinatorial number
青龙面板--花花阅读
DNS server configuration
芯片资料 网站 易特创芯
game攻防世界逆向
QT learning 26 integrated example of layout management
漏洞复现-Fastjson 反序列化
Topic not received? Try this
Yugu p1020 missile interception (binary search)
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
基于Pytorch 框架手动完成线性回归
Complete linear regression manually based on pytoch framework