华为2016校园招聘笔试题

时间:2022-11-13 18:55:17

      昨天刚参加华为的校园招聘笔试,总体来说是很容易的,一般人都可以吧前两道题做出来。下面我来分享一下题目吧:

1.从输入的字符串中找出元音字母:'a','o','e','i','u'。并且输出字符串中元音字母的种类,数量,与其他字符的数量。

输入样例:I love China

输出样例:5 5 7

  Ioeia

我的解答:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
char []chs = str.toCharArray();
List<Character> charList = new ArrayList<Character> ();
Set<Character> charSet = new HashSet<Character>();
for (int i = 0 ; i < chs.length; i ++) {
if (contain(chs[i])) {
charList.add(chs[i]);
charSet.add(chs[i]);
}
}

System.out.println(charSet.size() + " " + charList.size()
+ " " + (str.length() - charList.size()));
for (Character c : charList)
System.out.print(c);
}

private static boolean contain(char ch) {
char [] chs = new char[]{'a','o','e','i','u','A','O','E','I','U'};
for (int i = 0;i < 10 ;i ++)
if (ch == chs[i])
return true;
return false;
}
}


2.找出输入字符串的最大值与次最大值,

输入要求:数字间以逗号隔开。

输出要求:1.如果输入数字为空,则最大值与次最大值均为0

2.如果只输入一个数字,则最大值与次最大值均为该数字

3.如果输入的数字大于等于2,并且都相等,则最大值与次最大值均为该数字

输入样例: 输出样例:

1,3,5,6 6,5

7 7,7

6,6,66,6

我的解答:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
String[] strs = null;
if (str != null && !("").equals(str))
strs = str.split(",");
List<Integer> nums = deal(strs);
int max = getMax(nums);
int secondMax = getSecondMax(nums,max);
System.out.println(max + "," + secondMax);
}

private static int getSecondMax(List<Integer> nums,Integer max) {
if (nums.isEmpty() || nums.size() == 0)
return 0;
if (nums.size() == 1)
return max;
nums.remove(max);
return getMax(nums);
}

private static int getMax(List<Integer> nums) {
int index = 0;
if (nums.isEmpty() || nums.size() == 0)
return 0;
for (int i = 0; i < nums.size();i ++)
if (nums.get(index) < nums.get(i))
index = i;
return nums.get(index);
}

private static List<Integer> deal(String[] strs) {
List<Integer> nums = new ArrayList<Integer>();
if (strs != null) {
for (int i = 0;i < strs.length;i ++)
nums.add(Integer.parseInt(strs[i]));
}
return nums;
}
}


3.计算两个矩形的交叉面积,给定两个矩形的左上角坐标与长和宽,计算交叉面积

输入一个以逗号分隔的字符串,共输入八个数字

第一第二个数字表示第一个矩形的左上角坐标(x,y),第三第四个数字表示第一个矩形长和宽

第五第六个数字表示第二个矩形的左上角坐标(x,y),第七第八个数字表示第二个矩形长和宽

样例输入:

1,2,2,1,2,4,3,1

  样例输出

  1

我的解答:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
String[] strs = str.split(",");
Integer [] nums = new Integer[strs.length];
for (int i = 0;i < nums.length; i ++)
nums[i] = Integer.parseInt(strs[i]);
int result = getSquare(nums);
System.out.println(result);
}

private static int getSquare(Integer[] nums) {
//如果有一个边长为0,则返回0
if (nums[2] == 0 || nums[3] == 0
|| nums[6] == 0 || nums[7] == 0)
return 0;

//获取第一个矩形所包含的所有点
Point[][] pointArr1 = new Point[nums[2] + 1][nums[3] + 1];
for (int i = 0;i < nums[2] + 1;i ++) {
for (int j = 0;j < nums[3] + 1;j ++) {
pointArr1[i][j] = new Point(nums[0] + i,nums[1] - j);
}
}

//获取第二个矩形所包含的所有点
Point[][] pointArr2 = new Point[nums[6] + 1][nums[7] + 1];
for (int i = 0;i < nums[6] + 1;i ++) {
for (int j = 0;j < nums[7] + 1;j ++) {
pointArr2[i][j] = new Point(nums[4] + i,nums[5] - j);
}
}

//获取交叉的点
List<Point> pointList = getAllPoint(pointArr1, pointArr2);

return getSquareByList(pointList);
}

private static int getSquareByList(List<Point> pointList) {
if (pointList == null || pointList.isEmpty()
|| pointList.size() == 0)
return 0;
int maxX = 0, minX = 0, maxY = 0, minY = 0;
for (int i = 0;i < pointList.size();i ++) {
if (pointList.get(maxX).x < pointList.get(i).x)
maxX = i;
if (pointList.get(minX).x > pointList.get(i).x)
minX = i;
if (pointList.get(maxY).y < pointList.get(i).y)
maxY = i;
if (pointList.get(minY).y > pointList.get(i).y)
minY = i;
}
return (pointList.get(maxX).x - pointList.get(minX).x)
*(pointList.get(maxY).y - pointList.get(minY).y);
}

private static List<Point> getAllPoint(Point[][] pointArr1,
Point[][] pointArr2) {
List<Point> pointList = new ArrayList<Point>();
for (int i = 0;i < pointArr1.length;i ++) {
for (int j = 0;j < pointArr1[i].length;j ++) {
for (int k = 0;k < pointArr2.length;k ++) {
for (int m = 0;m < pointArr2[k].length;m ++) {
if (pointArr1[i][j].equals(pointArr2[k][m]))
pointList.add(pointArr1[i][j]);
}
}
}
}
return pointList;
}
}

class Point {
public int x;
public int y;

public Point(int x,int y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}

}

        关注微信公众号每天学习一点程序员的职业经

华为2016校园招聘笔试题