250322网易互娱后端暑期笔试
贴个Java代码
求股票购买和售出的最大收益
贪心,如果这一天存在正数的收益,那么就直接买入,第二天售出即可。因为这样,就可以保证下一次,可以买到更多的股票
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int N = in.nextInt(), M = in.nextInt(), K = in.nextInt(); double[][] nums = new double[N][M]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { nums[i][j] = in.nextDouble(); } } double res = K; int[][] records = new int[N][2]; for (int i = 0; i < N; i++) { // 遍历每一天的股票行情 int[] cur = new int[2]; if (i == 0) cur[0] = -1; // 第0天没有卖出 else cur[0] = records[i-1][1]; // 这一天的卖出,就是上一天的买入 double max = -(int)1e9; int maxIdx = -1; if (i == N-1) cur[1] = -1; // 最后一天没有买入 else { for (int j = 0; j < M; j++) { // 找到这一天最大收益的股票 if (nums[i+1][j]/nums[i][j] > max) { max = nums[i+1][j]/nums[i][j]; maxIdx = j; } } if (max > 1) { // 大于1才有收益 cur[1] = maxIdx; res *= max; } else { cur[1] = -1; } } records[i] = cur; } System.out.println(String.format("%.4f", res)); for (int i = 0; i < N; i++) { System.out.println(records[i][0] + " " + records[i][1]); } } } }
计算2048矩阵在上下左右移动后的结果矩阵
艰难模拟,写了好久。。
刚开始没考虑到中间有0值的情况,卡在9%,比如说 8 0 8 0 左移的结果是 16 0 0 0
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int T = in.nextInt(); while (T-- > 0) { int c = in.nextInt(); int[] ops = new int[c]; for (int i = 0; i < c; i++) ops[i] = in.nextInt(); int m = in.nextInt(), n = in.nextInt(); int[][] grid = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { grid[i][j] = in.nextInt(); } } for (int op : ops) { if (op == 0) { // up for (int j = 0; j < n; j++) { int idx = 0; // 要更新写入的坐标 for (int i = 0; i < m; i++) { if (grid[i][j] == 0) continue; // 跳过0值 int cur = grid[i][j]; // 当前不为0的值 while (i+1 < m && grid[i+1][j] == 0) i++; // 找到下一个不为0的值 if (i+1 < m && cur == grid[i+1][j]) { // 相等则合并 grid[idx++][j] = cur*2;; i++; } else { // 不相等则保存当前值到坐标即可 grid[idx++][j] = cur; } } while (idx < m) grid[idx++][j] = 0; // System.out.println(Arrays.toString(col)); } } else if (op == 1) { // left for (int i = 0; i < m; i++) { int[] row = grid[i]; int idx = 0; for (int j = 0; j < n; j++) { if (row[j] == 0) continue; int cur = row[j]; while (j+1 < n && row[j+1] == 0) j++; if (j+1 < n && cur == row[j+1]) { row[idx++] = cur*2; j++; } else { row[idx++] = cur; } } while (idx < n) row[idx++] = 0; } } else if (op == 2) { // down for (int j = 0; j < n; j++) { int idx = m-1; for (int i = m-1; i >= 0; i--) { if (grid[i][j] == 0) continue; int cur = grid[i][j]; while (i-1 >= 0 && grid[i-1][j] == 0) i--; if (i-1 >= 0 && cur == grid[i-1][j]) { grid[idx--][j] = cur*2; i--; } else { grid[idx--][j] = cur; } } while (idx >= 0) grid[idx--][j] = 0; } } else { //right for (int i = 0; i < m; i++) { int[] row = grid[i]; int idx = n-1; for (int j = n-1; j >= 0; j--) { if (row[j] == 0) continue; int cur = row[j]; while (j-1 >= 0 && row[j-1] == 0) j--; if (j-1 >= 0 && cur == row[j-1]) { row[idx--] = cur*2; j--; } else { row[idx--] = cur; } } while (idx >= 0) row[idx--] = 0; } } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print(grid[i][j] + " "); } System.out.println(); } } } } }
a*x + b*y + c*z + d*w = n
,求xyzw的可能取值,输出字典序最小的那个结果
xyzw的范围是0到2500
提前处理c*z + d*w
的所有可能结果,用HashMap保存,然后遍历a和b查看是否有满足的情况
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(), d = in.nextInt(), n = in.nextInt(); HashMap<Long, int[]> map = new HashMap<>(); for (int i = 0; i <= 2500; i++) { // `c*z + d*w`的所有可能结果 for (int j = 0; j <= 2500; j++) { map.putIfAbsent((long)i*c + (long)j*d, new int[]{i, j}); // 这里不能覆盖,才能保证c和d字典序最小 } } String res = ""; for (int i = 0; i <= 2500; i++) { if (res.length() > 0) break; for (int j = 0; j <= 2500; j++) { long cur = (long)i*a + (long)j*b; if (n-cur < 0) break; // 由于都大于0,所以更大的j更不行了,这里可以提前跳出 int[] val = map.get(n-cur); if (val != null) { // 满足题目条件,保存结果 res = i + " " + j + " " + val[0] + " " + val[1]; break; } } } if (res.length() > 0) System.out.println(res); else System.out.println(-1); } } }