您的位置:首页 > 移动开发 > Android开发

2018百度android方向校招编程题

2017-09-19 21:40 537 查看
晚上参加百度的校招,一共三道题。

第一道

给定一个数组,要求根据出公式计算,打印计算结果。

sum = a0 + 1/(a1+1/(a2+1/(a3+…+1/(an)))

下面是我的代码,运用递归思想。通过了90%,剩下的10%,应该是得处理精度,时间有点急,这道题因为一些细节花好多时间,一看过了90%,马上就跳下一题,后来回顾,才知道问题再这。

处理精度只需要判断两个相减小于0.00001,就判断相等。这样就OK了。要处理精度是因为计算机是二进制表示数据的,所以有时候两个数明明相等,计算比较出来却不是相等。

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] a = getInt(scanner.nextLine().split(" "));
double x = getInt1(a,0);
int[] b = getInt(scanner.nextLine().split(" "));
double y = getInt1(b,0);
String temp = "=";
if(x > y){
temp =">";
}else if(y < y){
temp = "<";
}
System.out.println(temp);
//       int[] c = {0,1,2,8};
//      System.out.println(getInt1(c, 0));
//      System.out.println(17D/25);

}

private static double getInt1(int[] a,int k) {
if(k == a.length-1){
int temp2 = a[k];
return temp2;
}else if(k > a.length-1){
return 0;
}
double temp = a[k];
double temp1 = getInt1(a,k+1);
return temp + 1d/temp1;
}

private static int[] getInt(String[] split) {

int[] a = new int[split.length];
for(int i = 0; i < split.length;i++){
a[i] = Integer.valueOf(split[i]);
}
return a;
}
}


第二题

给定一个字符串,只包含“W”或者”E”,W属于左边,E属于右边。现在要让你给字符串划成左边和右边,使得W在右边的数量加上E在右边的数量的和最少。注:可全部划为左边或者全部化为右边。

例如:

WWWEE:在第三个W之后划为右边,之前划为左边,和就为0。

WWEWWEEW:在第四个W之后划为右边,之前划为左边,那么有一个。

E跑到左边去了,一个W跑到右边去了,所以和为2

WWW:全部划左边,和就为0。

我的思路是:用动态规划的思想去做。先把计算全部划左边的和,然后计算下一种状态。AC了

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
int[] min = new int[s.length()];
int min1 = Integer.MAX_VALUE;
for(int j = 0; j < s.length();j++){
String temp = s.substring(j,j+1);
if(temp.equals("W")){
min[0]++;
}
}
for(int i = 0; i < s.length();i++){
String temp1 = s.substring(i,i+1);
if(temp1.equals("W")){
if(i != 0){
min[i] = min[i-1] - 1;
}else{
if(min[0] > 0){
min[0]--;
}
}
}else{
if(i != 0)
min[i] = min[i-1] + 1;
else{
min[0]++;
}
}
}

for(int i = 0; i < s.length();i++){
if(min[i] < min1){
min1 = min[i];
}
}
System.out.println(min1);
}
}


第三题

给一个N,和一个C。

N代表是N*N矩阵,C代表矩阵的打印模式。

假如N为3;

当C=1时,数组为:

1 2 3

6 5 4

7 8 9

当C=2时,数组为:

1 6 7

2 5 8

3 4 9

当C=3时,数组为:

1 2 3

8 9 4

7 6 5

当C=4时,数组为:

1 8 7

2 9 6

3 4 5

这道题因为时间不够,只写出了前两种矩阵模式,过了50%,再给我15分钟就算出来了。事实上,看似是四种矩阵,第一种和第二种、第三种和第四种几乎是一模一样的。

我是暴力去把数组的计算出来,时间和空间的复杂度都挺高的,所以我下面的代码有可能会内存超限或者时间超时,我总觉得,有时间复杂为O(n)的办法,我就是用了太多时间去找这个办法导致的代码
4000
写不全的……

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int c = scanner.nextInt();
if (c == 1) {
printfOne(N);
} else if (c == 2) {
printfTow(N);
} else if (c == 3) {
printfThree(N);
} else if(c == 4){
printfFrou(N);
}

}

private static void printfFrou(int n) {
int[][] a = new int

;
int count = 0;
int i = -1;
int j = 0;
while (true) {
if (count >= n * n) {
break;
}

while (count < n * n) {
i++;
if (i <n&&a[i][j] == 0) {
count++;
a[i][j] = count;
}else{
break;
}
}
i--;

while (count < n * n) {
j++;
if (j < n && a[i][j] == 0) {
count++;
a[i][j] = count;
}else{
break;
}
}
j--;

while (count < n * n) {
i--;
if (i >= 0 && a[i][j] == 0) {
count++;
a[i][j] = count;
}else{
break;
}
}
i++;

while (count < n * n) {
j--;
if (j >= 0 && a[i][j] == 0) {
count++;
a[i][j] = count;
}else{
break;
}
}
j++;

}
print(a);
}

private static void printfThree(int n) {
int[][] a = new int

;
int count = 0;
int i = 0;
int j = -1;
while (true) {
if (count >= n * n) {
break;
}

while (count < n * n) {
j++;
if (j < n && a[i][j] == 0) {
count++;
a[i][j] = count;
}else{
break;
}
}
j--;

while (count < n * n) {
i++;
if (i <n&&a[i][j] == 0) {
count++;
a[i][j] = count;
}else{
break;
}
}
i--;

while (count < n * n) {
j--;
if (j >= 0 && a[i][j] == 0) {
count++;
a[i][j] = count;
}else{
break;
}
}
j++;

while (count < n * n) {
i--;
if (i >= 0 && a[i][j] == 0) {
count++;
a[i][j] = count;
}else{
break;
}
}
i++;

}
print(a);
}

private static void printfTow(int n) {
int[][] a = new int

;
int count = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i % 2 == 0) {
a[j][i] = count;
count++;
} else {
a[n - j - 1][i] = count;
count++;
}
}
}
print(a);
}

private static void printfOne(int n) {
int[][] a = new int

;
int count = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i % 2 == 0) {
a[i][j] = count;
count++;
} else {
a[i][n - j - 1] = count;
count++;
}
}
}
print(a);
}

static void print(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}


记录一下,如果你有更好的办法,欢迎交流啊。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  百度 编程