牛客2018java模拟编程题

 

计算把钱全付房租能过几天,把已有的水果换成钱计算买水果和付房租能撑过几天,取小的那个即可。

Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {// 注意while处理多个case
    long x = in.nextLong();
    long f = in.nextLong();
    long d = in.nextLong();
    long p = in.nextLong();
    
    long res = (d+f*p)/(x+p);//把已有的水果转换成钱计算能撑过几天
    System.out.println(Math.min(res, d/x));//d除以x是把钱全付房租能撑过的天数
}
  •  

两个三盒的以及三个两盒的合并,最多能剩下一个三盒与两个两盒,按照需要的一盒数量从小到大筛选

一盒数量 两盒数量 三盒数量 撑过天数
5 2 1 2
1 1 1 1
2 2 0 1
3 0 1 1
4 1 0 1
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
while(T!=0){
int N = in.nextInt();
int A1 = in.nextInt();
int A2 = in.nextInt();
int A3 = in.nextInt();

int max = A3/2+A2/3;
A3=A3%2;
A2=A2%3;
if(A3==1&&A2==2&&A1>=5){//按照表格的顺序判断
    max+=2;
    A1-=5;
}else if (A3==1&&A2>=1&&A1>=1) {
    max++;
    A1-=1;
}else if (A3==0&&A2==2&&A1>=2) {
    max++;
    A1-=2;
}else if(A3==1&&A1>=3){
    max++;
    A1-=3;
}else if (A2==1&&A1>=4) {
    max++;
    A1-=4;
}
max+=A1/6;
System.out.println(max>=N?"Yes":"No");

T--;
  •  

第三题时间不够了用邻接矩阵写了一发最暴力的dfs,过了30%,可能用邻接表能ac或者得用别的方法做吧。

static int [][] road;
static int min;
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    while (in.hasNextInt()) {//注意while处理多个case
        int n = in.nextInt();
        int M = in.nextInt();
        int S = in.nextInt();
        int T = in.nextInt();
        road = new int[n+1][n+1];
        for(int i = 0;i<M;i++){
            int s = in.nextInt();
            int t = in.nextInt();
            int D = in.nextInt();
            road[s][t] = D;
        }
        min = Integer.MAX_VALUE;
        dfs(S, T, 0);
        int res = min;
        min = Integer.MAX_VALUE;
        dfs(T, S, 0);
        res+=min;
        System.out.println(res);
    }
}
static void dfs(int s,int t,int d){
    if(s==t){
        min = Math.min(min, d);
        return;
    }
    int temp;
    for(int i =0;i<road.length;i++){
        if((temp=road[s][i])!=0){
            road[s][i] = 0;
            dfs(i, t, d+temp);
            road[s][i] = temp;
        }
    }
}

题目应该都不是特别难,但是第二题卡了挺久的,比较考验排查问题的能力。第三题等可以再次提交了再来更新一波。

----------------------------------------------------------------

2018年5月26日 00:58:11 更新:第三题用dijkstra过80%样例依旧超时(https://www.cnblogs.com/zzzdp/p/9091372.html)

posted @ 2018-05-23 21:51  zhangdapao  阅读(477)  评论(0编辑  收藏  举报