`

蚂蚁问题

    博客分类:
  • IQ
阅读更多
之前看有的朋友谈百度的一道面试试题-蚂蚁问题(有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间)。关于这道题目,网上给出了很多的解释,但从整体来看,基本都是用到了等价置换(等量代换)的思想。要求最小时间,即为“最不容易”先到达两端的蚂蚁以最短的时间到达,所以我们只需找到所有蚂蚁中间的一只(共奇数只蚂蚁)或两只(共偶数只蚂蚁)到达一端的最短时间。比较麻烦的是求最长时间,有人会觉得当有很多只蚂蚁时,中间的蚂蚁们相互碰撞的次数多些会增加时间,感觉上比较复杂,可如果我们用等量代换的思想来解释就比较容易。假设中间的任意两只相邻蚂蚁即将发生碰撞,如:A ->        <-B,当A,B发生碰撞后,便有<-A    B->。A,B反向相当于<-B   A -> ,即二者继续向着原来的方向前进,对于任意相邻的发生碰撞的蚂蚁都适用,所以只需求最两端的两只蚂蚁距离两端的最远距离。由以上分析可知,如果出这样的问题,我们可以不用通过程序便能说出结果:5个点,中间蚂蚁的位置为11,即0-11-27,显然最小为11,最两端蚂蚁,0-3-27,最大为24,0-23-27,最大为23,所以最大为24。对于这个题,给出如下Java代码(随便写了几句,不符合面向对象思想)。
public class Ant {
    public static void main(String[] args){
        int length=27,points=5,min=0,max=0,temp_min=0,temp_max=0;
        int[] pos={3,7,11,17,23};
        for(int i: pos){
            temp_min=i>length-i?length-i:i;
            temp_max=i<length-i?length-i:i;
            if(temp_min>min)
                min=temp_min;
            if(temp_max>max)
                max=temp_max;
        }
        System.out.println("最短时间:"+min+"  最长时间:"+max);
    }
}有了如上的想法,我们能做出判断,为什么还要写代码呢?其实这个问题出自Waterloo Local Contest Sep.19,2004 准确描述如下:
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.

For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.

Sample Input
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191

Sample Output
4 8
38 207

在这里给出相应的c++代码:
#include<iostream>
using namespace std;
int main()
{
    int cases,l,n,min,max,temp_min,temp_max,pos;
    cin>>cases;
    while(cases--)
    {
        cin>>l>>n;
        min=0;
        max=0;
        while(n--)
        {
            cin>>pos;
            temp_min=pos>l-pos?l-pos:pos;
            temp_max=pos<l-pos?l-pos:pos;
            if(temp_min>min)
                min=temp_min;
            if(temp_max>max)
                max=temp_max;
        }
        cout<<min<<' '<<max<<endl;
    }
    return 0;
}
分享到:
评论

相关推荐

    怎样以Java编程解决趣味蚂蚁问题.pdf

    趣味蚂蚁问题是一个最近在互联网上出现的一个智力问 题。其具体描述如下: 有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过两只蚂蚁。开始时,...

    蚂蚁问题 C++

    很简单的蚂蚁问题实现,对于面向对象的学习时做的一个小的实验

    蚂蚁爬杆问题

    蚂蚁爬杆问题 A.B.C三只蚂蚁不同速度在一个杆上爬行,求蚂蚁爬出杆的时间问题

    蚂蚁感冒问题

    长100厘米的细长直杆子上有n只蚂蚁。它们的头有的朝左,有的朝右。 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒。 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。 这些蚂蚁中,有1只蚂蚁感冒了。...

    c++蚂蚁爬杆问题

    蚂蚁爬杆自己写的,希望大神能够帮助我写代码的质量,有什么问题随便提出来,自己一定会改正的谢谢

    蚂蚁与木棍问题仿真

    一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1厘米/秒。当两只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计)。给出每只蚂蚁的初始位置和朝向,计算T秒之后每只蚂蚁的位置。 程序给出...

    蚂蚁爬行问题源码

    用java实现的蚂蚁爬行问题 有界面 5只蚂蚁从一根杆子上的5个初始位置爬行,方向随机,相遇则回头,计算最大时间和最小时间

    蚂蚁爬杆问题(面向对象)

    用面向对象的思维方式解决蚂蚁爬杆问题,并将其过程进行可视化。

    蚂蚁云客服机器人面试答案.docx

    网络: 吞吐量、吞吐率 应用: jvm内存、日志、Full GC频率 3、微服务涉及到的技术以及需要注意的问题有哪些? 4、注册中心你了解了哪些? 答:Consul 、Eureka、ZooKeeper 5、consul 的可靠性你了解吗? 6、consul...

    AS_TSP(蚂蚁系统)_蚂蚁系统_TSP问题_

    蚁群算法用于求解一个经典的组合优化问题,旅行商问题

    人工蚂蚁源程序

    人工智能实验,人工蚂蚁问题的源程序实现。这个程序是很多初学者在学习人工智能课程时都用得到的

    蚂蚁算法解决TSP

    利用c语言编写的,蚂蚁算法解决TSP问题,下载后可直接运行。

    蚂蚁算法解决目标问题

    用蚂蚁算法解决多目标TSP问题,对多目标进行深入探讨,

    蚂蚁的移动速度问题

    对于同一根木棍,在上面的蚂蚁如何移动才能最快地离开木棍。这是一个有趣的问题?但是如何能更快第求出最短时间和最长时间呢?这是一个有难度的编程问题。请参看本文档的具体实现思路和代码吧。保证让你获益匪浅。

    蚂蚁聚类算法MATLAB程序

    改进的蚂蚁聚类算法,可运行。

    蚂蚁算法在结构优化中的应用研究

    蚂蚁算法作为一种新型的放生优化算法,已经在许多工程领域有成功的应用先例,并被证明是高质量的。但对于土木结构的优化问题,几乎没有相应的应用 实例。本研究尝试将蚂蚁算法(AntAlgorithm)应用到结构优化设计上来...

    蚂蚁分类信息系统4.0 地方门户系统单城市

    蚂蚁分类信息系统4.0 蚂蚁4.0(地方门户系统单城市)无域名限制破解版.rar 无密码 好东西当然要分享

    蚂蚁算法解决TSP问题

    A programming tools (Visual Studio .Net) to development an ACO optimization system for traveling salesman problem. Particularly, you may implement AS...用蚂蚁算法解决旅行商问题,可以读取标准化后的TSP档案

Global site tag (gtag.js) - Google Analytics