我正在尝试编写一个程序,该程序以递增的顺序给出n个整数数组和k个框,它将原始数组拆分为连续数字的框,即按输入顺序出现>
到目前为止,我已经编写了以下代码
int[] A = {1,2,3,4,5};
int k = 3;
int n = 5;
for(int i = 0; i <= n - k; i++){
for(int j = i+1; j < n-1; j++){
int[] k1 = Arrays.copyOfRange(A, 0, i+1);
int[] k2 = Arrays.copyOfRange(A, i+1, j+1);
int[] k3 = Arrays.copyOfRange(A, j+1, n);
System.out.println(Arrays.toString(k1) + "|" + Arrays.toString(k2) + "|" + Arrays.toString(k3));
}
}
但是,我的代码存在的问题是我已经对循环和k框进行了硬编码,但是我不确定如何解决我的问题。
该功能的目标是在每个框中生成所有元素放置的可能性。
感谢您对该算法的任何帮助或想法!
答案 0 :(得分:3)
搜索时,我遇到了this answer。您可以使用它来获取输入列表的所有可能分区。您必须对该代码进行一些小的修改:<custom-control name="custom" [(ngModel)]="model.custom" minlength="100"></custom-control>
ngAfterViewChecked(): void {
this.formRef.form.controls['custom'].updateValueAndValidity();
console.log(this.formRef.form.controls['custom'].valid);
}
变成def get_sec_info(bb_req_dir,bb_req_name,bb_firmname,sec_missing):
#Build request file and return the filepath
filepath = bb_req_file(bb_req_dir,bb_req_name,bb_firmname,sec_missing)
#Send the file to Bloomberg, wait and download response file. Returns the filename of the response file.
dir_response = bb_upload_download(filepath,bb_req_name)
df_secs = parse_response(dir_response)
return df_secs
def bb_req_file(bb_req_dir,bb_req_name,bb_firmname,sec_missing):
#Build req file
filepath = bb_req_dir + bb_req_name + '.req'
req_file = open(filepath,'w+')
with req_file:
req_file.write('START-OF-FILE')
req_file.write('\n')
req_file.write('FIRMNAME=' + bb_firmname)
req_file.write('\n')
req_file.write('FILETYPE=pc')
req_file.write('\n')
req_file.write('REPLYFILENAME=' + bb_req_name + '.out')
req_file.write('\n')
req_file.write('SECMASTER=yes')
req_file.write('\n')
req_file.write('DATEFORMAT=ddmmyyyy')
req_file.write('\n')
req_file.write('PROGRAMNAME=getdata')
req_file.write('\n')
req_file.write('PROGRAMFLAG=adhoc')
req_file.write('\n')
req_file.write('MIFIR=yes')
req_file.write('\n')
#Add the fields being requested
req_file.write('START-OF-FIELDS')
req_file.write('\n')
req_file.write('CFI_CODE')
req_file.write('\n')
req_file.write('END-OF-FIELDS')
req_file.write('\n')
#Add the securities
req_file.write('START-OF-DATA')
req_file.write('\n')
for sec in sec_missing:
req_file.write(''.join(sec))
req_file.write(' |Ticker|')
req_file.write('\n')
#Close
req_file.write('END-OF-DATA')
req_file.write('\n')
req_file.write('END-OF-FILE')
req_file.close()
return filepath
def bb_upload_download(filepath,bb_req_name):
warnings.filterwarnings('ignore','.*hostkeys')
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection('****', username='****', password='****', cnopts=cnopts) as sftp:
sftp.put(filepath)
response = filepath[:-3] + 'out'
file_response = bb_req_name + '.out'
exists = False
while exists == False:
exists = sftp.isfile(file_response)
sftp.get(file_response,response)
return response
,因此holder.addAll(b);
的值添加在前面而不是结尾,这意味着原始顺序主要是保留而不是颠倒。
之后,您可以使用两个过滤器:
holder.addAll(0,b);
的数量对其进行过滤(为此,我使用了Java 8+流过滤器)。以下是可能的解决方案:
b
哪个输出:
k
答案 1 :(得分:1)
根据此维基百科帖子stars and bars和我的类似回答related question here on SO,这是另一种没有信息流的方式:
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7};
int k = 3;
permute(arr,k);
}
public static void permute(int [] arr, int k ) {
int combinations = (int) Math.pow(2, arr.length - 1);
List<List<List<Integer>>> result = new ArrayList<>();
for (int i = 0; i < combinations; i++) {
String operators = String.format("%" + (arr.length - 1) + "s", Integer.toBinaryString(i)).replace(' ', '0');
int count = 0;
List<List<Integer>> tempListOfList = new ArrayList<>();
List<Integer> tempList = new ArrayList<>();
for (int x = 0; x < operators.length(); x++) {
tempList.add(arr[x]);
if (operators.charAt(x) == '0') {
if(count < k-1){
tempListOfList.add(tempList);
tempList = new ArrayList<>();
}
count++;
}
}
tempList.add(arr[arr.length-1]);
tempListOfList.add(tempList);
if(count == k-1){
result.add(tempListOfList);
}
}
for(List<List<Integer>> parts: result){
System.out.println(parts);
}
}