参考代码:
import java.util.*;
public class Main {//组合型
static int n;
static int r;
static int[]arr=new int[1000];
static int[]p=new int[1000];
static int count;
static int max=-(int)1e9;
public static void main(String[]args) {
Scanner input=new Scanner(System.in);
int k=input.nextInt();
while(k>0) {
n=input.nextInt();
r=input.nextInt();
for(int i=1;i<=n;i++)p[i]=input.nextInt();
dfs(1,1);
System.out.println(max);
max=-(int)1e9;
k--;
}
}
public static void dfs(int x,int start) {
if((x-1)+(n-start+1)<r)return;/*剪枝 x-1表示当前位置以及放置的个数
n-start+1表示剩下要排的个数 组合的剪纸 */
if(x>r) {
int sum=1;
for(int i=1;i<=r;i++) {
sum*=arr[i];
}
max=Math.max(sum, max);
}
for(int i=start;i<=n;i++) {//start表示有多个分支
arr[x]=p[i];
dfs(x+1,i+1);//这里i+1保证前一个数一定大于后一个数比如123.
arr[x]=0;
}
}
}
0.0分
0 人评分
简单的a+b (C语言代码)浏览:757 |
C语言训练-谁家孩子跑最慢* (C语言代码)浏览:1529 |
C二级辅导-分段函数 (C语言代码)浏览:577 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:818 |
【亲和数】 (C语言代码)浏览:893 |
输出正反三角形 (C语言代码)格式错误!!!浏览:1175 |
众数问题 (C语言代码)浏览:888 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:812 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:724 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:574 |