2018远景能源笔试

来源:互联网 发布:仿真软件multisim教程 编辑:程序博客网 时间:2024/04/29 21:27

一、根据输入的的序列判断是插入排序还是堆排序,然后输出对应的序列。

package 面试算法题;/** * According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?Input Specification:Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, NN integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.Output Specification:For each test case, print in the first line either "Insertion Sort" or "Heap Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.Sample Input 1:103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0Sample Output 1:Insertion Sort1 2 3 5 7 8 9 4 6 0 * */import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class 远景能源_1 {static int[] insource;static int[] result;public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(br.readLine());// 读取数据的第一个行,就一个元素n,表示接下来数组的元素的个数String[] str1 = br.readLine().split(" ");// 第二行元素String[] str2 = br.readLine().split(" ");// 第三行元素insource = new int[n];result = new int[n];// 将字符串数组中对应的元素解析添加到整数数组中for (int i = 0; i < n; i++) {int j = Integer.parseInt(str1[i]);insource[i] = j;j = Integer.parseInt(str2[i]);result[i] = j;}// 接下来根据输入的数据判断数据出现的规律,若是堆排序则修改isInsert这个变量int i = 1;boolean isInsert = true;for (; i < n; i++) {if (result[i] < result[i - 1]) {break;}}for (int j = i; j < n; j++) {if (insource[j] != result[j]) {isInsert = false;break;}}// 根据isInsert来判断利用那种排序方式来进行排序if (isInsert) {System.out.println("Insertion Sort");insertsort(i);// 利用插入排序,排序剩余的元素} else {System.out.println("Heap Sort");heapsort(n);// 利用堆排序排序剩余的元素}// 接下来输出剩余的元素for (i = 0; i < n - 1; i++) {System.out.print(result[i] + " ");}System.out.print(result[n - 1]);}private static void heapsort(int n) {int i = n - 1;for (; i > 0; i--) {if (result[i] < result[0]) {break;}}exchanged(0, i);int temp = 0;while (temp < i) {int j = temp * 2 + 1;if (j >= i)break;if (result[j] < result[j + 1] && (j + 1) < i)j++;if (result[temp] < result[j]) {exchanged(temp, j);temp = j;} else {break;}}}private static void insertsort(int n) {for (int i = n; i > 0; i--) {if (result[i] < result[i - 1]) {exchanged(i, i - 1);} else {break;}}}private static void exchanged(int i, int j) {int temp = result[i];result[i] = result[j];result[j] = temp;}}

二、根据输入的两个字符串然后输出最长公共子串,如果有多个则输出第一个首先出现的字符串。

package 面试算法题;import java.util.Scanner;/** * 题目描述:查找两个字符串a,b中的最长公共子串。若有多个,输出在较短串中最先出现的那个。  * 输入描述: 输入两个字符串  * 输出描述: 返回重复出现的字符 * 输入例子: abcdefghijklmnop  *    abcsafjklmnopqrstuvw  * 输出例子: jklmnop */public class 远景能源_2 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {String s1 = sc.nextLine();String s2 = sc.nextLine();String max = s1.length() >= s2.length()?s1:s2;String min = s1.length() >= s2.length()?s2:s1;int l = 0;String string = "";for(int i=0; i<min.length(); i++){for(int j=i+1; j<min.length(); j++){//本质上就是遍历min串的所有子串,然后找出最大的子串if(max.contains(min.substring(i, j)) && j-i > l){l = j-i;string = min.substring(i,j);}}}System.out.println(string);}}}

原创粉丝点击