兔子和蘿卜問題

題目:

有100個(gè)蘿卜,一只兔子邊背邊吃,走一米吃一個(gè),走到家剩下多少蘿卜,(到家一共50米,兔子每次只能背50個(gè))。

假設(shè):

兔子行動(dòng)距離是離散的,即最小行動(dòng)單位是1米。

代碼實(shí)現(xiàn):

package CarrotAndRabbit;

public class CarrotAndRabbitForDiscrete {

privateintcarrots= 0;

privateintdistance= 0;

privateintcapacity= 0;

privateintcarrots_rest= 0;

privateintdistance_rest= 0;

public CarrotAndRabbitForDiscrete(intcarrots,intdistance,intcapacity) {

this.carrots=carrots;

this.distance=distance;

this.carrots_rest=carrots;

this.distance_rest=distance;

this.capacity=capacity;

}

public int results() {

while(distance_rest> 0){

this.forwardOneMeter();

}

return this.carrots_rest;

}

public void conditions(){

System.out.println("Distance: "+distance+" ,Carrots: "+carrots);

}

private void forwardOneMeter() {

intmax_num= 0;

intdiscard_carrots= 0;

if(carrots_rest%capacity> 2){

max_num=carrots_rest/capacity+ 1;

}else{

max_num=carrots_rest/capacity;

discard_carrots=carrots_rest%capacity;

}

this.carrots_rest-=discard_carrots;

if(max_num< 1) {

this.carrots_rest= 0;

this.distance_rest= 0;

}elseif(max_num== 1) {

this.carrots_rest--;

this.distance_rest--;

}else{

this.carrots_rest=this.carrots_rest- 1 - (max_num- 1) * 2;

this.distance_rest--;

}

//System.out.println(carrots_rest + ":" + distance_rest);

}

public static void main(String...strings) {

CarrotAndRabbitForDiscretecarrotAndRabbit=newCarrotAndRabbitForDiscrete(100, 50, 50);

System.out.println(carrotAndRabbit.results());

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子...
    趙宇_阿特奇閱讀 2,077評(píng)論 0 2
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,740評(píng)論 18 399
  • 【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長(zhǎng)到第三個(gè)月后每個(gè)月又生一對(duì)兔...
    葉總韓閱讀 5,227評(píng)論 0 41
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,628評(píng)論 19 139
  • 我們?cè)谝黄?,僅僅是因?yàn)槲铱鞓纺阋部鞓?,如果有一天,你或我沒有這份快樂了,請(qǐng)免除那些無聊的猜忌、爭(zhēng)吵。好聚;好散。
    乍暖還涼0123閱讀 173評(píng)論 0 0

友情鏈接更多精彩內(nèi)容