华为od机试3道题目
题目一
1.给定一个从小到大的有序整数序列(存在正整数和负整数)和数组nums,请你在该数组中找出两个数,其和的绝对值(|nums[x] + nums[y]|)为最小值,并返回这个绝对值。每一种输入只会对应一个答案,但是,数组中同一个元素不能使用两遍。
输入描述:
两数之和绝对值最小值
输出描述:
一个通过空格分割的有序整数序列字符串,最多1000个整数,且整数数值范围是-65535~65535
举例
输入:
-3 -1 5 7 11 15
输出:
2
Java:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] strings = sc.nextLine().split(" "); int n = strings.length; int min = Integer.MAX_VALUE; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n;j++){ int a = Integer.valueOf(strings[i]); int b = Integer.valueOf(strings[j]); int count = Math.abs(a+b); min = Math.min(count, min); } } System.out.println(min); } }
题目二
2.给定一组不等式,判断是否成立并输出不等式的最大差(输出浮点数的整数部分),要求:1)不等式系数为double类型,是一个二维数组;2)不等式的变量为int类型,是一维数组;3)不等式的目标值为double类型,是一维数组;4)不等式约束为字符串数组,只能是:">", ">=", "<", "<=", "=",例如,不等式组:
最大差=
类型为整数(输出浮点数的整数部分)。
输入描述:
1)不等式组系数(double类型):
2)不等式变量(int类型):
3)不等式目标值(double类型): b1, b2, b3
4)不等式约束(字符串类型):<=, <, =, >, >=
输入:
a11,a12,a13,a14,a15;a21,a22,a23,a24,a25;a31,a32,a33,a34,a35;x1,x2,x3,x4,x5;b1,b2,b3,<=,<=,<=
输出描述:
true或者false,最大差
举例:
输入:
2.36,3,6,7.1,6;1,30,8.6,2.5,21;0.3,69,5.3,6.6,7.8;1,13,2,17,5;340,67,300.6;<=,>=,<=
输出:
false 758
Java
import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); String[] strings = line.split(";"); double[][] a = new double[strings[strings.length - 1].split(",").length][strings[0].split(",").length]; for (int i = 0; i < a.length; i++) { String[] les = strings[i].split(","); for (int j = 0; j < les.length; j++) { a[i][j] = Double.parseDouble(les[j]); } } Integer[] x = Arrays.stream(strings[a.length].split(",")).map(Integer::parseInt).collect(Collectors.toList()).toArray(new Integer[strings[0].split(",").length]); Double[] b = Arrays.stream(strings[strings.length - 2].split(",")).map(Double::parseDouble).collect(Collectors.toList()).toArray(new Double[a.length]); String[] con = Arrays.stream(strings[strings.length - 1].split(",")).collect(Collectors.toList()).toArray(new String[a.length]); boolean bool = true; double max = 0; for (int i = 0; i < con.length; i++) { boolean bo; double temp; switch (con[i]) { case ">": temp = getLeft(a[i], x) - b[i]; bo = temp > 0; break; case ">=": temp = getLeft(a[i], x) - b[i]; bo = temp >= 0; break; case "<=": temp = getLeft(a[i], x) - b[i]; bo = temp <= 0; break; case "<": temp = getLeft(a[i], x) - b[i]; bo = temp < 0; break; default: temp = getLeft(a[i], x) - b[i]; bo = temp == 0; } max = Math.max(temp, max); if (bool) { bool = bo; } } System.out.println(bool + " " + (int) max); } public static double getLeft(double[] aRight, Integer[] x) { double d = 0; for (int i = 0; i < x.length; i++) { d += aRight[i] * x[i]; } return d; } }
题目三
3.小华和小为是很要好的朋友,他们约定周末一起吃饭。通过手机交流,他们在地图上选择了多个聚餐地点(由于自然地形的原因,部分聚餐地点不可达),求小华和小为都能达到的聚餐地点有多少个?
输入描述:
第一行输入n和m,m代表的是地图的长度,n代表的是地图的宽度。第二行开始具体输入地图的信息,地图的信息包含:
0为通常的道路
1为障碍物(且仅1为障碍物)
2为小华或者小为,地图中必定有且仅有两个(非障碍物)
3为被选中的聚餐地点(非障碍物)
输出描述
可以被两方都达到的聚餐地点数量,行末无空格。
举例1:
输入:
4 4
2 1 0 3
0 1 2 1
0 3 0 0
0 0 0 0
输出
2
举例1:
输入:
4 4
2 1 2 3
0 1 0 0
0 1 0 0
0 1 0 0
输出
0
Java
import java.util.*; public class Main{ public static int endX; public static int endY; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int lenX = sc.nextInt(); int lenY = sc.nextInt(); int[][] migong = new int[lenX][lenY]; List<int[]> hw = new ArrayList<>(); List<int[]> canGuan = new ArrayList<>(); for ( int i=0; i<lenX; i++) { for ( int j=0; j<lenY; j++) { migong[i][j] = sc.nextInt(); if (migong[i][j] == 2) { int[] h = { i, j}; hw.add(h); } else if (migong[i][j] == 3) { int[] c = { i, j}; canGuan.add(c); } } } int[] xh = hw.get(0); int[] xw = hw.get(1); int res = 0; for (int i=0;i<canGuan.size();i++) { int temp[][] = copy(migong); endX = canGuan.get(i)[0]; endY = canGuan.get(i)[1]; if ( forEnd(xh[0],xh[1],temp) == 1 ) { temp = copy(migong); if ( forEnd(xw[0],xw[1],temp) == 1 ) { res++; } } } System.out.println(res); } public static int[][] copy(int[][] nums){ int x = nums.length; int y = nums[0].length; int[][] res = new int[x][y]; for ( int i=0; i<x; i++) { for ( int j=0; j<y; j++) { res[i][j] = nums[i][j]; } } return res; } public static int forEnd(int x,int y,int[][] ints){ int U = x - 1; int D = x + 1; int L = y - 1; int R = y + 1; if(x==endX && y==endY){ return 1; } if (U>=0) { if (ints[U][y] != 1) { ints[x][y] = 1; if (forEnd(U,y,ints)==1) { return 1; } } } if (D<ints.length) { if (ints[D][y] != 1) { ints[x][y] = 1; if (forEnd(D,y,ints)==1) { return 1; } } } if (L>=0) { if (ints[x][L] !=1 ) { ints[x][y] = 1; if (forEnd(x,L,ints)==1) { return 1; } } } if (R<ints[0].length) { if (ints[x][R] != 1) { ints[x][y] = 1; if (forEnd(x,R,ints)==1) { return 1; } } } return 0; } }#华为OD机考#