当前位置:网站首页>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;
}
边栏推荐
- Uniapp mobile terminal forced update function
- Linux server development, MySQL transaction principle analysis
- Notes on PHP penetration test topics
- Introduction to basic components of wechat applet
- Merging binary trees by recursion
- 力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
- The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
- Téléchargement des données de conception des puces
- Quickly use Jacobo code coverage statistics
- DNS server configuration
猜你喜欢

运放电路的反馈电阻上并联一个电容是什么作用

Hisense TV starts the developer mode

Linux server development, redis protocol and asynchronous mode

2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation

Introduction to basic components of wechat applet

【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)

LeetCode简单题之判断一个数的数字计数是否等于数位的值

Avatary's livedriver trial experience

Thinkcmf6.0安装教程

Call pytorch API to complete linear regression
随机推荐
Blob object introduction
Content of string
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
UnityHub破解&Unity破解
Network learning (I) -- basic model learning
It took "7" years to build the robot framework into a micro service
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
Jmeter 的使用
Example of file segmentation
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
Complex network modeling (II)
offer收割机:两个长字符串数字相加求和(经典面试算法题)
Avatary的LiveDriver试用体验
Myabtis_ Plus
Relevant data of current limiting
C语言队列
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
Leetcode simple question: find the K beauty value of a number
[VHDL parallel statement execution]
LeetCode简单题之判断一个数的数字计数是否等于数位的值