ANT(重要思想!)

来源:互联网 发布:12306网络购票暂停 编辑:程序博客网 时间:2024/06/05 11:34

一根长度为L厘米的木棒上有N只蚂蚁,每只蚂蚁要么向左走,要么向右走,速度为1厘米/秒。当两只蚂蚁相撞时,他们会同时掉头(掉头时间不计)给出每只蚂蚁距离木棒左端的距离,问多少秒后,刚好所有蚂蚁都从木棒上掉下来。

N 和 L均不超过1000

输入第一行两个整数,分别是N和L

接下来N行,每行先是一个字符,L或R,代表向左还是向右,然后是一个整数x,代表这个蚂蚁距离木棒左端的距离。

样例输入:

4 10

R 1

R 5

L 3

R 9

样例输出:

9

#include <stdio.h>int main() {    int time = 0;    int currenttime = 0;    int n, l;    char direction;    int dis;    scanf("%d%d", &n, &l);    while (n--) {        scanf(" %c%d", &direction, &dis);        if (direction == 'R') {            currenttime = l - dis;        }        if (time < currenttime) {            time = currenttime;        }    }    printf("%d\n", time);}

关键提示:两只蚂蚁相对走,一起转向和没有转向有什么区别?

0 0
原创粉丝点击