5月场Java模拟考编程题

1. 排序子序列
代码解析 : 使用一个变量来表示当前递增或递减, 遍历一遍数组即可
import java.util.Scanner;

/**
 * Created by zbyte on 17-5-19.
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int f = -1; // 标志: -1开始, 1递增, 2递减
        int last = sc.nextInt();
        int res = 0;
        for (int i = 1; i < n; i++) {
            int now = sc.nextInt();
            if (f == -1) {
                if (now > last) {
                    f = 1;
                } else if (now < last) {
                    f = 2;
                }
            } else if (f == 1) {
                if (now < last) {
                    f = -1;
                    res++;
                }
            } else {
                if (now > last) {
                    f = -1;
                    res++;
                }
            }
            last = now;
        }
        res++;

        System.out.println(res);
    }
}

2. 组队竞赛
代码解析 : 将数组排序, 如例子n=2, 数组a排序后为1 2 5 5 5 8, 则对数组下标为n, n+2, ... , n*3-2的值求和即为答案(贪心)
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by zbyte on 17-5-19.
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] a = new int[n*3];
        for (int i = 0; i < n*3; i++) {
            a[i] = sc.nextInt();
        }
        Arrays.sort(a);
        long res = 0;
        for (int i = n; i < n*3; i += 2) {
            res += a[i];
        }

        System.out.println(res);
    }
}

3. 训练部队
代码解析 : 先对数组排序, 按战斗力排, 战斗力相等后按潜力值排. 之后找到x+y最大值的位置, 然后倒序往回对y-x>0的值求和, 得到最终结果. cha值的意义: 当x1=6,y1=8; x2=7; 通过cha值可以从战斗力为7开始往回遍历, 得到最终结果15
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * Created by zbyte on 17-5-19.
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        Entity[] a = new Entity[n];
        for (int i = 0; i < n; i++) {
            a[i] = new Entity(sc.nextInt(), sc.nextInt());
        }

        Arrays.sort(a, new Comparator<Entity>() {
            @Override
            public int compare(Entity o1, Entity o2) {
                return o1.x-o2.x!=0 ? o1.x-o2.x : o1.y-o2.y;
            }
        });

        int max = 0; //x+y的最大值
        int index = 0; // 下标
        int cha = 0; // 差值
        for (int i = 0; i < n; i++) {
            if (a[i].x + a[i].y >= max || a[i].x + a[i].y + cha >= max) {
                max = a[i].x + a[i].y;
                cha = a[i].y - a[i].x;
                index = i;
            }
        }
        long res = max;
        for (int i = index-1; i >= 0 ; i--) {
            if (a[i].y > a[i].x) {
                res += a[i].y - a[i].x;
            }
        }

        System.out.println(res);
    }

    static class Entity {
        int x;
        int y;
        public Entity(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}


全部评论
请问能说一下最后一题的思路吗?
点赞 回复 分享
发布于 2017-05-21 17:41
厉害了
点赞 回复 分享
发布于 2017-05-20 11:15
第二题居然要用long。。。巨亏调了好久
点赞 回复 分享
发布于 2017-05-20 01:20
差不多都想到了。。。唉不过都没ac全。。。
点赞 回复 分享
发布于 2017-05-20 01:19
点赞 回复 分享
发布于 2017-05-19 22:32

相关推荐

06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
6
20
分享

创作者周榜

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