| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3232 人关注过本帖, 2 人收藏
标题:我也国庆大赠送,新手尝试一下:猎狗追兔
只看楼主 加入收藏
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
呵呵,又来百分大餐?

授人以渔,不授人以鱼。
2011-10-01 20:42
dreamofgod
Rank: 5Rank: 5
等 级:职业侠客
帖 子:194
专家分:341
注 册:2011-8-16
收藏
得分:0 
回复 31楼 TonyDeng
也许不用,标题独特点就可以了~~~~反正不会有正确答案。

一个单片机就让我头疼不已~~~
2011-10-01 20:46
worldc
Rank: 1
等 级:新手上路
帖 子:4
专家分:8
注 册:2011-10-2
收藏
得分:8 
标准物理竞赛题。。
2011-10-02 08:38
hjywyj
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:1114
专家分:2611
注 册:2010-4-14
收藏
得分:8 
要是一直沿着直线追,那就简单了。
2011-10-02 08:54
hjywyj
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:1114
专家分:2611
注 册:2010-4-14
收藏
得分:0 
从一个点到另一个点,相同的速度。
一直跑直线所花的时间最短。跑曲线花的时间长点。
是不是一直按直线追呢?
2011-10-02 08:56
laznrbfe
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:482
专家分:1599
注 册:2011-5-22
收藏
得分:0 
回复 8楼 TonyDeng
不知道是不是高一奥数题?
2011-10-02 14:14
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
高中物理竞赛经典题型。

授人以渔,不授人以鱼。
2011-10-02 14:19
q332010372
Rank: 2
等 级:论坛游民
帖 子:52
专家分:61
注 册:2010-7-27
收藏
得分:0 
Dog.cs
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Date20111002
{
    class Dog
    {
        public bool dogWin = false;
        public double locationX;
        public double locationY;
        public double x1,y1;
        public double speed;
        public double sina;
        public Dog(double x, double y, int theSpeed) {
            locationX = x;
            locationY = y;
            speed = theSpeed;
        }
    }
}
Rabbit.cs
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Date20111002
{
    class Rabbit
    {
        public bool rabbitWin = false;
        private double X, Y;
        public double speed;
        public double x1, y1;
        public readonly double sina;
        private double rabbitDistance;

        public double locationX {
            get {
                return X;
            }
            set {
                if (value < 0)
                {
                    X = 0;
                    Y = 0;
                }
                else {
                    X = value;
                }
            }
        }

        public double locationY {
          get {
                return Y;
            }
            set {
                if (value < 0)
                {
                    Y = 0;
                    X = 0;
                }
                else {
                    Y = value;
                }
            }
        }
       
        public double RabbitDistance {
            get {
                return rabbitDistance;
            }
            set {
                if (rabbitDistance <= 0)
                {
                    rabbitDistance = 0;
                }
                else {
                    rabbitDistance = value;
                }
            }
        }

        public Rabbit(double x, double y, int theSpeed) {
            X = x;
            Y = y;
            speed = theSpeed;
            rabbitDistance = Math.Sqrt(x*x+y*y);
            sina = y / Math.Sqrt((x * x + y * y));

        }

    }
}
Program.cs
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Date20111002
{
    class Program
    {
        public void Star(Rabbit rabbit, Dog dog)
        {
            double count = 0;
            double dogDistance = Math.Sqrt((dog.locationX - rabbit.locationX) * (dog.locationX - rabbit.locationX) + (dog.locationY - rabbit.locationY) * (dog.locationY - rabbit.locationY));
            Console.WriteLine("兔子离自己的窝距离为: {0}", rabbit.RabbitDistance);
            Console.WriteLine("猎犬离兔子的距离为:{0}\n", dogDistance);
            while (dog.dogWin == false && rabbit.rabbitWin == false) {
           
                    rabbit.RabbitDistance = rabbit.RabbitDistance - rabbit.speed;

                    rabbit.y1 = rabbit.speed * rabbit.sina;
                    rabbit.x1 = Math.Sqrt((rabbit.speed * rabbit.speed) - (rabbit.y1 * rabbit.y1));
                    rabbit.locationY = checked(rabbit.locationY - rabbit.y1);
                    rabbit.locationX = checked(rabbit.locationX - rabbit.x1);

                    Console.WriteLine("兔子已跑至坐标: ({0},{1})", rabbit.locationX, rabbit.locationY);
                    Console.WriteLine("兔子离自己的窝距离为: {0}", rabbit.RabbitDistance);
                    Debug.WriteLine("rabbit:({0},{1})", rabbit.locationX, rabbit.locationY);
                    Debug.WriteLine("dog:({0},{1})", dog.locationX, dog.locationY);

                    dogDistance = Math.Sqrt((dog.locationX - rabbit.locationX) * (dog.locationX - rabbit.locationX) + (dog.locationY - rabbit.locationY) * (dog.locationY - rabbit.locationY));
                    dog.sina = Math.Abs(dog.locationY - rabbit.locationY) / dogDistance;
                    Debug.WriteLine("dog.sina:{0}", dog.sina);

                    if (rabbit.locationX <= dog.locationX && rabbit.locationY >= dog.locationY)
                    {
                        dog.y1 = dog.speed * dog.sina;
                        dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1)));
                        dog.locationX -= dog.x1;
                        dog.locationY += dog.y1;
                    }
                    else if (rabbit.locationX >= dog.locationX && rabbit.locationY >= dog.locationY)
                    {
                        dog.y1 = dog.speed * dog.sina;
                        dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1)));
                        dog.locationX += dog.x1;
                        dog.locationY += dog.y1; 
                    }
                    else if (rabbit.locationX <= dog.locationX && rabbit.locationY <= dog.locationY)
                    {
                        dog.y1 = dog.speed * dog.sina;
                        dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1)));
                        dog.locationX -= dog.x1;
                        dog.locationY -= dog.y1;
                    }
                    else if (rabbit.locationX >= dog.locationX && rabbit.locationY <= dog.locationY)
                    {
                        dog.y1 = dog.speed * dog.sina;
                        dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1)));
                        dog.locationX += dog.x1;
                        dog.locationY -= dog.y1;
                    }

                    dogDistance = Math.Sqrt((dog.locationX - rabbit.locationX) * (dog.locationX - rabbit.locationX) + (dog.locationY - rabbit.locationY) * (dog.locationY - rabbit.locationY));
                    Console.WriteLine("猎犬已跑至坐标: ({0},{1})", dog.locationX, dog.locationY);
                    Console.WriteLine("猎犬离兔子的距离为:{0}\n", dogDistance);
                    count++;
                    if ((rabbit.locationX == 0 && rabbit.locationY == 0) || rabbit.RabbitDistance == 0)
                    {
                        rabbit.rabbitWin = true;
                        Console.WriteLine("The rabbit win for {0} seconds !",count/20);
                    }
                    else if (dogDistance <= 0.02) {
                        dog.dogWin = true;
                        Console.WriteLine("The dog win for {0} seconds !", count/20);
                    }
            }
        }

        static void Main(string[] args)
        {
            Program newStar = new Program();
            Rabbit theRabbit = new Rabbit(8.0,3.0, 1);
            Dog theDog = new Dog(16.0, 2.0, 1);
            theDog.speed /= 20.0;
            theRabbit.speed /= 20.0;
            Debug.WriteLine("rabbit.sina:{0}\n", theRabbit.sina);
            Console.WriteLine("兔子的坐标:({0},{1}), 速度:{2}", theRabbit.locationX, theRabbit.locationY, theRabbit.speed*20);
            Console.WriteLine("猎犬的坐标:({0},{1}), 速度:{2}\n", theDog.locationX, theDog.locationY, theDog.speed*20);

            Console.WriteLine("开始计算...\n");
            newStar.Star(theRabbit, theDog);

            Console.ReadKey();
        }
    }
}
C#写的,会C……


收到的鲜花
  • TonyDeng2011-10-02 14:44 送鲜花  10朵   附言:原创内容 辛苦了!得看看C#怎么测试这个程 ...
2011-10-02 14:34
q332010372
Rank: 2
等 级:论坛游民
帖 子:52
专家分:61
注 册:2010-7-27
收藏
得分:0 
只写了第一象限,兔子窝设定在原点。如果要写4个象限,更改窝的坐标的话,就需要重写兔子的直线方程
2011-10-02 15:20
q332010372
Rank: 2
等 级:论坛游民
帖 子:52
专家分:61
注 册:2010-7-27
收藏
得分:0 
还没学windows窗体,自己不能测试,总感觉有地方写错了。

[ 本帖最后由 q332010372 于 2011-10-2 16:14 编辑 ]
2011-10-02 16:10
快速回复:我也国庆大赠送,新手尝试一下:猎狗追兔
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014658 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved