我试图找到这个问题的解决方案:我有两个整数A和B(A和B可以有不同的维度).我必须在这两个数组中找到共同的元素.我有另一个条件:公共元素之间的最大距离是k.所以,这是我的解决方案.我认为是正确的:
for (int i = 0; i<A.length; i++){
for (int j=jlimit; (j<B.length) && (j <= ks); j++){
if(A[i]==B[j]){
System.out.println(B[j]);
jlimit = j;
ks = j+k;
}//end if
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做出更好的解决方案?有什么建议?提前致谢!
我在java中读取文件时遇到一些问题:我的文件是例如:
3,4
2
6
4
1
7
3
8
9
Run Code Online (Sandbox Code Playgroud)
第一行3和4是数组A和B的长度,然后是每个数组的元素.我做的
import java.io.*;
import java.util.Arrays;
public class Progetto {
public static void main(String args[])
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("prova.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine = br.readLine(); // step 1
if (strLine != null) {
String[] delims = strLine.split(","); // step 2
// step 3
int[] a = new int[Integer.parseInt(delims[0])];
int[] b = new int[Integer.parseInt(delims[1])]; …Run Code Online (Sandbox Code Playgroud) 我尝试从文件创建一个多字典;
k = []
with open('directory file txt',r) as f:
for line in f:
k.append(line.strip().split('.'))
Run Code Online (Sandbox Code Playgroud)
如何创建这样的多字典:
dict=[key1][key2][key3][key4]='value'
Run Code Online (Sandbox Code Playgroud)
哪一个key1是第一行的第一个元素,key2第二个是等等.