我試圖解決來自Geeksforgeeks的最大整數(shù)值問題。問題說明如下:給定一個由數(shù)字 (0-9) 組成的字符串 S,您的任務是通過在遍歷時在數(shù)字之間放置“*”或“+”運算符來找到可以從字符串中獲得的最大值從字符串的左到右,一次選擇一個數(shù)字。輸入:輸入的第一行包含 T 表示測試用例的數(shù)量。T 測試用例如下。每個測試用例包含一行表示字符串的輸入。輸出:對于每個測試用例,打印獲得的最大值。這就是我所做的:class GFG { public static void sort(int[] numbers) { int n = numbers.length; for (int i = 1; i < n; ++i) { int key = numbers[i]; int j = i - 1; while (j >= 0 && numbers[j] > key) { numbers[j + 1] = numbers[j]; j = j -1 ; } numbers[j + 1] = key; } System.out.println(numbers.length - 1); } public static void main (String[] args) { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); int [] maxNum; for(int i = 0; i< testCases; i++) { String numbers = sc.nextLine(); char[] cNumbers = numbers.toCharArray(); maxNum = new int [cNumbers.length]; for(int j = 0; j + 1 < cNumbers.length; j++) { int sum = 0; int mult = 0; sum = cNumbers[j] + cNumbers[j + 1]; mult = cNumbers[j] * cNumbers[j + 1]; int maxNumber = Math.max(sum, mult); maxNum[i] = maxNumber; } sort(maxNum); } }}輸入示例:2 01230 891 我的輸出:-1 4 正確輸出:9 73我的代碼有什么問題?!
添加回答
舉報
0/150
提交
取消