当前位置:网站首页>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;
}
边栏推荐
- Linux server development, SQL statements, indexes, views, stored procedures, triggers
- [UTCTF2020]file header
- [webrtc] M98 screen and window acquisition
- The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
- Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
- What are the positions of communication equipment manufacturers?
- Force buckle 144 Preorder traversal of binary tree
- Figure out the working principle of gpt3
- Niu Mei's mathematical problem --- combinatorial number
- [UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
猜你喜欢
Li Kou interview question 04.01 Path between nodes
[2022 actf] Web Topic recurrence
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
PHP exports millions of data
php导出百万数据
探索Cassandra的去中心化分布式架构
2022 tea master (intermediate) examination questions and mock examination
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
开源生态|打造活力开源社区,共建开源新生态!
Most elements
随机推荐
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
C language queue
Detailed explanation of Kalman filter for motion state estimation
[OBS] win capture requires winrt
Cnopendata American Golden Globe Award winning data
[2022 ciscn] replay of preliminary web topics
Qt学习26 布局管理综合实例
2022焊工(初级)判断题及在线模拟考试
Value sequence (subsequence contribution problem)
LeetCode 90:子集 II
pytest+allure+jenkins環境--填坑完畢
Numbers that appear only once
【webrtc】m98 screen和window采集
央视太暖心了,手把手教你写HR最喜欢的简历
[experience sharing] how to expand the cloud service icon for Visio
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
Explore Cassandra's decentralized distributed architecture
自定义类加载器加载网络Class