历届试题 蚂蚁感冒
2018-02-07 18:43
302 查看
问题描述
长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
主要就是一个思维的问题
1.两点相撞返回看成是交错
2.各个蚂蚁的相对位置不变(这题没用)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn=110;
struct Ant {
int id;
int dir;
int pos;
bool operator<(const Ant& a)const {
return pos<a.pos;
}
} st[maxn];
int main()
{
int n;
scanf("%d",&n);
int stx;
for(int i=0; i<n; i++) {
int pos,d;
scanf("%d",&pos);
if(i==0) stx=abs(pos);
if(pos>0)
st[i]=(Ant) {
0, 1, pos
};//神奇的赋值方式
else
st[i]=(Ant) {
0,-1,-pos
};
}
sort(st,st+n);
int st_id;
for(int i=0; i<n; i++) {
//printf("i:%d %d\n",i,st[i].pos);
st[i].id=i;
if(st[i].pos==stx) st_id=i;
}
//printf("st_id:%d\n",st_id);
int ans=0;
if(st[st_id].id==1) {
for(int i=st_id+1; i<n; i++) {
if(st[i].dir==-1)
ans++;
}
if(ans)
for(int i=0; i<st_id; i++)
if(st[i].dir==1)
ans++;
} else {
for(int i=0; i<st_id; i++)
if(st[i].dir==1)
ans++;
if(ans)
for(int i=st_id+1; i<n; i++) {
if(st[i].dir==-1)
ans++;
}
}
printf("%d\n",ans+1);
return 0;
}
长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
主要就是一个思维的问题
1.两点相撞返回看成是交错
2.各个蚂蚁的相对位置不变(这题没用)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn=110;
struct Ant {
int id;
int dir;
int pos;
bool operator<(const Ant& a)const {
return pos<a.pos;
}
} st[maxn];
int main()
{
int n;
scanf("%d",&n);
int stx;
for(int i=0; i<n; i++) {
int pos,d;
scanf("%d",&pos);
if(i==0) stx=abs(pos);
if(pos>0)
st[i]=(Ant) {
0, 1, pos
};//神奇的赋值方式
else
st[i]=(Ant) {
0,-1,-pos
};
}
sort(st,st+n);
int st_id;
for(int i=0; i<n; i++) {
//printf("i:%d %d\n",i,st[i].pos);
st[i].id=i;
if(st[i].pos==stx) st_id=i;
}
//printf("st_id:%d\n",st_id);
int ans=0;
if(st[st_id].id==1) {
for(int i=st_id+1; i<n; i++) {
if(st[i].dir==-1)
ans++;
}
if(ans)
for(int i=0; i<st_id; i++)
if(st[i].dir==1)
ans++;
} else {
for(int i=0; i<st_id; i++)
if(st[i].dir==1)
ans++;
if(ans)
for(int i=st_id+1; i<n; i++) {
if(st[i].dir==-1)
ans++;
}
}
printf("%d\n",ans+1);
return 0;
}
相关文章推荐
- 蓝桥杯 历届试题 蚂蚁感冒
- 历届试题 蚂蚁感冒
- 【蓝桥杯训练】 历届试题 蚂蚁感冒
- 历届试题 蚂蚁感冒 (找规律模拟)
- 历届试题 蚂蚁感冒
- 【历届试题 蚂蚁感冒】(思维)
- (蓝桥杯)历届试题 蚂蚁感冒 (思维题)
- 历届试题 蚂蚁感冒
- 历届试题 蚂蚁感冒
- 历届试题 蚂蚁感冒
- 算法笔记_175:历届试题 蚂蚁感冒(Java)
- 历届试题 蚂蚁感冒(创建了对象、不创建对象)
- 蓝桥杯 历届试题 蚂蚁感冒
- 蓝桥杯历届试题 蚂蚁感冒(模拟)
- 蓝桥杯 历届试题 蚂蚁感冒(模拟)
- 蓝桥杯 历届试题 蚂蚁感冒
- 蓝桥杯 历届试题 蚂蚁感冒 详解
- 历届试题 蚂蚁感冒
- 历届试题 PREV-27 蚂蚁感冒
- 历届试题 蚂蚁感冒 蓝桥杯