当前位置:网站首页>蓝桥杯 历届试题 蚂蚁感冒
蓝桥杯 历届试题 蚂蚁感冒
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
边栏推荐
- C语言-函数知识点
- There are thousands of roads. Why did this innovative storage company choose this one?
- easypoi
- How does redis implement inventory deduction? How to prevent oversold?
- Bayesian inference problem, MCMC and variational inference
- Can only one task be submitted by one table if the flinkcdc is submitted by flinksql? When there are thousands of watches
- math_ Proving common equivalent infinitesimal & Case & substitution
- pd. Difference between before and after cut interval parameter setting
- 如何获取飞机穿过雷达两端的坐标
- PCL 环境下安装配置CGAL 5.4.1
猜你喜欢

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

Grafana biaxial graph with your hands

春风动力携手华为打造智慧园区标杆,未来工厂创新迈上新台阶

Installation and configuration of CGAL in PCL environment 5.4.1

Paper 3 vscode & texlive & sumatrapdf create a perfect tool for writing papers

视差js特效js轮播图插件

深度学习需要多强的数学基础?

Demo of intelligent computing system 2 bangc operator development (heterogeneous programming flow of CPU and mlu270)

MDM data analysis function description

Markdown drawing Mermaid practical tutorial
随机推荐
Win11底部状态栏如何换成黑色?Win11底部状态栏换黑色的方法
C#连接数据库完成增删改查操作
Can py SQL get the table structure?
echart:横向柱状图的类目文字位置调整
Kettle (VI): full database backup based on kettle
在arm版本rk3399中搭建halo博客
Nanopc-t4 (rk3399) Game1 OLED (I2C) display time weather temperature
Design of secsha system
阿里开源(EasyExcel)
Bayesian inference problem, MCMC and variational inference
G biaxial graph SQL script
Intelligent computing system 1 environment construction
math_ Proving common equivalent infinitesimal & Case & substitution
Are there any regular and safe foreign exchange dealers in China?
视频压缩处理之ffmpeg用法
机器学习笔记 temperature+Softmax
Can only one task be submitted by one table if the flinkcdc is submitted by flinksql? When there are thousands of watches
Fontawesome icon for color gradient
基于趋势和季节性的时间序列预测
Leetcode 周赛299