当前位置:网站首页>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;
}
边栏推荐
- Leetcode 90: subset II
- 复杂网络建模(三)
- Bayes' law
- 力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
- Linux server development, redis protocol and asynchronous mode
- Avatary的LiveDriver试用体验
- Who has docker to install MySQL locally?
- 【无标题】
- LeetCode简单题之字符串中最大的 3 位相同数字
- [quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
猜你喜欢
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
Main window in QT learning 27 application
JS复制图片到剪切板 读取剪切板
Leetcode simple question: find the K beauty value of a number
【踩坑系列】uniapp之h5 跨域的问题
追风赶月莫停留,平芜尽处是春山
Implementation of replacement function of shell script
LeetCode简单题之找到一个数字的 K 美丽值
Thinkcmf6.0安装教程
快解析内网穿透为文档加密行业保驾护航
随机推荐
C language queue
Excel import function of jeesite form page
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
电池、电机技术受到很大关注,反而电控技术却很少被提及?
微信小程序基本组件使用介绍
Linux server development, detailed explanation of redis related commands and their principles
It took "7" years to build the robot framework into a micro service
快解析内网穿透为文档加密行业保驾护航
ZCMU--1396: 队列问题(2)
Notes on PHP penetration test topics
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
【踩坑系列】uniapp之h5 跨域的问题
Binary tree and heap building in C language
青龙面板--花花阅读
The element with setfieldsvalue set is obtained as undefined with GetFieldValue
Qinglong panel -- Huahua reading
Content of string
C语言二叉树与建堆
Unityhub cracking & unity cracking
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO