360笔试

记错时间了,晚了半个小时,顺带一提,为什么java开发那么多道其他语言的题目,不知道是不是c++,我基本上是靠猜的

算法题1:

应该不难可能是逻辑有点小漏缺

只过了55%

代码如下,如有大佬发现代码漏缺,欢迎指正

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {
    static int mod= (int) (1e9+7);
    static long res=0;
    public static void main(String[] args) throws IOException {
        //直接dfs
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        int n = Integer.parseInt(s);
        s = br.readLine();
        dfs(s,0,0);
        System.out.println(res%mod);
    }
    //start end左闭右闭
    public static void dfs(String s,int start,int end){
        //边界条件:当遍历到s的边界时
        if(end>=s.length()-1){

                res+=1;

            return ;
        }
        //截断在此
        if(end-start>2){
            return;
        }
        String a=s.substring(start,end+1);
        int aa=Integer.parseInt(a);
        if(aa<=26) {
            dfs(s, end+1, end+1);
        } else{
            return ;
        }
        if (aa < 3) {
            //接着往下传
            dfs(s, start, end+1);
        }

    }
}

第二题(太痛了,静态变量没设置值,给我搞了半小时一直是0通过率,我还是刚刚才发现的)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;


public class Main1 {
    static int max=0;
    static int n;
    public static void main(String[] args) throws IOException {
        //同样dfs
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        String[] ss = s.split(" +");
       n = Integer.parseInt(ss[0]);
        int a = Integer.parseInt(ss[1]);
        int []num=new int[n+1];//前缀和
        s = br.readLine();
        ss = s.split(" +");
        for (int i = 0; i < ss.length; i++) {
            num[i+1]=Integer.parseInt(ss[i])+num[i];
        }


        dfs(num,a-1,1,new ArrayList<>());
        System.out.println(max);
    }
    
    public static void dfs(int[]nums, int count, int idx, List<Integer>cur){
        //todo:防止对应的dfs出现问题,尤其死递归,不分割那块
        if(count==0){
            //遍历cur,计算出当前的值
            int sum=0;
            int start=0;
            AtomicReference<String> ll= new AtomicReference<>("");
            cur.forEach(integer -> {
                ll.updateAndGet(v -> v + integer);
            });
            System.out.println("list:"+ll.get());
            for (Integer i : cur) {
                System.out.println("sum,add:"+(nums[i]-nums[start]));
                sum+=nums[i]-nums[start];
                start=i;
            }
            //加上最后一段
            Integer last = cur.get(cur.size() - 1);
            System.out.println("n:"+n+"last:"+last);
            System.out.println("last:"+(nums[n]-nums[last]));
//            System.out.println("sum,add:"+(nums[i]-nums[start]));
            sum+=nums[n]-nums[last];
            if(sum>max){
                max=sum;
            }
            return;
        }
        //分割
        if(count>0) {
            cur.add(idx);
            dfs(nums, count - 1, idx + 1, cur);
            cur.remove(cur.size() - 1);
        }
        //不分割
        if(nums.length-idx-1>count) {
            dfs(nums, count, idx + 1, cur);
        }
    }
}

#360#
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务