程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 狼追兔子問題C模擬解

狼追兔子問題C模擬解

編輯:關於C語言

說,一只兔子在O點處,它的洞穴在正北20m的B點處,一只狼位於兔子正東33米的A點處,模擬如下追逐問題:狼以一倍於兔子的速度緊盯兔子追擊,問兔子到達洞口前是否會被狼逮住?

網上很多人說能被追上,包括類似的一題距離兔子100碼,洞在60碼處的。那麼究竟能不能呢?

通過計算機模擬,不用解算微分方程,就可以找到答案。

計算機要模擬,基本上就是用第推算法,給出初始條件,按公式計算下一步,如此往復直到一定條件終止計算。

模擬追逐問題,就把時間切片來看,這一步是什麼樣下一步又是如何,這裡把速度可以轉換為步進距離來看。

假設平面坐標系上,兔子位於原點O,正北為y軸正向,狼從I象限追進。

 

[cpp]  #include <stdio.h>  
#include <math.h>  
 
double Td,Tx,Ty,Ld,Lx,Ly; 
 
double dist() 

    return sqrt((Tx-Lx)*(Tx-Lx)+(Ty-Ly)*(Ty-Ly)); 

 
//狼位置變化,指向兔子的線段上跑動Ld距離,相應ldx,ldy分量  
double NextL() 

    double dx=fabs(Tx-Lx); 
    double dy=fabs(Ty-Ly); 
    double k,dk; 
    int usek=1; 
    double ldx; 
    double ldy; 
    //  
    if(dx > 0.00000000001 ) 
    { 
        k=dy/dx; 
    }else 
    { 
        usek=0; 
        dk=dx/dy; 
    } 
    //  
    if(usek==1) 
    { 
        ldx=sqrt((Ld*Ld)/(k*k+1)); 
        ldy=k*ldx; 
    }else 
    { 
        ldy=sqrt((Ld*Ld)/(dk*dk+1)); 
        ldx=dk*ldy; 
    } 
    //僅限I象限非震蕩追進  
    Lx=Lx-ldx; 
    Ly=Ly+ldy; 

 
//兔子位置變化  
double NextT() 

    Tx=Tx; 
    Ty=Ty+Td; 

 
void loop() 

//坐標建立,初始化  
 Tx=0.0; 
 Ty=0.0; 
 Lx=33.0; 
 Ly=0.0; 
//兔子步進距離  
 Td=0.1; 
//狼步進距離  
 Ld=Td*2.0; 
    while( dist() >= Td ) 
    { 
        NextL(); 
        NextT(); 
        printf("兔子:(%lf,%lf) 狼:(%lf,%lf)\n",Tx,Ty,Lx,Ly); 
    } 
    //  

 
int main(int argc,char** argv) 

    loop(); 
    return 0; 

#include <stdio.h>
#include <math.h>

double Td,Tx,Ty,Ld,Lx,Ly;

double dist()
{
 return sqrt((Tx-Lx)*(Tx-Lx)+(Ty-Ly)*(Ty-Ly));
}

//狼位置變化,指向兔子的線段上跑動Ld距離,相應ldx,ldy分量
double NextL()
{
 double dx=fabs(Tx-Lx);
 double dy=fabs(Ty-Ly);
 double k,dk;
 int usek=1;
 double ldx;
 double ldy;
 //
 if(dx > 0.00000000001 )
 {
  k=dy/dx;
 }else
 {
  usek=0;
  dk=dx/dy;
 }
 //
 if(usek==1)
 {
  ldx=sqrt((Ld*Ld)/(k*k+1));
  ldy=k*ldx;
 }else
 {
  ldy=sqrt((Ld*Ld)/(dk*dk+1));
  ldx=dk*ldy;
 }
 //僅限I象限非震蕩追進
 Lx=Lx-ldx;
 Ly=Ly+ldy;
}

//兔子位置變化
double NextT()
{
 Tx=Tx;
 Ty=Ty+Td;
}

void loop()
{
//坐標建立,初始化
 Tx=0.0;
 Ty=0.0;
 Lx=33.0;
 Ly=0.0;
//兔子步進距離
 Td=0.1;
//狼步進距離
 Ld=Td*2.0;
 while( dist() >= Td )
 {
  NextL();
  NextT();
  printf("兔子:(%lf,%lf) 狼:(%lf,%lf)\n",Tx,Ty,Lx,Ly);
 }
 //
}

int main(int argc,char** argv)
{
 loop();
 return 0;
}


這裡,NextT給出了兔子的位置第推公式,NextL給出了狼的位置第推公式,在循環時因為狼是根據兔子的位子來做下一步推算的所以要先於兔子位置變換來做移動(反過來的話也可,就是說狼還能稍稍預測兔子的動向,如果狼能預測判斷兔子路徑完全跑直線可追上兔子無疑,然而事實一般都是緊追不捨的多。)


運行結果

兔子:(0.000000,0.100000) 狼:(32.800000,0.000000)
兔子:(0.000000,0.200000) 狼:(32.600001,0.000610)
兔子:(0.000000,0.300000) 狼:(32.400005,0.001833)
兔子:(0.000000,0.400000) 狼:(32.200013,0.003673)
兔子:(0.000000,0.500000) 狼:(32.000028,0.006135)
兔子:(0.000000,0.600000) 狼:(31.800052,0.009221)
兔子:(0.000000,0.700000) 狼:(31.600087,0.012936)
兔子:(0.000000,0.800000) 狼:(31.400134,0.017284)
兔子:(0.000000,0.900000) 狼:(31.200196,0.022267)
兔子:(0.000000,1.000000) 狼:(31.000275,0.027892)
兔子:(0.000000,1.100000) 狼:(30.800373,0.034160)
兔子:(0.000000,1.200000) 狼:(30.600493,0.041077)
兔子:(0.000000,1.300000) 狼:(30.400636,0.048646)
兔子:(0.000000,1.400000) 狼:(30.200805,0.056872)
兔子:(0.000000,1.500000) 狼:(30.001003,0.065757)
兔子:(0.000000,1.600000) 狼:(29.801231,0.075308)
兔子:(0.000000,1.700000) 狼:(29.601492,0.085527)
兔子:(0.000000,1.800000) 狼:(29.401789,0.096419)
兔子:(0.000000,1.900000) 狼:(29.202124,0.107988)
兔子:(0.000000,2.000000) 狼:(29.002500,0.120238)
兔子:(0.000000,2.100000) 狼:(28.802918,0.133173)
兔子:(0.000000,2.200000) 狼:(28.603383,0.146799)
兔子:(0.000000,2.300000) 狼:(28.403896,0.161118)
兔子:(0.000000,2.400000) 狼:(28.204461,0.176136)
兔子:(0.000000,2.500000) 狼:(28.005080,0.191857)
兔子:(0.000000,2.600000) 狼:(27.805756,0.208285)
兔子:(0.000000,2.700000) 狼:(27.606491,0.225425)
兔子:(0.000000,2.800000) 狼:(27.407290,0.243281)
兔子:(0.000000,2.900000) 狼:(27.208155,0.261857)
兔子:(0.000000,3.000000) 狼:(27.009088,0.281159)
兔子:(0.000000,3.100000) 狼:(26.810094,0.301191)
兔子:(0.000000,3.200000) 狼:(26.611175,0.321957)
兔子:(0.000000,3.300000) 狼:(26.412334,0.343462)
兔子:(0.000000,3.400000) 狼:(26.213576,0.365710)
兔子:(0.000000,3.500000) 狼:(26.014902,0.388707)
兔子:(0.000000,3.600000) 狼:(25.816317,0.412457)
兔子:(0.000000,3.700000) 狼:(25.617825,0.436965)
兔子:(0.000000,3.800000) 狼:(25.419428,0.462236)
兔子:(0.000000,3.900000) 狼:(25.221130,0.488274)
兔子:(0.000000,4.000000) 狼:(25.022935,0.515084)
兔子:(0.000000,4.100000) 狼:(24.824847,0.542671)
兔子:(0.000000,4.200000) 狼:(24.626869,0.571041)
兔子:(0.000000,4.300000) 狼:(24.429006,0.600198)
兔子:(0.000000,4.400000) 狼:(24.231261,0.630146)
兔子:(0.000000,4.500000) 狼:(24.033638,0.660892)
兔子:(0.000000,4.600000) 狼:(23.836142,0.692440)
兔子:(0.000000,4.700000) 狼:(23.638776,0.724795)
兔子:(0.000000,4.800000) 狼:(23.441546,0.757962)
兔子:(0.000000,4.900000) 狼:(23.244454,0.791947)
兔子:(0.000000,5.000000) 狼:(23.047506,0.826754)
兔子:(0.000000,5.100000) 狼:(22.850707,0.862389)
兔子:(0.000000,5.200000) 狼:(22.654060,0.898857)
兔子:(0.000000,5.300000) 狼:(22.457570,0.936162)
兔子:(0.000000,5.400000) 狼:(22.261242,0.974312)
兔子:(0.000000,5.500000) 狼:(22.065081,1.013310)
兔子:(0.000000,5.600000) 狼:(21.869092,1.053162)
兔子:(0.000000,5.700000) 狼:(21.673279,1.093874)
兔子:(0.000000,5.800000) 狼:(21.477648,1.135451)
兔子:(0.000000,5.900000) 狼:(21.282204,1.177897)
兔子:(0.000000,6.000000) 狼:(21.086953,1.221220)
兔子:(0.000000,6.100000) 狼:(20.891899,1.265423)
兔子:(0.000000,6.200000) 狼:(20.697048,1.310514)
兔子:(0.000000,6.300000) 狼:(20.502406,1.356496)
兔子:(0.000000,6.400000) 狼:(20.307978,1.403376)
兔子:(0.000000,6.500000) 狼:(20.113770,1.451160)
兔子:(0.000000,6.600000) 狼:(19.919788,1.499852)
兔子:(0.000000,6.700000) 狼:(19.726037,1.549459)
兔子:(0.000000,6.800000) 狼:(19.532525,1.599985)
兔子:(0.000000,6.900000) 狼:(19.339257,1.651438)
兔子:(0.000000,7.000000) 狼:(19.146239,1.703822)
兔子:(0.000000,7.100000) 狼:(18.953478,1.757143)
兔子:(0.000000,7.200000) 狼:(18.760980,1.811407)
兔子:(0.000000,7.300000) 狼:(18.568752,1.866619)
兔子:(0.000000,7.400000) 狼:(18.376800,1.922786)
兔子:(0.000000,7.500000) 狼:(18.185133,1.979913)
兔子:(0.000000,7.600000) 狼:(17.993755,2.038005)
兔子:(0.000000,7.700000) 狼:(17.802676,2.097069)
兔子:(0.000000,7.800000) 狼:(17.611901,2.157110)
兔子:(0.000000,7.900000) 狼:(17.421438,2.218135)
兔子:(0.000000,8.000000) 狼:(17.231295,2.280149)
兔子:(0.000000,8.100000) 狼:(17.041480,2.343157)
兔子:(0.000000,8.200000) 狼:(16.851999,2.407166)
兔子:(0.000000,8.300000) 狼:(16.662862,2.472182)
兔子:(0.000000,8.400000) 狼:(16.474076,2.538210)
兔子:(0.000000,8.500000) 狼:(16.285648,2.605256)
兔子:(0.000000,8.600000) 狼:(16.097589,2.673326)
兔子:(0.000000,8.700000) 狼:(15.909905,2.742425)
兔子:(0.000000,8.800000) 狼:(15.722605,2.812561)
兔子:(0.000000,8.900000) 狼:(15.535700,2.883738)
兔子:(0.000000,9.000000) 狼:(15.349196,2.955962)
兔子:(0.000000,9.100000) 狼:(15.163103,3.029240)
兔子:(0.000000,9.200000) 狼:(14.977431,3.103576)
兔子:(0.000000,9.300000) 狼:(14.792189,3.178977)
兔子:(0.000000,9.400000) 狼:(14.607386,3.255449)
兔子:(0.000000,9.500000) 狼:(14.423032,3.332997)
兔子:(0.000000,9.600000) 狼:(14.239137,3.411627)
兔子:(0.000000,9.700000) 狼:(14.055711,3.491344)
兔子:(0.000000,9.800000) 狼:(13.872764,3.572155)
兔子:(0.000000,9.900000) 狼:(13.690307,3.654065)
兔子:(0.000000,10.000000) 狼:(13.508349,3.737080)
兔子:(0.000000,10.100000) 狼:(13.326902,3.821204)
兔子:(0.000000,10.200000) 狼:(13.145977,3.906445)
兔子:(0.000000,10.300000) 狼:(12.965584,3.992807)
兔子:(0.000000,10.400000) 狼:(12.785735,4.080296)
兔子:(0.000000,10.500000) 狼:(12.606441,4.168917)
兔子:(0.000000,10.600000) 狼:(12.427714,4.258675)
兔子:(0.000000,10.700000) 狼:(12.249565,4.349577)
兔子:(0.000000,10.800000) 狼:(12.072007,4.441627)
兔子:(0.000000,10.900000) 狼:(11.895052,4.534830)
兔子:(0.000000,11.000000) 狼:(11.718711,4.629191)
兔子:(0.000000,11.100000) 狼:(11.542999,4.724716)
兔子:(0.000000,11.200000) 狼:(11.367926,4.821410)
兔子:(0.000000,11.300000) 狼:(11.193507,4.919277)
兔子:(0.000000,11.400000) 狼:(11.019755,5.018323)
兔子:(0.000000,11.500000) 狼:(10.846682,5.118552)
兔子:(0.000000,11.600000) 狼:(10.674302,5.219968)
兔子:(0.000000,11.700000) 狼:(10.502630,5.322577)
兔子:(0.000000,11.800000) 狼:(10.331678,5.426382)
兔子:(0.000000,11.900000) 狼:(10.161462,5.531389)
兔子:(0.000000,12.000000) 狼:(9.991995,5.637601)
兔子:(0.000000,12.100000) 狼:(9.823292,5.745022)
兔子:(0.000000,12.200000) 狼:(9.655368,5.853657)
兔子:(0.000000,12.300000) 狼:(9.488238,5.963509)
兔子:(0.000000,12.400000) 狼:(9.321917,6.074583)
兔子:(0.000000,12.500000) 狼:(9.156421,6.186881)
兔子:(0.000000,12.600000) 狼:(8.991764,6.300408)
兔子:(0.000000,12.700000) 狼:(8.827964,6.415166)
兔子:(0.000000,12.800000) 狼:(8.665035,6.531158)
兔子:(0.000000,12.900000) 狼:(8.502995,6.648389)
兔子:(0.000000,13.000000) 狼:(8.341859,6.766859)
兔子:(0.000000,13.100000) 狼:(8.181645,6.886573)
兔子:(0.000000,13.200000) 狼:(8.022369,7.007533)
兔子:(0.000000,13.300000) 狼:(7.864049,7.129740)
兔子:(0.000000,13.400000) 狼:(7.706701,7.253198)
兔子:(0.000000,13.500000) 狼:(7.550344,7.377907)
兔子:(0.000000,13.600000) 狼:(7.394995,7.503870)
兔子:(0.000000,13.700000) 狼:(7.240672,7.631088)
兔子:(0.000000,13.800000) 狼:(7.087393,7.759561)
兔子:(0.000000,13.900000) 狼:(6.935176,7.889292)
兔子:(0.000000,14.000000) 狼:(6.784041,8.020281)
兔子:(0.000000,14.100000) 狼:(6.634005,8.152529)
兔子:(0.000000,14.200000) 狼:(6.485088,8.286034)
兔子:(0.000000,14.300000) 狼:(6.337310,8.420799)
兔子:(0.000000,14.400000) 狼:(6.190688,8.556821)
兔子:(0.000000,14.500000) 狼:(6.045244,8.694101)
兔子:(0.000000,14.600000) 狼:(5.900995,8.832638)
兔子:(0.000000,14.700000) 狼:(5.757964,8.972431)
兔子:(0.000000,14.800000) 狼:(5.616169,9.113478)
兔子:(0.000000,14.900000) 狼:(5.475630,9.255776)
兔子:(0.000000,15.000000) 狼:(5.336369,9.399326)
兔子:(0.000000,15.100000) 狼:(5.198406,9.544122)
兔子:(0.000000,15.200000) 狼:(5.061761,9.690164)
兔子:(0.000000,15.300000) 狼:(4.926455,9.837447)
兔子:(0.000000,15.400000) 狼:(4.792509,9.985968)
兔子:(0.000000,15.500000) 狼:(4.659945,10.135724)
兔子:(0.000000,15.600000) 狼:(4.528784,10.286710)
兔子:(0.000000,15.700000) 狼:(4.399047,10.438921)
兔子:(0.000000,15.800000) 狼:(4.270755,10.592353)
兔子:(0.000000,15.900000) 狼:(4.143931,10.746999)
兔子:(0.000000,16.000000) 狼:(4.018595,10.902855)
兔子:(0.000000,16.100000) 狼:(3.894770,11.059913)
兔子:(0.000000,16.200000) 狼:(3.772477,11.218168)
兔子:(0.000000,16.300000) 狼:(3.651739,11.377612)
兔子:(0.000000,16.400000) 狼:(3.532577,11.538237)
兔子:(0.000000,16.500000) 狼:(3.415014,11.700035)
兔子:(0.000000,16.600000) 狼:(3.299070,11.862999)
兔子:(0.000000,16.700000) 狼:(3.184770,12.027119)
兔子:(0.000000,16.800000) 狼:(3.072133,12.192386)
兔子:(0.000000,16.900000) 狼:(2.961183,12.358789)
兔子:(0.000000,17.000000) 狼:(2.851942,12.526319)
兔子:(0.000000,17.100000) 狼:(2.744432,12.694965)
兔子:(0.000000,17.200000) 狼:(2.638673,12.864716)
兔子:(0.000000,17.300000) 狼:(2.534690,13.035559)
兔子:(0.000000,17.400000) 狼:(2.432502,13.207482)
兔子:(0.000000,17.500000) 狼:(2.332132,13.380473)
兔子:(0.000000,17.600000) 狼:(2.233602,13.554519)
兔子:(0.000000,17.700000) 狼:(2.136933,13.729605)
兔子:(0.000000,17.800000) 狼:(2.042147,13.905717)
兔子:(0.000000,17.900000) 狼:(1.949264,14.082841)
兔子:(0.000000,18.000000) 狼:(1.858306,14.260960)
兔子:(0.000000,18.100000) 狼:(1.769293,14.440060)
兔子:(0.000000,18.200000) 狼:(1.682246,14.620124)
兔子:(0.000000,18.300000) 狼:(1.597186,14.801134)
兔子:(0.000000,18.400000) 狼:(1.514133,14.983074)
兔子:(0.000000,18.500000) 狼:(1.433107,15.165926)
兔子:(0.000000,18.600000) 狼:(1.354126,15.349671)
兔子:(0.000000,18.700000) 狼:(1.277212,15.534289)
兔子:(0.000000,18.800000) 狼:(1.202382,15.719763)
兔子:(0.000000,18.900000) 狼:(1.129656,15.906072)
兔子:(0.000000,19.000000) 狼:(1.059052,16.093195)
兔子:(0.000000,19.100000) 狼:(0.990587,16.281111)
兔子:(0.000000,19.200000) 狼:(0.924280,16.469800)
兔子:(0.000000,19.300000) 狼:(0.860147,16.659239)
兔子:(0.000000,19.400000) 狼:(0.798206,16.849405)
兔子:(0.000000,19.500000) 狼:(0.738473,17.040277)
兔子:(0.000000,19.600000) 狼:(0.680964,17.231830)
兔子:(0.000000,19.700000) 狼:(0.625694,17.424041)
兔子:(0.000000,19.800000) 狼:(0.572678,17.616887)
兔子:(0.000000,19.900000) 狼:(0.521931,17.810341)
兔子:(0.000000,20.000000) 狼:(0.473466,18.004380)
兔子:(0.000000,20.100000) 狼:(0.427297,18.198979)
兔子:(0.000000,20.200000) 狼:(0.383437,18.394110)
兔子:(0.000000,20.300000) 狼:(0.341898,18.589749)
兔子:(0.000000,20.400000) 狼:(0.302691,18.785868)
兔子:(0.000000,20.500000) 狼:(0.265829,18.982442)
兔子:(0.000000,20.600000) 狼:(0.231320,19.179442)
兔子:(0.000000,20.700000) 狼:(0.199176,19.376842)
兔子:(0.000000,20.800000) 狼:(0.169405,19.574614)
兔子:(0.000000,20.900000) 狼:(0.142017,19.772730)
兔子:(0.000000,21.000000) 狼:(0.117018,19.971161)
兔子:(0.000000,21.100000) 狼:(0.094416,20.169880)
兔子:(0.000000,21.200000) 狼:(0.074218,20.368858)
兔子:(0.000000,21.300000) 狼:(0.056429,20.568065)
兔子:(0.000000,21.400000) 狼:(0.041056,20.767473)
兔子:(0.000000,21.500000) 狼:(0.028101,20.967053)
兔子:(0.000000,21.600000) 狼:(0.017570,21.166776)
兔子:(0.000000,21.700000) 狼:(0.009466,21.366611)
兔子:(0.000000,21.800000) 狼:(0.003789,21.566531)
兔子:(0.000000,21.900000) 狼:(0.000544,21.766505)

可知狼是在22米處才能追上兔子,20米處不行。

同樣距離100個單位時,要追到66.6個單位處才能追上,60個單位處不行。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved