当前位置:网站首页>2018年江苏省信息与未来程序设计小能手比赛试题--(新)鸡兔同笼
2018年江苏省信息与未来程序设计小能手比赛试题--(新)鸡兔同笼
2022-07-16 04:11:00 【ceshyong】
题目描述
有三种动物分别被关在笼子里,它们分别是鸡,兔子和三脚猫。
每只鸡有一个头,两只脚;每只三脚猫有一个头,三只脚;每只兔子有一个头,四只脚。现在这道笼子里一共有X个头,Y只脚,但这个消息不足以推导出鸡,兔和三脚猫的准确数量。这时候就需要编程来帮忙了——请你求出笼子里分别至少,至多有多少只兔子。
输入
你需要在一个测试数据中处理多个鸡兔同笼问题。输入第一行T表示问题的数量。
接下来T行,每行两个用空格分隔的正整数X,Y表示头和脚的数量。
输出
对于每个问题,输出一行两个空格分隔的整数,表示笼子里至少和至多有几只兔子。输入数据保证每个问题至少有一种鸡,三脚猫和兔子的合法组合。
数据范围
对于50%的数据,1<=x<=10^6。
对于100%的数据,1<=X,Y<=10^9,1<=T<=20。
题目思路
和鸡羊同栏(链接在评论区)一样,这道题目也是一道鸡兔同笼类型的题目。但是,这里多了一个三脚猫。那该怎么办呢?
我们可以分最多和最少两种情况来写。既然要兔子最少,那么三脚猫就要最多。那么如果三脚猫脚的总只数大于等于头的三倍,那么就可以直接输零。否则输出总脚数减去头数的三倍。
如果要兔子最多的话,如果我们可以先设每个都是鸡,然后脚数比头的两倍大的数量除二就是最多的数量(如果脚数比头的两倍大的数量不能被而整除,那么就增加一只三脚猫。
最后前面在输入T,再套个循环就可以了。
题目主程序如下:
void cx(int t)
{
int x,y;
for(int i=1;i<=n;i++)
{
cin>>x>>y;
if(y<=x*3) cout<<0<<' ';
else cout<<y-x*3<<' ';
if(y<=x*2) cout<<0<<endl;
else cout<<(y-x*2)/2<<endl;
}
}题目标程
题目标程如下:
#include<bits/stdc++.h>
using namespace std;
int t,x,y;
int main()
{
cin>>t;
for(int i=1;i<=t;i++)
{
cin>>x>>y;
if(y<=x*3) cout<<0<<' ';
else cout<<y-x*3<<' ';
if(y<=x*2) cout<<0<<endl;
else cout<<(y-x*2)/2<<endl;
}
return 0;
}这道题目就这么多。
边栏推荐
- Clickhouse (04) how to build a Clickhouse cluster
- 递归求解二叉树的遍历(常考基础例题)
- Example analysis of go language selector
- Micro, m3o micro service series (II)
- MySQL triggers and stored procedures
- [TinyML]APQ:Joint Search for Network Architecture, Pruning and Quantization Policy
- 6、 JMeter timer
- 2.18 haas506 2.0 development tutorial - communication between Alibaba cloud M2M devices - rule engine /topic message routing (only versions above 2.2 are supported)
- 函数的重载
- micro、M3O微服务系列(二)
猜你喜欢

Software testing zero basic testing - Basic Concepts

Sword finger offer 48 The longest substring without repeated characters

Sword finger offer 28 Symmetric binary tree

Bluetooth technology | the first Bluetooth Le audio compatible devices will be launched this year, and the endurance of Bluetooth headsets will be improved by leaps and bounds

Sword finger offer 26 Substructure of tree

Excel, how to choose the right chart?

Gan online learning notes

singan:learning a generative model from a single natural image

in_ Example analysis of the third parameter of array

AB控制器 L36ERM_2个处理器之间的通讯
随机推荐
[step on the pit] resurrect Pico go
Jishili multimeter dmm6500
Sword finger offer 10- I. Fibonacci sequence
dpdk flow filter总结(flow director/ rte_flow)
MySQL 高级原理: MySQL执行过程及执行顺序
三匹马携手乾元公益基金会 | 炎夏送清凉,致敬坚守者 !
Advanced principle of MySQL: MySQL execution process and order
NPM install installation error: gyp info it worked if it ends with OK how to solve it
Mysql5.7 create user error: error 1364 (HY000): field 'SSL_ Cipher 'doesn't have a default value solution
老子云3D产品及服务全介绍,一张图告诉你如何选!
Task-Customized Self-Supervised Pre-training with Scalable Dynamic Routing
[TinyML]NetAug:Network Augmentation for Tiny Deep Learning
Introduction of oscilloscope bandwidth
The third day of MATLAB learning (data types, operators and various operations)
测试/开发程序员幽默的 “自嘲“?印象流派......
贝加莱PLC-更改轴任务扫描时间和周期位置下发
[Luogu at2230] water distribution (pressure DP) (minimum spanning tree)
node请求页面时自动下载
AB控制器 L36ERM_2个处理器之间的通讯
DLS-42/4-4 DC110V双位置继电器