当前位置:网站首页>蓝桥杯 历届试题 蚂蚁感冒
蓝桥杯 历届试题 蚂蚁感冒
2022-06-28 19:29:00 【Yuyy】
本文最后更新于 1174 天前,其中的信息可能已经有所发展或是发生改变。
问题描述
长100厘米的细长直杆子上有n只蚂蚁。它们的头有的朝左,有的朝右。
每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒。
当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。
这些蚂蚁中,有1只蚂蚁感冒了。并且在和其它蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。
请你计算,当所有蚂蚁都爬离杆子时,有多少只蚂蚁患上了感冒。
输入格式
第一行输入一个整数n (1 < n < 50), 表示蚂蚁的总数。
接着的一行是n个用空格分开的整数 Xi (-100 < Xi < 100), Xi的绝对值,表示蚂蚁离开杆子左边端点的距离。正值表示头朝右,负值表示头朝左,数据中不会出现0值,也不会出现两只蚂蚁占用同一位置。其中,第一个数据代表的蚂蚁感冒了。
输出格式
要求输出1个整数,表示最后感冒蚂蚁的数目。 样例输入 3 5 -2 8 样例输出 1 样例输入 5 -10 8 -20 12 25 样例输出 3
提示
当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。可以理解为插肩而过,效果一样。
代码
using namespace std;
int main(){
int array[55];
int n,flag,flag1=0,flag2=0,temp,sum=1;
cin>>n;
cin>>flag;
for(int i=1;i<n;i++){
cin>>array[i];
//cin>>temp;
//array[i]=temp;
}
if(flag>0){
for(int i=1;i<n;i++){
if(array[i]<0&&(flag+array[i])<0){
sum++;
flag1=1;
}
}
if(flag1==1){
for(int i=1;i<n;i++){
if(array[i]>0&&(flag-array[i])>0){
sum++;
}
}
}
}
else{
for(int i=1;i<n;i++){
if(array[i]>0&&(flag+array[i])<0){
sum++;
flag2=1;
}
}
if(flag2==1){
for(int i=1;i<n;i++){
if(array[i]<0&&(flag-array[i])>0){
sum++;
}
}
}
}
cout<<sum;
return 0;
}Post Views: 266
边栏推荐
- NanoPC-T4(RK3399) game1 oled(I2C)显示时间天气温度
- C language - function knowledge points
- 5G NR MBS架构介绍
- Full MySQL solution ubuntu/win10
- 秋招经验分享 | 银行笔面试该怎么准备
- 易观分析《2022年中国银行业隐私计算平台供应商实力矩阵分析》研究活动 正式启动
- echart:横向柱状图的类目文字位置调整
- Web3 that unleashes the value of the Internet
- Industry analysis - quick intercom, building intercom
- PY SQL可以获取到表结构吗?
猜你喜欢

math_证明常用等价无穷小&案例&代换

Bayesian inference problem, MCMC and variational inference

《数据安全法》出台一周年,看哪四大变化来袭?

R语言GLM广义线性模型:逻辑回归、泊松回归拟合小鼠临床试验数据(剂量和反应)示例和自测题

PCL 环境下安装配置CGAL 5.4.1

视频压缩处理之ffmpeg用法

I. The HR system is put on the enterprise wechat ISV to enhance the in-depth application of enterprise wechat in service chain retail and other industries

视差js特效js轮播图插件

Figure introduction to neural networks (GNN, GCN)

SQL interview question: find the maximum number of consecutive login days
随机推荐
Constrained Delaunay triangulation in MATLAB
微信小程序_8,视图与逻辑
Win11底部状态栏如何换成黑色?Win11底部状态栏换黑色的方法
Why is it not enough to declare the structure alias when using the structure variable of other files in C language, and the proper name must be used? (cannot add struct when using alias)
pd. Difference between before and after cut interval parameter setting
matlab 二维或三维三角剖分
Build halo blog in arm version rk3399
视差js特效js轮播图插件
【324. 摆动排序 II】
列表加入计时器(正计时、倒计时)
Group programming TIANTI competition exercise - continuously updating
小白创业做电商,选对商城系统很重要!
Ffmpeg learning summary
Live app system source code, automatically playing when encountering video dynamically
数据库学习笔记(SQL04)
2280.Cupboards
How does win11 slim down the system disk? Slimming method of win11 system disk
Intelligent computing system 1 environment construction
100人成绩的平均
kettle(六):基于Kettle的数据库全量备份