本着尊重某公司的态度,这里只为记录自己跳过的坑,谢谢该公司给我第一次笔试的机会,怪自己没有好好珍惜。
昨天晚上做了某公司笔试,虽然做的不好,但是也不是很受挫,毕竟自己还是有些思路的,但是找个外部原因吧:因为用的不是自己的电脑,所以编程题写的很乱,还有很多错误,导致无法通过编译,我太难了,好不容易有个笔试啊,所以今天整理了一下,先上一道题:
题目:
//两排树,编号分别为: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;
}
}
作为一个初学者,感觉自己的想法很简单,思路过于简单,导致代码冗余
希望随着自己的进步,可以简化代码