参考代码:
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 人评分
【明明的随机数】 (C++代码)(C++库中有qsort函数直接快排,不用码排序代码hhh)浏览:1089 |
C语言训练-舍罕王的失算 (C++代码)(都给答案了还算什么)浏览:989 |
C语言考试练习题_排列 (C++代码)浏览:706 |
C语言考试练习题_一元二次方程 (C语言代码)浏览:761 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:566 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:745 |
C语言程序设计教程(第三版)课后习题9.4 (C语言代码)浏览:696 |
1024题解浏览:873 |
1051(奇了怪了)浏览:739 |
格式化数据输出 (C语言代码)浏览:880 |