被砍两树之间剩余树的最大数量

本着尊重某公司的态度,这里只为记录自己跳过的坑,谢谢该公司给我第一次笔试的机会,怪自己没有好好珍惜。

昨天晚上做了某公司笔试,虽然做的不好,但是也不是很受挫,毕竟自己还是有些思路的,但是找个外部原因吧:因为用的不是自己的电脑,所以编程题写的很乱,还有很多错误,导致无法通过编译,我太难了,好不容易有个笔试啊,所以今天整理了一下,先上一道题:

题目:

//两排树,编号分别为:1/3/5......99   和   2/4/6......100
//砍掉n棵,并给出砍掉的序号,求最多连续棵数,并给出连续序号的第一个序号
输入:n  及 被砍掉的n棵数的序号
输出:m(剩余树最多连续棵数的起始序号)  及  剩余树最多连续棵数


import java.util.Arrays;
import java.util.Scanner;

public class TreeSort {
    public static void main(String[] args) {
        //首先输入被砍棵数
        System.out.println("请输入被砍树的棵数:");
        Scanner sc1 = new Scanner(System.in);
        int num;
        while (!sc1.hasNextInt()) {
            System.out.println("输入错误,请重新输入");
            sc1 = new Scanner(System.in);
        }
        num = sc1.nextInt();

        //输入被砍树的序号
        System.out.println("请输入被砍树的序号:");
        Scanner sc2 = new Scanner(System.in);
        int[] se = new int[num];
        try {
            if (sc2.hasNextInt()) {
                for (int i = 0; i < num; i++) {
                    int temp = sc2.nextInt();
                    se[i] = temp;
                }
            }
        } catch (Exception e) {
            System.out.println("输入错误,请重新输入");
            sc2 = new Scanner(System.in);
        }
        //由小到大排序,方便计算中间树的棵数
        Arrays.sort(se);
        //System.out.println(Arrays.toString(se));
        //将奇数和偶数分开保存,如果没有第一棵和最后一棵树的序号,则加上
        int[] even = new int[num + 2];
        int[] odd = new int[num + 2];
        //计算奇数和偶数的个数
        int count_e = 0;
        int count_o = 0;
        for(int i = 0; i < num; i++){
            if(se[i] % 2 == 0){
                even[count_e] = se[i];
                count_e++;
            }else{
                odd[count_o] = se[i];
                count_o++;
            }
        }
        //System.out.println(count_e + " " + count_o);

        //将最后一颗树和第一棵树的序号加上,此处应该做一个标记,
        // 如果第一棵树和最后一棵树的序号是加上的,那么其与最近的被砍树之间的数量应该加1;
        //此处设置标记
        boolean e_F = false;
        boolean e_L = false;
        boolean o_F = false;
        boolean o_L = false;
        if(even[count_e - 1] != 100 ){
            e_L = true;
            even[count_e ] = 100;
            count_e += 1;
        }
        if(even[0] != 0){
            e_F = true;
            even[count_e] = 0;
            count_e += 1;
        }
        //System.out.println(count_e);
        //因为数组长度本身比较大,所以只截取前边有意义的数
        int[] new_even = new int[count_e];
        System.arraycopy(even, 0, new_even, 0, count_e);
        Arrays.sort(new_even);
        System.out.println(Arrays.toString(new_even));
        if(odd[count_o - 1] != 99){
            o_L = true;
            odd[count_o ] = 99;
            count_o += 1;
        }
        if(odd[0] != 1){
            o_F = true;
            odd[count_o] = 1;
            count_o += 1;
        }
        //System.out.println(count_o);
        int[] new_odd = new int[count_o];
        System.arraycopy(odd, 0, new_odd, 0, count_o);
        Arrays.sort(new_odd);
        System.out.println(Arrays.toString(new_odd));
        TreeSort res_even = new TreeSort();
        TreeSort res_odd = new TreeSort();
        if(res_even.result_d(count_e, new_even, e_F, e_L)[1] > res_odd.result_d(count_o, new_odd, o_F, o_L)[1]){
            System.out.println(Arrays.toString(res_even.result_d(count_e, new_even, e_F, e_L)));
        }else{
            System.out.println(Arrays.toString(res_odd.result_d(count_o, new_odd, o_F, o_L)));
        }
    }

    //计算被砍两树之间树的数量
    //例如3/9:count = (9-3)/2 - 1;

    private int[] result_d(int count, int[] arr, boolean F, boolean L){
        int[] res = new int[2];
        //存储中间树的棵树
        int[] number = new int[count - 1];
        //记录最大值
        int max = 0;
        //记录从最大值的起始序号指针
        int sequence_n = 0;
        for(int i = 0; i < count - 1; i++){
            number[i] = (arr[i+1] - arr[i])/2-1;
        }
        if(F){
            number[0] += 1;
        }
        if(L){
            number[count-2] +=1;
        }
        for(int i = 0; i < count-1; i++){
            if(number[i] > max){
                max = number[i];
                sequence_n = i;
            }
        }
        int sequence = arr[sequence_n];

        res[0] = sequence;
        if(sequence != number[0]){
            res[0] += 1;
        }
        res[1] = max;
        return res;
    }
}

作为一个初学者,感觉自己的想法很简单,思路过于简单,导致代码冗余

希望随着自己的进步,可以简化代码

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

localhost1212

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值