浪潮java笔试大题

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

浪潮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]);
}
}
}
}
}。

相关文档
最新文档