# [编程题] 最大乘积

3 4 1 2

24

import java.util.Arrays;
import java.util.Scanner;

public class Main {
//nlgn用了排序
public static void main1(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
long[] nums = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextLong();
}
Arrays.sort(nums);
System.out.println(Math.max(nums[n - 1] * nums[0] * nums[1], nums[n - 1] * nums[n - 2] * nums[n - 3]));
}
}

//O(n)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
long[] nums = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextLong();
}
long max1 = Long.MIN_VALUE;
long max2 = Long.MIN_VALUE;
long max3 = Long.MIN_VALUE;
long min1 = Long.MAX_VALUE;
long min2 = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (nums[i] > max1) {
max3 = max2;
max2 = max1;
max1 = nums[i];
} else if (nums[i] > max2) {
max3 = max2;
max2 = nums[i];
} else if (nums[i] > max3) {
max3 = nums[i];
}

if (nums[i] < min1) {
min2 = min1;
min1 = nums[i];
} else if (nums[i] < min2) {
min2 = nums[i];
}

}
System.out.println(Math.max(min1 * min2 * max1, max1 * max2 * max3));
}
}

}

# [编程题] 大整数相乘

72106547548473106236 982161082972751393

70820244829634538040848656466105986748

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.next();
String str2 = sc.next();
int alen = str1.length();
int blen = str2.length();

char[] s1 = str1.toCharArray();
char[] s2 = str2.toCharArray();
// 高低位对调
covertdata(s1, alen);
covertdata(s2, blen);
// 两数乘积位数不会超过乘数位数和+ 3位
int csize = alen + blen + 3;
// 开辟乘积数组
int[] c = new int[csize];
// 乘积数组填充0
for (int ii = 0; ii < csize; ii++) {
c[ii] = 0;
}
// 对齐逐位相乘(没有进位处理)
for (int j = 0; j < blen; j++) {
for (int i = 0; i < alen; i++) {
c[i + j] += Integer.parseInt(String.valueOf(s1[i])) * Integer.parseInt(String.valueOf(s2[j]));
}
}
// System.out.println(Arrays.toString(c));
int m = 0;
// 进位处理
for (m = 0; m < csize; m++) {
int carry = c[m] / 10;
c[m] = c[m] % 10;
if (carry > 0)
c[m + 1] += carry;
}
// 找到最高位（避免前导0）
for (m = csize - 1; m >= 0;) {
if (c[m] > 0)
break;
m--;
}
// 由最高位开始打印乘积
for (int n = 0; n <= m; n++) {
System.out.print(c[m - n]);
}
System.out.println("");
}

}

public static void covertdata(char data[], int len) {
// 高低位对调
for (int i = 0; i < len / 2; i++) {
data[i] += data[len - 1 - i];
data[len - 1 - i] = (char) (data[i] - data[len - 1 - i]);
data[i] = (char) (data[i] - data[len - 1 - i]);
}
}
}


# [编程题] 六一儿童节

第一行：n，表示h数组元素个数
第二行：n个h数组元素
第三行：m，表示w数组元素个数
第四行：m个w数组元素

3
2 2 3
2
3 1

1

import java.util.Arrays;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] child = new int[n];
for (int i = 0; i < n; i++) {
child[i] = sc.nextInt();
}
int m = sc.nextInt();
int[] cho = new int[m];
for (int i = 0; i < m; i++) {
cho[i] = sc.nextInt();
}
System.out.println(count(n,child,m,cho));
}
public static int count(int n,int[] child,int m ,int[] cho){
int res = 0;
Arrays.sort(child);
Arrays.sort(cho);
for (int i=0,j=0;i<m&j<n;i++){
if (cho[i]>=child[j]){
res++;
j++;
}
}
return res;
}
}


# [编程题] 迷宫寻路

5 5
02111
01a0A
01003
01001
01111

7

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
// 四个方向
private static int[] xx = new int[] { 0, 0, 1, -1 };
private static int[] yy = new int[] { 1, -1, 0, 0 };

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

while (scanner.hasNext()) {
int m = scanner.nextInt();
int n = scanner.nextInt();
scanner.nextLine();

char[][] datas = new char[m][n];
for (int i = 0; i < m; i++) {
datas[i] = scanner.nextLine().toCharArray();
}

int x0 = 0, y0 = 0;
int xd = 0, yd = 0;

for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (datas[i][j] == '2') {
x0 = i;
y0 = j;
continue;
}

if (datas[i][j] == '3') {
xd = i;
yd = j;
break;
}
}
}

System.out.println(bfs(datas, m, n, x0, y0, xd, yd));
}

}

private static int bfs(char[][] datas, int m, int n, int x0, int y0, int xd, int yd) {

int[][][] keys = new int[m][n][1024];

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int s = 0; s < 1024; s++) {
keys[i][j][s] = Integer.MAX_VALUE;
}
}
}

keys[x0][y0][0] = 0;

Node node = null;
int x = 0;
int y = 0;
int key = 0;

while (queue.size() > 0) {
node = queue.poll();
x = node.x;
y = node.y;
key = node.key;

if (x == xd && y == yd)
return keys[x][y][key];

for (int i = 0; i < 4; i++) {
x = node.x + xx[i];
y = node.y + yy[i];
key = node.key;

if (!isValid(x, y, m, n, datas))
continue;
// 最多10把钥匙
if (datas[x][y] >= 'a' && datas[x][y] <= 'j') {
key = key | (0x1 << (datas[x][y] - 'a'));
}
// 有对应的钥匙继续往下走，没有则跳过
if (datas[x][y] >= 'A' && datas[x][y] <= 'J') {// door
if ((key & (0x1 << (datas[x][y] - 'A'))) > 0) {// haskey
// key = key | ~(0x1 << (datas[x][y] - 'A'));
} else {
continue;
}
}
// keys[x][y][key] 钥匙数
if (keys[x][y][key] > keys[node.x][node.y][node.key] + 1) {
keys[x][y][key] = keys[node.x][node.y][node.key] + 1;
}

}
}

return Integer.MAX_VALUE;
}

private static boolean isValid(int x, int y, int m, int n, char[][] data) {
if (x >= 0 && x < m && y >= 0 && y < n && data[x][y] != '0')
return true;
return false;
}

private static class Node {
int x;
int y;
int key;

public Node(int x, int y, int keys) {
this.x = x;
this.y = y;
this.key = keys;
}
}

}

https://www.nowcoder.com/test/6161937/summary

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客