骆驼与胡萝卜
题
快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题
目java解[资料]
问题提出:一个商人骑一头驴要穿越1000公里长的沙漠,去卖3000根胡萝卜。已知驴一次性可驮1000根胡萝卜,但每走1公里又要吃掉1根胡萝卜。问:商人最多可卖出多少胡萝卜,
问题分析:骆驼,胡萝卜都可以看做对象,这个问题用面向对象
方法
快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载
解决。
代码有3个类,Client类中包含main方法,Carrot胡萝卜类,Camel骆驼类
Clint.java
1.引入相关包及类说明
import java.util.ArrayList;
import java.util.List;
/**
* 问题描述:一个商人骑一头驴要穿越1000公里长的沙漠,去卖3000根胡萝卜。
* 已知驴一次性可驮1000根胡萝卜,但每走1公里又要吃掉1根胡萝卜。
* 问:商人最多可卖出多少胡萝卜,
* @author 姚云良 superyyl@hotmail.com
* @see Carrot
* @see Camel
* @version 1.0
*/
2.声明变量
public class Client {
public static final int STEP = 1; //设置骆驼每步行进多少米
public static final int MAX_CARRY = 1000; //设置骆驼每次最多运多少根胡萝卜
public static final int INIT_CARROT_NUM = 3000; //设置起点胡萝卜数量
public static final int DISTANCE = 1000; //设置路程总长度
List
carrots = new ArrayList();
Camel camel;
3.主函数
/**
* 主函数 生成对象 调用初始化方法 模拟行进路线
* @param args
*/
public static void main(String[] args) {
Client cc = new Client();
cc.init();
while (cc.camel.pos < DISTANCE) {
cc.CarryOnce();
}
System.out.println(cc);
System.out.println(cc.camel);
}
4.单次模拟搬运
/**
* 单次搬运模拟
* 如果剩余胡萝卜大于最大搬运量,则分次搬运
* 否则一口气搬完
*
* 如果骆驼后方的胡萝卜数量小于步长*2,则骆驼会在返程路上没胡萝卜吃,所以抛
弃
* 否则需返程搬运剩余胡萝卜
*/
public void CarryOnce() {
int carryNum = 0;
if (getCarrotsPosNum(carrots, camel.pos) >= MAX_CARRY) {
carryNum = MAX_CARRY;
} else {
carryNum = getCarrotsPosNum(carrots, camel.pos);
}
camel.pick(carryNum);
camel.move(STEP);
camel.place();
if (getCarrotsPreNum(carrots, camel.pos) >= 2 * STEP) {
camel.pick(STEP);
camel.move(-STEP);
camel.place();
}
}
5.初始化
/**
* 初始化胡萝卜数量,并使Camel对象持有前台引用
*/
public void init() {
for (int i = 0; i < INIT_CARROT_NUM; i++) {
carrots.add(new Carrot());
}
camel = new Camel(this);
System.out.println("初始化完成:\r\n\t" +
"路程总长度:"+DISTANCE+"米\r\n\t"+
"胡萝卜初始数:"+INIT_CARROT_NUM+"根\r\n\t"+
"骆驼最大负载:"+MAX_CARRY+"根\r\n\t"+
"每次行进步长:"+STEP+"米\r\n");
}
6.一些辅助方法,得到各种胡萝卜数量
/**
* 在骆驼的位置,返回胡萝卜根数
* @param carrots 胡萝卜泛型
* @param pos 骆驼所在坐标
* @return 骆驼所在坐标的胡萝卜根数
*/
public static int getCarrotsPosNum(List carrots, int pos)
{
int posNum = 0;
for (Carrot c : carrots) {
if (c.pos == pos) {
posNum++;
}
}
return posNum;
}
/**
* 在骆驼的身后,返回胡萝卜根数
* @param carrots 胡萝卜泛型
* @param pos 骆驼所在坐标
* @return 骆驼所在坐标后方的胡萝卜根数
*/
public static int getCarrotsPreNum(List carrots, int pos)
{
int preNum = 0;
for (Carrot c : carrots) {
if (c.pos < pos) {
preNum++;
}
}
return preNum;
}
7.重写toString()方法,方便打印相关信息
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
String str = "";
int[] posNum = new int[DISTANCE + 1];
for (int i = 0; i <= DISTANCE; i++) {
posNum[i] = 0;
}
for (Carrot c : carrots) {
posNum[c.pos]++;
}
for (int i = 0; i <= DISTANCE; i++) {
if (posNum[i] != 0) {
str += "有" + posNum[i] + "根胡萝卜在坐标" + i + "处\r\n";
}
}
return str;
}
}
Carrot.java
胡萝卜类很简单,就一个pos坐标属性
/**
* 胡萝卜类
* @author 姚云良 superyyl@hotmail.com
* @see Client
* @version 1.0
*/
public class Carrot {
int pos;
/**
* 构造方法 初始化胡萝卜在坐标0的位置
*/
public Carrot() {
pos = 0;
}
}
Camel.java 1.引入相关包及类说明 import java.util.ArrayList; import java.util.List;
/**
* 骆驼类
* @author 姚云良 superyyl@hotmail.com
* @see Client
* @version 1.0
*/
2.声明变量
public class Camel {
Client client;
int pos;
int len;
int absLen;
List carrots = new ArrayList();
3.骆驼类的构造方法
/**
* 构造方法 用Client生成骆驼对象
* @param client 持有客户端引用
*/
public Camel(Client client) {
this.client = client;
this.pos = 0;
}
4.背起胡萝卜
/**
* 背胡萝卜
* @param num 背起胡萝卜的个数
*/
public void pick(int num) {
for (int i = client.carrots.size() - 1; i >= 0; i--) {
if (this.pos == client.carrots.get(i).pos) {
this.carrots.add(client.carrots.get(i));
client.carrots.remove(i);
num--;
}
if (num <= 0)
break;
}
}
5.移动
/**
* 移动,正数为移动步长,负数则为返回
* @param len 移动的距离
*/
public void move(int len) {
if (len > 0)
absLen = len;
else
absLen = carrots.size();
for (Carrot c : carrots) {
c.pos += len;
}
for (int i = 0; i < absLen; i++) {
carrots.remove(0);
}
this.pos += len;
}
6.放下所有胡萝卜
/**
* 放下骆驼身上所有胡萝卜
*/
public void place() {
client.carrots.addAll(carrots);
carrots.removeAll(carrots);
}
7.重写toString()方法
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
String str = "";
boolean camelEmpty = true;
int[] posNum = new int[Client.MAX_CARRY + 1];
for (int i = 0; i <= Client.MAX_CARRY; i++) {
posNum[i] = 0;
}
for (Carrot c : carrots) {
posNum[c.pos]++;
}
for (int i = 0; i <= Client.MAX_CARRY; i++) {
if (posNum[i] != 0) {
str += "(骆驼)有" + posNum[i] + "根胡萝卜在坐标" + i + "
处\r\n";
camelEmpty = false;
}
}
if (camelEmpty) {
str = "(骆驼)正在坐标" + pos + "处休息";
}
return str;
}
}
结果:步进为1
由于在533处有1根胡萝卜,而骆驼在534处,此时回去搬运1根胡萝卜显然不合算,所以
算法抛弃了这根胡萝卜