因为4月初要参加微软的online。所以今天把微软的面试题拿出来做了,自己解答了题目。经过努力已经全部AC了。下面附上我的解答代码。
--------16年微软实习4道题:
第一道题:Magic Box
题目:
题目1 : Magic Box 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 The circus clown Sunny has a magic box. When the circus is performing, Sunny puts some balls into the box one by one. The balls are in three colors: red(R), yellow(Y) and blue(B). Let Cr, Cy, Cb denote the numbers of red, yellow, blue balls in the box. Whenever the differences among Cr, Cy, Cb happen to be x, y, z, all balls in the box vanish. Given x, y, z and the sequence in which Sunny put the balls, you are to find what is the maximum number of balls in the box ever. For example, let's assume x=1, y=2, z=3 and the sequence is RRYBRBRYBRY. After Sunny puts the first 7 balls, RRYBRBR, into the box, Cr, Cy, Cb are 4, 1, 2 respectively. The differences are exactly 1, 2, 3. (|Cr-Cy|=3, |Cy-Cb|=1, |Cb-Cr|=2) Then all the 7 balls vanish. Finally there are 4 balls in the box, after Sunny puts the remaining balls. So the box contains 7 balls at most, after Sunny puts the first 7 balls and before they vanish. 输入 Line 1: x y z Line 2: the sequence consisting of only three characters 'R', 'Y' and 'B'. For 30% data, the length of the sequence is no more than 200. For 100% data, the length of the sequence is no more than 20,000, 0 <= x, y, z <= 20. 输出 The maximum number of balls in the box ever. 提示 Another Sample Sample Input Sample Output 0 0 0 RBYRRBY 4 样例输入 1 2 3 RRYBRBRYBRY 样例输出 7 EmacsNormalVim
AC答案:
import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { int x, y, z; Scanner sc = new Scanner(System.in); x = sc.nextInt(); y = sc.nextInt(); z = sc.nextInt(); LinkedList list1 = new LinkedList(); list1.add(x); list1.add(y); list1.add(z); Collections.sort(list1); String str = sc.next(); int redCount = 0; int yellowCount = 0; int blueCount = 0; int num = 0; int max = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'R') { redCount++; } else if (str.charAt(i) == 'Y') { yellowCount++; } else { blueCount++; } num++; max = Math.max(max, num); LinkedList list2 = new LinkedList(); int x1 = Math.abs(redCount - yellowCount); int y1 = Math.abs(redCount - blueCount); int z1 = Math.abs(yellowCount - blueCount); list2.add(x1); list2.add(y1); list2.add(z1); Collections.sort(list2); if (list1.equals(list2)) { num = 0; redCount = 0; yellowCount = 0; blueCount = 0; } } System.out.println(max); } }
第二道题:
15年:
备注:在hihocoder。需要自己接口。常用基本接口如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()) { int a = in.nextInt(); int b = in.nextInt(); System.out.println(a + b); } } }
We say a string is beautiful if it has the equal amount of 3 or more continuous letters (in increasing order.)
Here are some example of valid beautiful strings: "abc", "cde", "aabbcc", "aaabbbccc".
Here are some example of invalid beautiful strings: "abd", "cba", "aabbc", "zab".
Given a string of alphabets containing only lowercase alphabets (a-z), output "YES" if the string contains a beautiful sub-string, otherwise output "NO".
The first line contains an integer number between 1 and 10, indicating how many test cases are followed.
For each test case: First line is the number of letters in the string; Second line is the string. String length is less than 10MB.
For each test case, output a single line "YES"/"NO" to tell if the string contains a beautiful sub-string.
Huge input. Slow IO method such as Scanner in Java may get TLE.
4 3 abc 4 aaab 6 abccde 3 abb
YES NO YES NO
答案:
import java.util.Scanner; import java.lang.String; import java.util.ArrayList; class MyMap { char c; int count; MyMap(char ch, int n) { c = ch; count = n; } } public class Main { public static void main(String[] args) { int num = 0; Scanner sc = new Scanner(System.in); num = sc.nextInt(); int length = 0; String str = new String(); while (num-- > 0) { length = sc.nextInt(); str = sc.next(); ArrayList<MyMap> list = new ArrayList(); for (int i = 0; i < str.length();) { char tempC = str.charAt(i); int tempN = 0; while (i < str.length() && str.charAt(i) == tempC) { tempN++; i++; } MyMap map = new MyMap(tempC, tempN); list.add(map); } boolean flag = true; for (int i = 1; i < list.size() - 1; i++) { if (list.get(i).c - list.get(i - 1).c == 1 && list.get(i + 1).c - list.get(i).c == 1) { if (list.get(i).count <= list.get(i - 1).count && list.get(i).count <= list.get(i + 1).count) { flag = false; System.out.println("YES"); break; } } } if (flag) { System.out.println("NO"); } } } }