贝壳9.3前两题,后两题不会,求大佬指点

第一题相同长度木棒的个数
import java.util.Scanner;

public class demo01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] array = new int[n];
        for (int i = 0; i <n ; i++) {
            array[i] = sc.nextInt();
        }
        int[] array1 = new int[n];
        for (int i = 0; i <n ; i++) {

            if(array[i] % 2 ==0){
                array1[i] = array[i] /2 ;
            }else {
                array1[i] = (array[i]+1)/2;
            }
        }
        for (int i = 0; i <n ; i++) {
            System.out.println(array1[i]);
        }
    }
}d
第二题 将括号序列转化为一个常规括号序列
import java.util.Scanner;
import java.util.Stack;

public class Main02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        String[] s = new String[n];
        for (int i = 0; i <n ; i++) {
         nums[i] = sc.nextInt();
         sc.nextLine();
         s[i] = sc.nextLine();
        }
        for (int i = 0; i <n ; i++) {
            System.out.println(result(nums[i],s[i]));
        }
    }
    public static  int result(int length,String s){
        if(length==0) return 0;
        char[] c = s.toCharArray();
        Stack<Character> stack = new Stack<Character>();
        for (int i = 0; i <length ; i++) {
            if (c[i]=='('){
                stack.push('(');
            }else if(stack.isEmpty()) {
                continue;

            }else {
                stack.pop();
            }
        }
        return stack.size();
    }
}d
第三题 得到物品的总价值数
。。
第四题 最佳配置的矩阵质量
。。。

#贝壳找房##笔经#
全部评论
俺也一样。第三题我是这么想的,维护两个大根堆,big存>m的数,small存<=m的树,当天数>k时,每次small弹出k+1个元素,big弹出一个元素,两者进行比较,较大加到结果中,较小的重新压入堆中。最后的k天要特殊考虑,但是逻辑没写出来。
点赞 回复 分享
发布于 2021-09-03 22:00

相关推荐

04-12 13:42
江南大学 C++
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务