当前位置:网站首页>OpenJudge NOI 2.1 1752:鸡兔同笼
OpenJudge NOI 2.1 1752:鸡兔同笼
2022-07-07 04:45:00 【君义_noip】
【题目链接】
【题目考点】
1. 枚举
【解题思路】
解法1:枚举
设有鸡x只,兔子y只,已知有a只脚,那么有
2 x + 4 y = a 2x+4y=a 2x+4y=a
鸡最少有0只,当所有的脚都是鸡脚时,鸡最多,有 a 2 \frac{a}{2} 2a只,所以 0 ≤ x ≤ a 2 0\le x \le \frac{a}{2} 0≤x≤2a
兔子最少有0只,最多有 ⌊ a 4 ⌋ \lfloor \frac{a}{4} \rfloor ⌊4a⌋只,所以 0 ≤ y ≤ ⌊ a 4 ⌋ 0\le y \le \lfloor \frac{a}{4} \rfloor 0≤y≤⌊4a⌋
总结为:
- 枚举对象:x, y
- 枚举范围: 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⌋
- 枚举条件: 2 x + 4 y = a 2x+4y=a 2x+4y=a
选择所有满足条件的x与y中,x+y的最大与最小值。
解法2:找规律
如果输入的脚的数量为奇数,那么没有满足要求的答案。
当有尽量多的鸡时,动物应该是最多的。当有尽量多的兔子时,动物应该是最少的。
鸡最多为 a 2 \frac{a}{2} 2a,那么动物最多为 a 2 \frac{a}{2} 2a
兔子最多为 ⌊ a 4 ⌋ \lfloor \frac{a}{4} \rfloor ⌊4a⌋,剩下 a % 4 a\%4 a%4只脚都是鸡,有 a % 4 2 \frac{a\%4}{2} 2a%4只,此时动物最少,有 ⌊ a 4 ⌋ + a % 4 2 \lfloor \frac{a}{4} \rfloor+\frac{a\%4}{2} ⌊4a⌋+2a%4只。
【题解代码】
解法1:枚举
#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)
{
//更新最大值与最小值
mx = max(mx, x+y);
mn = min(mn, x+y);
}
}
if(mx == 0 && mn == INF)//如果没有找到
cout << "0 0";
else
cout << mn << ' ' << mx;
return 0;
}
解法2:找规律
#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;
}
边栏推荐
- C语言航班订票系统
- 解决问题:Unable to connect to Redis
- LeetCode 40:组合总和 II
- misc ez_ usb
- Figure out the working principle of gpt3
- Leetcode 90: subset II
- QT learning 26 integrated example of layout management
- Rust versus go (which is my preferred language?)
- Redis technology leak detection and filling (II) - expired deletion strategy
- Shell 脚本的替换功能实现
猜你喜欢

These five fishing artifacts are too hot! Programmer: I know, delete it quickly!

JS quick start (I)

2022 tea master (intermediate) examination questions and mock examination

Es FAQ summary

Leetcode 90: subset II
![[guess-ctf2019] fake compressed packets](/img/a2/7da2a789eb49fa0df256ab565d5f0e.png)
[guess-ctf2019] fake compressed packets

【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)

Cnopendata American Golden Globe Award winning data

2022制冷与空调设备运行操作复训题库及答案

Wechat applet data binding multiple data
随机推荐
[SUCTF 2019]Game
【VHDL 并行语句执行】
即刻报名|飞桨黑客马拉松第三期等你挑战
buuctf misc USB
json 数据展平pd.json_normalize
Pytorch parameter initialization
Codeforces Global Round 19
Leetcode 90: subset II
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
Linux server development, MySQL stored procedures, functions and triggers
Detailed explanation of Kalman filter for motion state estimation
芯片资料 网站 易特创芯
Codeforce c.strange test and acwing
Introduction to basic components of wechat applet
Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
C语言二叉树与建堆
Pytorch(六) —— 模型调优tricks
贝叶斯定律
大视频文件的缓冲播放原理以及实现
php导出百万数据