当前位置:网站首页>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;
}
边栏推荐
- Who has docker to install MySQL locally?
- Linux server development, redis protocol and asynchronous mode
- 图解GPT3的工作原理
- [matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
- 大视频文件的缓冲播放原理以及实现
- [experience sharing] how to expand the cloud service icon for Visio
- Detailed explanation of Kalman filter for motion state estimation
- [webrtc] m98 Screen and Window Collection
- 追风赶月莫停留,平芜尽处是春山
- Pytest+allure+jenkins environment -- completion of pit filling
猜你喜欢
Content of string
QT learning 28 toolbar in the main window
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
[2022 actf] Web Topic recurrence
2022 tea master (intermediate) examination questions and mock examination
Qt学习27 应用程序中的主窗口
Niu Mei's mathematical problem --- combinatorial number
[mathematical notes] radian
即刻报名|飞桨黑客马拉松第三期等你挑战
padavan手动安装php
随机推荐
Linux server development, MySQL process control statement
Leetcode 40: combined sum II
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
C语言航班订票系统
2022 tea master (intermediate) examination questions and mock examination
2022制冷与空调设备运行操作复训题库及答案
[guess-ctf2019] fake compressed packets
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
Linux server development, MySQL stored procedures, functions and triggers
芯片 設計資料下載
[webrtc] M98 screen and window acquisition
MySQL multi column index (composite index) features and usage scenarios
微信小程序基本组件使用介绍
Cnopendata geographical distribution data of religious places in China
[Stanford Jiwang cs144 project] lab4: tcpconnection
QT learning 28 toolbar in the main window
SQL优化的魅力!从 30248s 到 0.001s
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
Explore dry goods! Apifox construction ideas
Linux server development, MySQL transaction principle analysis