拼多多2018校招编程题

[编程题] 最大乘积

给定一个无序数组,包含正数、负数和0,要求从中找出3个数的乘积,使得乘积最大,要求时间复杂度:O(n),空间复杂度:O(1) 
输入描述:
无序整数数组A[n]

输出描述:
满足条件的最大乘积

输入例子1:
3 4 1 2

输出例子1:

24

最大乘积为最大的三个数字乘积或者最大一个数字和最小两个数字乘积,负负得正。

注意此题需要用long

第一种方法是排序,取得最值

第二种方法是使用五个变量

  1. import java.util.Arrays;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.     //nlgn用了排序  
  6.     public static void main1(String[] args) {  
  7.         Scanner sc = new Scanner(System.in);  
  8.         while (sc.hasNext()) {  
  9.             int n = sc.nextInt();  
  10.             long[] nums = new long[n];  
  11.             for (int i = 0; i < n; i++) {  
  12.                 nums[i] = sc.nextLong();  
  13.             }  
  14.             Arrays.sort(nums);  
  15.             System.out.println(Math.max(nums[n - 1] * nums[0] * nums[1], nums[n - 1] * nums[n - 2] * nums[n - 3]));  
  16.         }  
  17.     }  
  18.       
  19.     //O(n)  
  20.     public static void main(String[] args) {  
  21.         Scanner sc = new Scanner(System.in);  
  22.         while (sc.hasNext()) {  
  23.             int n = sc.nextInt();  
  24.             long[] nums = new long[n];  
  25.             for (int i = 0; i < n; i++) {  
  26.                 nums[i] = sc.nextLong();  
  27.             }  
  28.             long max1 = Long.MIN_VALUE;  
  29.             long max2 = Long.MIN_VALUE;  
  30.             long max3 = Long.MIN_VALUE;  
  31.             long min1 = Long.MAX_VALUE;  
  32.             long min2 = Long.MAX_VALUE;  
  33.             for (int i = 0; i < n; i++) {  
  34.                 if (nums[i] > max1) {  
  35.                     max3 = max2;  
  36.                     max2 = max1;  
  37.                     max1 = nums[i];  
  38.                 } else if (nums[i] > max2) {  
  39.                     max3 = max2;  
  40.                     max2 = nums[i];  
  41.                 } else if (nums[i] > max3) {  
  42.                     max3 = nums[i];  
  43.                 }  
  44.   
  45.                 if (nums[i] < min1) {  
  46.                     min2 = min1;  
  47.                     min1 = nums[i];  
  48.                 } else if (nums[i] < min2) {  
  49.                     min2 = nums[i];  
  50.                 }  
  51.   
  52.             }  
  53.             System.out.println(Math.max(min1 * min2 * max1, max1 * max2 * max3));  
  54.         }  
  55.     }  
  56.   
  57. }  

[编程题] 大整数相乘

有两个用字符串表示的非常大的大整数,算出他们的乘积,也是用字符串表示。不能用系统自带的大整数类型。 
输入描述:
空格分隔的两个字符串,代表输入的两个大整数

输出描述:
输入的乘积,用字符串表示

输入例子1:
72106547548473106236 982161082972751393

输出例子1:
70820244829634538040848656466105986748

  1. import java.util.*;  
  2.   
  3. public class Main {  
  4.     public static void main(String[] args) {  
  5.         Scanner sc = new Scanner(System.in);  
  6.         while (sc.hasNext()) {  
  7.             String str1 = sc.next();  
  8.             String str2 = sc.next();  
  9.             int alen = str1.length();  
  10.             int blen = str2.length();  
  11.   
  12.             char[] s1 = str1.toCharArray();  
  13.             char[] s2 = str2.toCharArray();  
  14.             // 高低位对调  
  15.             covertdata(s1, alen);  
  16.             covertdata(s2, blen);  
  17.             // 两数乘积位数不会超过乘数位数和+ 3位  
  18.             int csize = alen + blen + 3;  
  19.             // 开辟乘积数组  
  20.             int[] c = new int[csize];  
  21.             // 乘积数组填充0  
  22.             for (int ii = 0; ii < csize; ii++) {  
  23.                 c[ii] = 0;  
  24.             }  
  25.             // 对齐逐位相乘(没有进位处理)  
  26.             for (int j = 0; j < blen; j++) {  
  27.                 for (int i = 0; i < alen; i++) {  
  28.                     c[i + j] += Integer.parseInt(String.valueOf(s1[i])) * Integer.parseInt(String.valueOf(s2[j]));  
  29.                 }  
  30.             }  
  31.             // System.out.println(Arrays.toString(c));  
  32.             int m = 0;  
  33.             // 进位处理  
  34.             for (m = 0; m < csize; m++) {  
  35.                 int carry = c[m] / 10;  
  36.                 c[m] = c[m] % 10;  
  37.                 if (carry > 0)  
  38.                     c[m + 1] += carry;  
  39.             }  
  40.             // 找到最高位(避免前导0)  
  41.             for (m = csize - 1; m >= 0;) {  
  42.                 if (c[m] > 0)  
  43.                     break;  
  44.                 m--;  
  45.             }  
  46.             // 由最高位开始打印乘积  
  47.             for (int n = 0; n <= m; n++) {  
  48.                 System.out.print(c[m - n]);  
  49.             }  
  50.             System.out.println("");  
  51.         }  
  52.   
  53.     }  
  54.   
  55.     public static void covertdata(char data[], int len) {  
  56.         // 高低位对调  
  57.         for (int i = 0; i < len / 2; i++) {  
  58.             data[i] += data[len - 1 - i];  
  59.             data[len - 1 - i] = (char) (data[i] - data[len - 1 - i]);  
  60.             data[i] = (char) (data[i] - data[len - 1 - i]);  
  61.         }  
  62.     }  
  63. }  

[编程题] 六一儿童节


六一儿童节,老师带了很多好吃的巧克力到幼儿园。每块巧克力j的重量为w[j],对于每个小朋友i,当他分到的巧克力大小达到h[i] (即w[j]>=h[i]),他才会上去表演节目。老师的目标是将巧克力分发给孩子们,使得最多的小孩上台表演。可以保证每个w[i]> 0且不能将多块巧克力分给一个孩子或将一块分给多个孩子。 

输入描述:
 第一行:n,表示h数组元素个数
 第二行:n个h数组元素
 第三行:m,表示w数组元素个数
 第四行:m个w数组元素

输出描述:
上台表演学生人数

输入例子1:

 3 
 2 2 3
 2
 3 1

输出例子1:
1

排序,贪心,计算总数

  1. import java.util.Arrays;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.     public static void main(String[] args) {  
  6.         Scanner sc = new Scanner(System.in);  
  7.         int n = sc.nextInt();  
  8.         int[] child = new int[n];  
  9.         for (int i = 0; i < n; i++) {  
  10.             child[i] = sc.nextInt();  
  11.         }  
  12.         int m = sc.nextInt();  
  13.         int[] cho = new int[m];  
  14.         for (int i = 0; i < m; i++) {  
  15.             cho[i] = sc.nextInt();  
  16.         }  
  17.         System.out.println(count(n,child,m,cho));  
  18.     }  
  19.     public static int count(int n,int[] child,int m ,int[] cho){  
  20.         int res = 0;  
  21.         Arrays.sort(child);  
  22.         Arrays.sort(cho);  
  23.         for (int i=0,j=0;i<m&j<n;i++){  
  24.             if (cho[i]>=child[j]){  
  25.                 res++;  
  26.                 j++;  
  27.             }  
  28.         }  
  29.         return res;  
  30.     }  
  31. }  

[编程题] 迷宫寻路


假设一个探险家被困在了地底的迷宫之中,要从当前位置开始找到一条通往迷宫出口的路径。迷宫可以用一个二维矩阵组成,有的部分是墙,有的部分是路。迷宫之中有的路上还有门,每扇门都在迷宫的某个地方有与之匹配的钥匙,只有先拿到钥匙才能打开门。请设计一个算法,帮助探险家找到脱困的最短路径。如前所述,迷宫是通过一个二维矩阵表示的,每个元素的值的含义如下 0-墙,1-路,2-探险家的起始位置,3-迷宫的出口,大写字母-门,小写字母-对应大写字母所代表的门的钥匙 
输入描述:
迷宫的地图,用二维矩阵表示。第一行是表示矩阵的行数和列数M和N
后面的M行是矩阵的数据,每一行对应与矩阵的一行(中间没有空格)。M和N都不超过100, 门不超过10扇。

输出描述:
路径的长度,是一个整数

输入例子1:
5 5
02111
01a0A
01003
01001
01111

输出例子1:
7


这题就是普通的bfs多了‘钥匙’这个状态, 所以keys[x][y][key]的意义就是 横坐标为x,纵坐标为y,钥匙状态为key的点是否访问过,  钥匙的状态 就用二进制数表示 最多10 把钥匙 那就是1024,  比如我现在有第二把钥匙和第四把钥匙  那么我的钥匙状态就是 0101000000 也就是 320

  1. import java.util.LinkedList;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.     // 四个方向  
  6.     private static int[] xx = new int[] { 001, -1 };  
  7.     private static int[] yy = new int[] { 1, -100 };  
  8.   
  9.     public static void main(String[] args) {  
  10.         Scanner scanner = new Scanner(System.in);  
  11.   
  12.         while (scanner.hasNext()) {  
  13.             int m = scanner.nextInt();  
  14.             int n = scanner.nextInt();  
  15.             scanner.nextLine();  
  16.   
  17.             char[][] datas = new char[m][n];  
  18.             for (int i = 0; i < m; i++) {  
  19.                 datas[i] = scanner.nextLine().toCharArray();  
  20.             }  
  21.   
  22.             int x0 = 0, y0 = 0;  
  23.             int xd = 0, yd = 0;  
  24.   
  25.             for (int i = 0; i < m; i++) {  
  26.                 for (int j = 0; j < m; j++) {  
  27.                     if (datas[i][j] == '2') {  
  28.                         x0 = i;  
  29.                         y0 = j;  
  30.                         continue;  
  31.                     }  
  32.   
  33.                     if (datas[i][j] == '3') {  
  34.                         xd = i;  
  35.                         yd = j;  
  36.                         break;  
  37.                     }  
  38.                 }  
  39.             }  
  40.   
  41.             System.out.println(bfs(datas, m, n, x0, y0, xd, yd));  
  42.         }  
  43.   
  44.     }  
  45.   
  46.     private static int bfs(char[][] datas, int m, int n, int x0, int y0, int xd, int yd) {  
  47.         LinkedList<Node> queue = new LinkedList<>();  
  48.   
  49.         int[][][] keys = new int[m][n][1024];  
  50.   
  51.         for (int i = 0; i < m; i++) {  
  52.             for (int j = 0; j < n; j++) {  
  53.                 for (int s = 0; s < 1024; s++) {  
  54.                     keys[i][j][s] = Integer.MAX_VALUE;  
  55.                 }  
  56.             }  
  57.         }  
  58.   
  59.         queue.add(new Node(x0, y0, 0));  
  60.         keys[x0][y0][0] = 0;  
  61.   
  62.         Node node = null;  
  63.         int x = 0;  
  64.         int y = 0;  
  65.         int key = 0;  
  66.   
  67.         while (queue.size() > 0) {  
  68.             node = queue.poll();  
  69.             x = node.x;  
  70.             y = node.y;  
  71.             key = node.key;  
  72.   
  73.             if (x == xd && y == yd)  
  74.                 return keys[x][y][key];  
  75.   
  76.             for (int i = 0; i < 4; i++) {  
  77.                 x = node.x + xx[i];  
  78.                 y = node.y + yy[i];  
  79.                 key = node.key;  
  80.   
  81.                 if (!isValid(x, y, m, n, datas))  
  82.                     continue;  
  83.                 // 最多10把钥匙  
  84.                 if (datas[x][y] >= 'a' && datas[x][y] <= 'j') {  
  85.                     key = key | (0x1 << (datas[x][y] - 'a'));  
  86.                 }  
  87.                 // 有对应的钥匙继续往下走,没有则跳过  
  88.                 if (datas[x][y] >= 'A' && datas[x][y] <= 'J') {// door  
  89.                     if ((key & (0x1 << (datas[x][y] - 'A'))) > 0) {// haskey  
  90.                         // key = key | ~(0x1 << (datas[x][y] - 'A'));  
  91.                     } else {  
  92.                         continue;  
  93.                     }  
  94.                 }  
  95.                 // keys[x][y][key] 钥匙数  
  96.                 if (keys[x][y][key] > keys[node.x][node.y][node.key] + 1) {  
  97.                     keys[x][y][key] = keys[node.x][node.y][node.key] + 1;  
  98.                     queue.add(new Node(x, y, key));  
  99.                 }  
  100.   
  101.             }  
  102.         }  
  103.   
  104.         return Integer.MAX_VALUE;  
  105.     }  
  106.   
  107.     private static boolean isValid(int x, int y, int m, int n, char[][] data) {  
  108.         if (x >= 0 && x < m && y >= 0 && y < n && data[x][y] != '0')  
  109.             return true;  
  110.         return false;  
  111.     }  
  112.   
  113.     private static class Node {  
  114.         int x;  
  115.         int y;  
  116.         int key;  
  117.   
  118.         public Node(int x, int y, int keys) {  
  119.             this.x = x;  
  120.             this.y = y;  
  121.             this.key = keys;  
  122.         }  
  123.     }  
  124.   

此篇为转载。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值