字节跳动笔试,奶牛怎么死

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test_1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        test_1 t = new test_1();
        System.out.println(t.getNum(n));
    }
    
    public  int getNum(int n){  List<Build> list1=new ArrayList<Build>();//母牛数  List<Build> list2=new ArrayList<Build>();//小牛数  List<Build> list3=new ArrayList<Build>();//死亡的牛数  Build build = new Build(0);//母牛  list1.add(build);      for(int i = 0; i < n ; i++){  list2.clear();  for(int j = 0;j<list1.size();j++ ){  Build build2 = list1.get(j);  build2.setSage(build2.getSage()+1);//涨一岁  if(build2.ready()){//如果>3  list1.add(new Build(0));//增加1个小牛  }  if(build2.die()){//如果>10  list3.add(new Build(3));//list3增加一个牛(list的牛都可生小牛)  }    }    list1.addAll(list2);  //list1.remove(list3);  }  return list1.size()-list3.size();
}   class Build{   private final int age = 3;//从3岁开始   private final int mage = 7;//从7岁不生育   private final int dage = 11;//从10岁死亡   private int sage;//增加的年龄       public int getSage() {  return sage;  }  public void setSage(int sage) {  this.sage = sage;  }  public int getBuildge() {  return age;  }      public Build(int  a){   sage = a;   }   public boolean ready(){   if(sage >= age && sage <= mage){   return true;   }   return false;   }   public boolean die(){   if(sage > dage){   return true;   }   return false;   }     }
 }


菜到极致10%...#字节跳动##笔试题目##春招##Java#
全部评论
public class Main4 {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int year = sc.nextInt();         int increase = 2;         int cowCount = 0;         // key为age,value为数量         Map<Integer, Integer> map = new TreeMap<Integer, Integer>();         // 1岁的牛只有一头,初始化值         map.put(1, 1);         while(increase <= year){             // 更新map中的key,表示年龄             Map<Integer, Integer> newCows = new TreeMap<Integer, Integer>();             // 表示存储具有生育能力的牛的数量             int canBorn = 0;             for(Entry<Integer, Integer> entry:map.entrySet()){                 if(entry.getKey() + 1 <= 10){                     newCows.put(entry.getKey() + 1, entry.getValue());                     if(entry.getKey() >= 2 && entry.getKey() <= 6){                         // 年龄在3-7岁,生一只牛                         canBorn += entry.getValue();                     }                 }             }             if(canBorn != 0){                 newCows.put(1, canBorn);             }             map = newCows;             increase++;         }                  for(Entry<Integer, Integer> entry:map.entrySet()){             cowCount += entry.getValue();         }         System.out.println(cowCount);     } }
点赞 回复 分享
发布于 2019-04-27 13:27
#include<iostream> using namespace std; int f[100000]; struct node {     int born;     int num; }p[100000]; int main() {     int n;     p[1].born=1;p[1].num=1;     p[2].born=0;p[2].num=1;     p[3].born=1;p[3].num=2;     for(int i=4;i<=10;i++){         p[i].num=p[i-1].num+p[i-2].num;         if(i>=8){//不能生育的                 p[i].num=p[i].num-p[i-7].born;                 if(i>=9)p[i].num=p[i].num-p[i-8].born;                 if(i>=10)p[i].num=p[i].num-p[i-9].born;         }         p[i].born=p[i].num-p[i-1].num;         if(i>=11)p[i].num-=p[i-10].born*2;//死亡的         //cout<<i<<" "<<p[i].born<<" "<<p[i].num<<endl;     }     for(int i=11;i<=30000;i++){         int sum=0;         for(int j=i-6;j<=i-2;j++)           sum+=p[j].born;         p[i].born=sum;         //cout<<i<<" "<<sum<<endl;       p[i].num=p[i-1].num+sum-p[i-10].born;     }     while(cin>>n)     cout<<p[n].num<<endl;     return 0; }
点赞 回复 分享
发布于 2019-04-27 12:08

相关推荐

10-02 19:29
已编辑
浙江科技大学 运营
点赞 评论 收藏
分享
10-01 09:50
门头沟学院 Java
肖先生~:这个人真的很好,点赞
点赞 评论 收藏
分享
评论
点赞
5
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务