范紫晨


私信TA

用户名:dotcpp0729894

访问量:227

签 名:

等  级
排  名 3471
经  验 1843
参赛次数 0
文章发表 10
年  龄 0
在职情况 学生
学  校 江西财经大学
专  业

  自我简介:

参考代码:

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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区