浪潮java笔试大题

第一题石头
求最少操作多少次使得石头升序排列
只需求出最大递增1的子序列长度,再用总长度减去子序列长度

package 浪潮编程;

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

		int num = 1, max = 1;
		int tree = in.nextInt();
		if (tree <= 100000) {
			int[] c = new int[tree];
			int[] cc = new int[tree];
			for (int i = 0; i < tree; i++) {
				c[i] = in.nextInt();
			}
			if (tree < c.length)
				System.out.println(0);
			for (int i = 0; i < tree; i++) {
				cc[i] = c[i];
				for (int j = i + 1; j < tree; j++) {
					cc[j] = c[j];
					if (cc[i] + 1 == cc[j]) {
						num += 1;
						cc[i] = cc[j];
					}
				}
				if (num > max) {
					max = num;
				}
				num = 1;
			}
			System.out.println(tree - max);
		}
	}
}

第二题被砍掉的树

兴中道是中山最美丽的道路,路中间的绿化带上种了两列漂亮的大树,这些大树分成了50行,每行两棵大树,一共100棵大树,这些大树被编上了号,编号方式如下:
1 3 5 7 ………… 95 97 99
2 4 6 8 ………… 96 98 100
再过几天奥运火炬就要在中山传递了,美丽的兴中道当然是最重要的必经之路,但是某天晚上却发生了一件令人震惊的大事–可恶的破坏分子为了破坏奥运,让中山人民丢丑,竟然偷去了这100棵大树中的一部分!
公安部门马上出动,列出了被偷去了大树的编号。现在摆在我们面前的情况是,如果火炬的旁边是空空的树坑,那是令人无法接受的,因此我们只能压缩火炬在兴中道上的传递距离,务必使火炬在连续的大树边传递,当时,我们就得找出一列最长的连续的大树供传递火炬时展现在全世界的人面前。请你编写程序解决这一难题。

输入
【输入格式】
N (表示有N棵大树被盗)
N1 N2 N3……NN (被盗大树的编号)

输出
【输出格式】
M X (表示从第M棵大树开始,共有连续的X棵大树,如果有多个解,只输出一个解即可)

样例输入
59 15 27 35 6
样例输出
8 47

import java.util.*;

public class MaxTree {
	private static int[] oddArr;
	private static int[] evenArr;

	public static int[] getLongestTreeSequence(int[] arr, List<Integer> cutTreeList) {

		PriorityQueue<Integer> queue = new PriorityQueue<>();
		for (int i = 0; i < arr.length; i++) {
			queue.add(arr[i]);
		}
		Collections.sort(cutTreeList);
		int pos = -1;
		int longest = 0;
		int i = 0;
		while (!queue.isEmpty() && i < cutTreeList.size()) {
			int cutTreeVal = cutTreeList.get(i);
			int curLen = 0;
			int curPos = queue.peek();
			while (!queue.isEmpty() && queue.peek() < cutTreeVal) {
				queue.poll();
				curLen++;
			}
			if (!queue.isEmpty() && cutTreeVal == queue.peek()) {
				queue.poll();
			}
			if (curLen > longest) {
				pos = curPos;
				longest = curLen;
			}
			i++;
		}
		if (!queue.isEmpty()) {
			if (queue.size() > longest) {
				pos = queue.peek();
				longest = queue.size();
			}
		}
		int[] ret = new int[2];
		ret[0] = pos;
		ret[1] = longest;
		return ret;
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int[] oddArr = new int[50];
		oddArr[0] = 1;
		for (int i = 2; i <= 50; i++) {
			oddArr[i - 1] = 2 * i - 1;
		}
		int[] evenArr = new int[50];
		for (int j = 1; j <= 50; j++) {
			evenArr[j - 1] = j * 2;
		}
		while (in.hasNext()) {
			String input1 = in.nextLine();
			int N = Integer.parseInt(input1);
			String input2 = in.nextLine();
			String[] cutTrees = input2.split(" ");
			List<Integer> oddList = new ArrayList<>();
			List<Integer> evenList = new ArrayList<>();
			for (int j = 0; j < cutTrees.length; j++) {
				int temp = Integer.parseInt(cutTrees[j]);
				if ((temp & 1) == 0) {
					evenList.add(temp);
				} else {
					oddList.add(temp);
				}
			}
			int[] oddLongest = getLongestTreeSequence(oddArr, oddList);
			int[] evenLongedt = getLongestTreeSequence(evenArr, evenList);
			if (oddLongest[1] > evenLongedt[1]) {
				System.out.print(oddLongest[0] + " " + oddLongest[1]);
			} else if (oddLongest[1] < evenLongedt[1]) {
				System.out.print(evenLongedt[0] + " " + evenLongedt[1]);
			} else {
				if (evenLongedt[0] < oddLongest[0]) {
					System.out.print(evenLongedt[0] + " " + evenLongedt[1]);

				} else {
					System.out.print(oddLongest[0] + " " + oddLongest[1]);
				}
			}
		}
	}
}

你可能感兴趣的:(笔试题,Java,找工作)