资源简介
某企业面试编程题:蚂蚁爬杆
有一根300厘米的细木杆,在第30厘米、80厘米、110厘米、160厘米、250厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过两只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝相反方向走。假设蚂蚁们每秒钟可以走5厘米的距离。请编写一个程序,计算各种可能情形下所有蚂蚁都离开木杆的最小时间和最大时间。
如题目的图形界面解决方法~c#表达~

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace ANTS
{
public partial class Form1 : Form
{
ants[] ant = new ants[32];
int xBegin = 30 yBegin = 20;
int minTime = 0 maxTime = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender EventArgs e)
{
initializateAnts();
for (int i = 0; i < 32;i++ ) showAnts(ant[i] xBegin + i % 2 * 340 yBegin + i / 2 * 20);
}
private void button2_Click(object sender EventArgs e)
{
progress();
showResult();
}
void initializateAnts()
{
int[] num = new int[5];//32diferent direction
int i1;
for (int i = 0; i < 32; i++)
{
i1 = i;
for (int j = 0; j < 5; j++)
{
num[j] = i1 % 2;
if (num[j] == 0) num[j] = -1;
i1 /= 2;
}
ant[i] = new ants(num[0] num[1] num[2] num[3] num[4]);
}
}//初始化32个蚂蚁通过0-32的二位数字初始化方向
void showAnts(ants antint xint y)
{
Graphics barGra = this.CreateGraphics();
Pen BarPen = new Pen(Color.BurlyWood 10);
barGra.DrawLine(BarPen x-6 y x + 300 + 6 y);//-2+2 CHANGE!!
Graphics antGra = this.CreateGraphics();
Pen antPenLeft = new Pen(Color.Black 2);//CHANGE!!
Pen antPenRight = new Pen(Color.White 2);
for (int i = 0; i < 5;i++ )//画2种方向的蚂蚁
{
if (ant.dir[i] == 1) antGra.DrawRectangle(antPenLeft x + ant.pla[i] y - 1 2 2);
if (ant.dir[i] == -1) antGra.DrawRectangle(antPenRight x + ant.pla[i] y - 1 2 2);
}
Pen meetPen = new Pen(Color.Red 2);//(float)1.5);
for (int i = 0; i < 5; i++)//画碰撞时的爆炸
{
for (int j = i+1; j < 5; j++)
{
if (ant.pla[i] == ant.pla[j]&&ant.pla[i]>10&&ant.pla[i]<290)
antGra.DrawEllipse(meetPen x + ant.pla[i]-2 y - 3 8 6);
}
}
}//显示每个bar
void progress()//蚂蚁移动 延时 及获取minTime、maxTime
{
int gameOver = 1;
int minTimeGot = 0;
int waitTime;
while (gameOver > 0)
{
gameOver = 0;
for (int i = 0; i < 32; i++)
{
ant[i].move();
if (ant[i].alldroped() == true) gameOver++;
showAnts(ant[i] xBegin + i %
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 12800 2008-10-13 09:12 bin\Debug\ANTS.exe
文件 32256 2008-10-13 09:12 bin\Debug\ANTS.pdb
文件 14328 2008-10-13 09:41 bin\Debug\ANTS.vshost.exe
文件 490 2007-07-21 01:33 bin\Debug\ANTS.vshost.exe.manifest
文件 247 2008-10-01 17:26 obj\ANTS.csproj.FileList.txt
文件 1318 2008-10-13 09:41 obj\Debug\ANTS.csproj.FileListAbsolute.txt
文件 847 2008-10-13 09:12 obj\Debug\ANTS.csproj.GenerateResource.Cache
文件 12800 2008-10-13 09:12 obj\Debug\ANTS.exe
文件 180 2008-10-13 09:12 obj\Debug\ANTS.Form1.resources
文件 32256 2008-10-13 09:12 obj\Debug\ANTS.pdb
文件 180 2008-10-07 12:21 obj\Debug\ANTS.Properties.Resources.resources
文件 4608 2008-10-12 09:19 obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
文件 1180 2008-09-29 18:59 Properties\AssemblyInfo.cs
文件 2832 2008-10-07 12:21 Properties\Resources.Designer.cs
文件 5612 2008-09-29 18:59 Properties\Resources.resx
文件 1102 2008-10-07 12:21 Properties\Settings.Designer.cs
文件 249 2008-09-29 18:59 Properties\Settings.settings
文件 3464 2008-10-07 12:21 ANTS.csproj
文件 2560 2008-10-13 09:13 ANTS.suo
文件 1 2008-10-01 17:10 ClassDiagram1.cd
文件 7467 2008-10-13 09:12 Form1.cs
文件 7591 2008-10-13 09:12 Form1.Designer.cs
文件 5814 2008-10-13 09:12 Form1.resx
文件 463 2008-09-29 18:59 Program.cs
目录 0 2008-10-13 14:51 obj\Debug\Refactor
目录 0 2008-10-13 14:51 obj\Debug\TempPE
目录 0 2008-10-13 14:51 bin\Debug
目录 0 2008-10-13 14:51 obj\Debug
目录 0 2008-10-13 14:51 bin
目录 0 2008-10-13 14:51 obj
............此处省略4个文件信息
- 上一篇:C#远程控制软件TwoCare完美无错源代码
- 下一篇:航空管理系统 asp.net
相关资源
- C# TIP文件生成和拆解
- C#解析HL7消息的库135797
- C# OCR数字识别实例,采用TessnetOcr,对
- 考试管理系统 - C#源码
- asp.net C#购物车源代码
- C#实时网络流量监听源码
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
评论
共有 条评论