我的目标是扫描包含冒号作为除法的字符串,并将其两个部分保存在一个元组中。
例如:
input: "a:b"
output: ("a", "b")
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的方法不断收到错误消息:
“scanf:字符号 9 处输入错误:寻找 ':',找到 '\n'”。
Scanf.bscanf Scanf.Scanning.stdin "%s:%s" (fun x y -> (x,y));;
Run Code Online (Sandbox Code Playgroud)
此外,我的方法适用于整数,我很困惑为什么它不适用于字符串。
Scanf.bscanf Scanf.Scanning.stdin "%d:%d" (fun x y -> (x,y));;
4:3
- : int * int = (4, 3)
Run Code Online (Sandbox Code Playgroud) public static ArrayList<Integer> duplicates(int[] arr) {
ArrayList<Integer> doubles = new ArrayList<Integer>();
boolean isEmpty = true;
for(int i = 0; i<arr.length; i++) {
for (int j = i+1; j< arr.length; j++) {
if( arr[i] == arr[j] && !doubles.contains(arr[i]) ){
doubles.add(arr[i]);
isEmpty = false;
break;
}
}
}
if(isEmpty) doubles.add(-1);
Collections.sort(doubles);
return doubles;
}
public static void main(String[] args) {
System.out.println( ( duplicates( new int[]{1,2,3,4,4,4} ) ) ); // Return: [4]
}
Run Code Online (Sandbox Code Playgroud)
我用 Java 创建了这个函数,它返回输入 int 数组的倍数,如果输入数组为空或没有倍数,则返回 -1。
它有效,但可能有一种方法可以让它更快。是否有任何好的做法可以使函数总体上更高效、更快?