java编程题

一个农场有100头牛,性别年龄随机数,母牛每三年生一头小牛 年龄为1性别随机数,牛只能活16岁,找出随机一头牛的母亲,兄弟姐妹,父亲,20年之后有多少头牛?(一个sb公司提出来的这问题)。

(第一种做法)首先定义一个类

package org.ssm.web.adc;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class Cow {
private Integer id;


public void setId(Integer id) {
this.id = id;
}


private List<Map<Integer, Integer>> coupleList = new ArrayList<>();//cp历史


private Integer sex;


public Integer getMaleId() {
return maleId;
}


public Integer getFemaleId() {
return femaleId;
}


private Integer age;

private static int SPED = 1;


public static int getSPED() {
return SPED;
}


public static void setSPED(int sPED) {
SPED = sPED;
}


private Integer maleId; // 父id

private Integer femaleId;//母id


public void setMaleId(Integer maleId) {
this.maleId = maleId;
}


public void setFemaleId(Integer femaleId) {
this.femaleId = femaleId;
}


int sex1 = (int) (Math.random() * 2);

public Cow() {
super();
}


public Cow(int id, int age, int sex) {
super();
this.id = id;
this.age = age;
this.sex = sex;
}


public Cow(int age, int sex) {
super();
this.age = age;
this.sex = sex;
id = SPED ++ ;
}



public Integer getId() {
return id;
}


public Cow(int age) {
this.age = age;
}


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}

public Cow produceCow() {
if (age%3==0&&sex==0) {
return new Cow(1, sex1);
}
return null;
}


public int getSex() {
return sex;
}


public void setSex(int sex) {
this.sex = sex;
}


@Override
public String toString() {
return "Cow [id=" + id + ", coupleList=" + coupleList + ", sex=" + sex + ", age=" + age + ", maleId=" + maleId
+ ", femaleId=" + femaleId + "]";
}


public List<Map<Integer, Integer>> getCoupleList() {
return coupleList;
}


public void setCoupleList(List<Map<Integer, Integer>> coupleList) {
this.coupleList = coupleList;
}


public void setSex(Integer sex) {
this.sex = sex;
}

public void setAge(Integer age) {
this.age = age;
}

}

然后编写main方法

package org.ssm.web.adc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


public class Test {
private static List<Cow> cows;//农场的奶牛群集合 
 static{
 cows = new ArrayList<>();
 for (int i = 0; i < 100; i++) {
 int sex =(int) (Math.random()*2);
 int age =(int) (Math.random()*10);
     Cow cow = new Cow((i+1),age,sex);  
     cows.add(cow);
 }
   }
 
 public static Cow getMale(){
 Cow temp=cows.get(new Random().nextInt(cows.size()));
 while(temp.getSex()==0){
 temp=getMale();
 if(temp.getSex()==1){
 break;
 }
 }
 return temp;
 }
 
 public static List<Cow> getCows(int year){  
       for(int i=0;i<year;i++){ //奶牛场year年的变化,从第0年到第year年(共year年)  
           List<Cow> tempCows = new ArrayList<>();//新一年变化,统计农场奶牛数量时,新建一个临时奶牛圈,圈住已有的奶牛和新生的奶牛  
           for(Cow cow:cows){ //遍历已有奶牛圈
            if (cow.getAge()<15) {
            tempCows.add(cow);//将原奶牛加入新奶牛圈
}
               cow.setAge(cow.getAge()+1);//原奶牛年龄+1
               Cow smalCow = cow.produceCow();//原奶牛自己判断自己年龄,如果到了生育期,自动生一头小牛,没到3的倍数,返回null  
               if(smalCow!=null){
               //获取牛的父亲
               Cow temp=getMale();
               Map<Integer, Integer> map=new HashMap<>();
               map.put(cow.getId(), temp.getId());
               smalCow.getCoupleList().add(map);
               if(temp.produceCow()==null){
               smalCow.setMaleId(temp.getId());
               }
               //把cp记录存入list中要想获取他的兄弟
               //姐妹可以循环现在的奶牛圈判断父id和母id是cp记录中存储的,前面的元素都是它的哥哥或者姐姐后面的都是它的弟弟或者妹妹
               //如果要是空的则说明并不是通过cp生的
               smalCow.setFemaleId(cow.getId());
                   tempCows.add(smalCow);//将新生的小奶牛加入新奶牛圈 
                   if(smalCow.getFemaleId()==cow.getId()){
                   System.out.println("母id:"+smalCow.getFemaleId()+"\t 父id:"+smalCow.getMaleId()+"  id:"+smalCow.getId()+" "+ smalCow.getSex()); //找出母id
                   }
               }
           }
           cows = tempCows;//统计所有奶牛结束,将临时奶牛圈中的所有奶牛赶入农场原有奶牛圈 
//            System.out.printf("第%d年奶牛数量:%d%n",i+1,cows.size());  
           tempCows=null;//撤掉临时奶牛圈
       }  
       return cows;//返回第year年后农场的奶牛群 
   } 
 
 public static Cow getRandom(){
 Cow c=cows.get(new Random().nextInt(cows.size()));
 while(c.getMaleId()==null){
 c=getRandom();
 if(c.getMaleId()!=null){
 break;
 }
 }
 return c;
 }
 
//一个农场有100头牛,性别年龄随机数,母牛每三年生一头小牛 年龄为1性别随机数,牛只能活16岁,找出随机一头牛的母亲,兄弟姐妹,父亲,20年之后有多少头牛?
public static void main(String[] args ) {
int year=20;
Test.getCows(year);
System.out.println(year+"年后一共"+cows.size()+"牛");
//随机获取一个牛获取它的父母和兄弟姐妹
Cow c=getRandom();
Cow temp=null;
String sex=c.getSex()==1 ? "男" :"女";
System.out.println("牛"+c.getId()+"被成功选中,以下是被选中牛的基本信息");
System.out.println("----------------------------------------------------------------------------");
System.out.println("牛"+c.getId()+"\t性别"+sex+"\t牛父亲"+c.getMaleId()+"\t牛母亲"+c.getFemaleId());
System.out.println("----------------------------------------------------------------------------");
//首先获取当前牛的母亲
Integer m=c.getFemaleId();
//声明一个集合用来存储是被选中牛同胞的牛
List<Cow> family=new ArrayList<>();
for (Cow cow : cows) {
if(cow.getId()==c.getId())
continue;
//循环判断集合中每个牛的父母是否和选中牛的一样如果一样就加入集合
for (Map<Integer, Integer> map :cow.getCoupleList() ) {
if(map.containsKey(m)){
family.add(cow);
}
}
}
if(family.size()==0)
System.out.println("没有同胞");
for (Cow c1 : family) {
String sex1=c1.getSex()==1 ? "男" :"女";
System.out.println("牛"+c1.getId()+"\t性别"+sex1+"\t牛父亲"+c1.getMaleId()+"\t牛母亲"+c1.getFemaleId());
}
}
}


第二中做法

package com.zhongji.cn;
import java.util.ArrayList;
import java.util.List;
public class Cattle {
public String id;
public int age;
public boolean sex;
public Cattle mother;//母亲
public Cattle father;//父亲
public List<Cattle> children=new ArrayList<Cattle>();//孩子

private static int serialId=0;//序号
private final static int bornCycle = 3;//生育周期
private final static int maxAge = 16;//最大年龄
private final static boolean MALE = true;
private final static boolean FEMALE = false;

// 新生牛
public Cattle() {
this.id=getSerialId();
this.setAge(0);
this.setSex(Math.random() > 0.5);
}
// 新生牛,父亲,母亲
public Cattle(Cattle father,Cattle mother) {
this.id=getSerialId();
this.setAge(1);
this.setSex(Math.random() > 0.5);
this.mother=mother;
this.father=father;
mother.children.add(this);
father.children.add(this);
}
public void growOneYear() {
this.age++;
}
public boolean alive() {
return this.age < Cattle.maxAge;
}
public boolean notDeading() {
return this.age < Cattle.maxAge-1;
}
public boolean borning() {
return this.sex == FEMALE && this.age % Cattle.bornCycle == 0;
}
private String getSerialId(){
return ""+serialId++;
}
public Cattle getFather() {
return father;
}
public void setFather(Cattle father) {
this.father = father;
}
public Cattle getMother() {
return mother;
}
public void setMother(Cattle mother) {
this.mother = mother;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public String toString(){
return this.id+"";
}
}


package com.zhongji.cn;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TestCattle {
public static void main(String args[]) {
Map<String,Cattle> cattles = new HashMap<String,Cattle>();
// 初始化牛群 100头牛
for (int i = 0; i < 100; i++) {
Cattle c = new Cattle();
cattles.put(c.id,c);
}
//20年
for (int i = 0; i < 20; i++) {
// 活着的公牛 建立集合
List<Cattle> GongNiu = new ArrayList<Cattle>();
// 活着的母牛,且到达产牛年份
List<Cattle> MuNiu = new ArrayList<Cattle>();
for (String id : cattles.keySet()) {
//遍历这个key 一个个的;
Cattle cattle=cattles.get(id);
// 涨一岁
cattle.growOneYear();
// 活着,且到达产牛年份母牛
if (cattle.alive() && cattle.borning()) {
//添加到母牛的集合里
MuNiu.add(cattle);
}//活着的公牛
else if (cattle.alive() && cattle.isSex()) {
//添加到活着的公牛集合里去
GongNiu.add(cattle);
}
}
//能够配对生牛的数目,取最小值
int pairCount = Math.min(MuNiu.size(), GongNiu.size());
for (int j = 0; j < pairCount; j++) {
//建立新生牛的对象
Cattle newCattle = new Cattle(GongNiu.get(j), MuNiu.get(j));
//将新生牛放到map集合中
cattles.put(newCattle.id,newCattle);//新生小牛
}
}
//计数器
int sum = 0;
for (int j = 0; j < cattles.size(); j++) {
//遍历集合,挨个取 活着就加1
if (cattles.get(j).alive()){
sum++;
}
}
System.out.println("20 年后一共有" + sum + "头牛存活");
//设下标
int index = (int) (Math.random() * sum);
//当牛的母亲是没有的时候做一个判断
if (cattles.get(index).mother == null) {
System.out.println("NO." + cattles.get(index).id+ " 没有母亲");

index = (int) (Math.random() * sum);
}
//有的话,就输出
Cattle c = cattles.get(index);
System.out.println( c.id + " 牛的母亲为:NO." + c.mother.id+ " 牛的父亲为:NO." + c.father.id);
//遍历的孩子的只要满足mother不为空且mother的id一样
for (Cattle cattle : c.mother.children) {
if(cattle.mother!=null&&cattle.mother.id==c.mother.id){
//当牛的id是一样的话 说明是同一个牛,不输出
if(cattle.id==c.id){
System.out.print("");
}
//判断公母,如果牛的性别和是true就是兄弟
if(cattle.sex){
// System.out.println(cattle.sex);
//输出
System.out.println("NO."+c.id+" 牛的兄弟是"+cattle.id);
}
else{//如果性别是false就是姐妹
System.out.println("NO."+c.id+" 牛的姐妹"+cattle.id);
}
}
}
}
}
























猜你喜欢

转载自blog.csdn.net/qq_31261105/article/details/53431171