publicclassMain { publicstaticvoidmain(String[] args) { Scannerin=newScanner(System.in); int n, m; n = in.nextInt(); m = in.nextInt(); int[][] nums = newint[n][m]; for (inti=0; i < n; i++) { for (intj=0; j < m; j++) { nums[i][j] = in.nextInt(); } }
intres=0; for (inti=0; i < n; i++) { for (intj=0; j < m; j++) { for (intlen=1; (i + len < n && j + len < m); len++) { inttemp= nums[i][j] + nums[i][j + len] + nums[i + len][j] + nums[i + len][j + len]; res = Math.max(res, temp); } } } System.out.println(res);
} }
测试案例只通过了这么一点点,不知道还有其他什么情况没有考虑到
编程题2-小红的乘法操作
思路
理解是求 b 和 a 的商,然后看这个商由x, y的多次乘法构成,先将商和x, y中最大的求商,直到商为1
publicclassMain { publicstaticvoidmain(String[] args) { Scannerin=newScanner(System.in); int x, y, a, b;
x = in.nextInt(); y = in.nextInt(); a = in.nextInt(); b = in.nextInt();
intres= calcCnt(x, y, a, b); System.out.println(res); }
publicstaticintcalcCnt(int x, int y, int a, int b) { if (b%a != 0) return -1; intcnt=0, res = b/a; if (x > y) { inttemp= x; y = x; x = y; } while (res != 1) { if (res % y == 0) { res /= y; cnt++; } elseif (res % x == 0){ res /= x; cnt++; } else { return -1; } } return cnt; } }
publicclasstest { publicstaticvoidmain(String[] args) { Scannerin=newScanner(System.in); int n, k; n = in.nextInt(); k = in.nextInt(); int[][] city = newint[n][2];
for (inti=0; i < n; i++) { city[i][0] = in.nextInt(); city[i][1] = in.nextInt(); }
intmax_happy=0; for (inti=0; i < n; i++) { intmax_cost= city[i][0]; intmin_cost= city[i][0]; inttemp_happy= city[i][1]; for (intj=0; j < n; j++) { if (j != i) { if (city[j][0] - min_cost < k && max_cost - city[j][0] < k) { temp_happy += city[j][1]; min_cost = Math.min(min_cost, city[j][0]); max_cost = Math.max(max_cost, city[j][0]); } } } max_happy = Math.max(max_happy, temp_happy); } System.out.println(max_happy);