题目1 难度:一星
将一个压缩的字符串原样输出。连续的3个字符及以上才需要压缩,否则输出!error
。
字符串包含'@'
和'A-Z'
时也输出!error
。
示例1:
input: 2fdd output: !error
示例2:
input: 3fdd output: fffdd
示例3:
input: 4f@A output: !error
我的答案
import java.util.*; /** * @author leekari * @date 2020/10/15 23:03 * @description */ public class Test { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); char[] chars = s.toCharArray(); if (chars.length > 100) { System.out.println("!error"); }else { boolean error = false; List<Object[]> list = new ArrayList<>(); for (int i = 0; i < chars.length; i++) { Object[] arr = new Object[2]; if (chars[i] >= '0' && chars[i] <= '9' && i == chars.length - 1) { error = true; System.out.println("!error"); break; } if (chars[i] >= 'A' && chars[i] <= 'Z' || chars[i] == 'a') { error = true; System.out.println("!error"); break; } if (chars[i] >= '0' && chars[i] <= '9') { int count = 0; char c = ' '; if (chars[i + 1] >= '0' && chars[i + 1] <= '9') { count = (chars[i] - 48) * 10 + chars[i + 1] - 48; if (chars[i + 2] > 'a' && chars[i + 2] < 'z') { c = chars[i + 2]; i = i + 2; }else { error = true; System.out.println("!error"); break; } }else { count = chars[i] - 48; if (chars[i + 1] > 'a' && chars[i + 1] < 'z') { c = chars[i + 1]; i = i + 1; }else { error = true; System.out.println("!error"); break; } } if (count <= 2) { error = true; System.out.println("!error"); break; } arr[0] = count; arr[1] = c; list.add(arr); }else if (chars[i] >= 'a' && chars[i] <= 'z'){ arr[0] = 1; arr[1] = chars[i]; list.add(arr); } } if (!error){ StringBuilder sb = new StringBuilder(); for (Object[] o: list) { int count = (int) o[0]; char c = (char) o[1]; while (count > 0) { sb.append(c); count--; } } if (sb.length() > 100){ System.out.println("!error"); }else { System.out.println(sb.toString()); } } } } }
题目2 难度:一星
存在不等式:
a11 * x1 + a12 * x2 + a13 * x3 + a14 * x4 + a15 * x5 >= b1 a21 * x1 + a22 * x2 + a23 * x3 + a24 * x4 + a25 * x5 >= b2 a31 * x1 + a32 * x2 + a33 * x3 + a34 * x4 + a35 * x5 >= b3
其中a11,a21,...,a35都为int
类型
x1,...,x5为double
类型,b1,b2,b3为double
类型
符号位为'>='
或者'<='
判断不等式是否成立,并输出最大差
示例1:
input: 2.36,3,6,7.1,6;1,30,8.6,2.5,21;0.3,69,5.3,6.6,7.8;1,13,2,17,5;340,67,300.6;<=,>=,<= output: false 758
示例2:
input: 2.3,3,5.6,7,6;11,3,8.6,25,1;0.3,9,5.3,66,7.8;1,3,2,7,5;340,670,80.6;<=,<=,<= output: false 458
我的答案
import java.util.*; /** * @author leekari * @date 2020/10/15 23:54 * @description */ public class Test2 { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); String[] strings = str.split(";"); String[] a1 = strings[0].split(","); String[] a2 = strings[1].split(","); String[] a3 = strings[2].split(","); String[][] doubleA = new String[][]{a1, a2, a3}; String[] x = strings[3].split(","); String[] b = strings[4].split(","); String[] flags = strings[5].split(","); boolean ok = true; double max = Double.MIN_VALUE; for (int i = 0; i < flags.length; i++) { String[] a = doubleA[i]; double result = 0.0; for (int j = 0; j < a.length; j++) { result += Double.parseDouble(a[j]) * Integer.parseInt(x[j]); } if (flags[i].equals(">=")) { double sub = result - Double.parseDouble(b[i]); if (sub < 0){ ok = false; } max = Math.max(max, Math.abs(sub)); }else { double sub = Double.parseDouble(b[i]) - result; if (sub < 0){ ok = false; } max = Math.max(max, Math.abs(sub)); } } System.out.println(ok + " " + (int)max); } }
题目3 难度:二星
有m*n
台计算机,计算机由0,1表示,组成局域网。需要相连的计算机标识为1,就可以组成局域网。计算在m*n
计算机中组成的最大的局域网包含几台计算机。
示例:
input: 2 2 1 0 1 1 output: 3
我的错误答案:
import java.util.Scanner; /** * @author leekari * @date 2020/10/16 00:19 * @description */ public class Test3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] strings = s.split(" "); int n = Integer.parseInt(strings[0]); int m = Integer.parseInt(strings[1]); int[][] arr = new int[n][m]; for (int i = 0; i < m; i++) { String line = scanner.nextLine(); String[] lines = line.split(" "); for (int j = 0; j < lines.length; j++) { arr[j][i] = Integer.parseInt(lines[j]); } } int[] res = new int[n * m]; int max = Integer.MIN_VALUE; for(int r = 0; r < m; r++) { for (int l = 0; l < n; l ++) { int count = 0; for (int i = r; i < m; i++) { for (int j = l; j < n; j++) { if (arr[j][i] == 1){ count++; }else { break; } } } max = Math.max(count, max); } } System.err.println(max); } }