jager


私信TA

用户名:uq_37087738881

访问量:591

签 名:

等  级
排  名 16697
经  验 750
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:

将数组排序和三次循环

注意事项:

注意终止条件

参考代码:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int n = scanner.nextInt();
       int k = scanner.nextInt();
       int[] nums = new int[n];
       for (int i = 0; i < n; i++) {
           nums[i]=scanner.nextInt();
       }
       int result = -1;
       Arrays.sort(nums);
       for (int i = n-1; i >=0; i--) {
           if(nums[i]*3-3<result)
               break;
           for (int j = i-1; j >=0; j--) {
               if(nums[i]+nums[j]+nums[j]-1<result)
                   break;
               for (int l = j-1; l >=0; l--) {
                   int temp = nums[i] + nums[j] + nums[l];
                   if(temp<result){
                      break;
                   }
                   if(temp %k==0)
                   {
                     result = temp;
                   }
               }
           }
       }
       System.out.println(result);
   }
}

 

0.0分

2 人评分

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

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

代码解释器

代码纠错

SQL生成与解释

  评论区