OPPO 0810 笔试B卷

感觉难度中等偏低,两道 easy 一道 medium 的样子

90+100+100

第一题

踩小石子过河,算出石子最大间隙就可以

import java.util.*;

class Main{
    static int n;
    static int[] array;
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		array = new int[n];
		for (int i = 0; i < n; i++) {
		    array[i] = sc.nextInt();
		}
		
		int ans = 0;
		int last = -1;
        for (int i = 0; i < n; i++) {
            if (array[i] == 1) {
                ans = Math.max(ans, i - last);
                last = i;
            }
        }
        if (ans == 1) System.out.println(0);
		else System.out.println(ans);
	}
}

第二题

贪心遍历一遍

import java.util.*;

class Main{
    static int n;
    static int k;
    static int[] array;
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		k = sc.nextInt();
		array = new int[n];
		for (int i = 0; i < n; i++) {
		    array[i] = sc.nextInt();
		}
		
		int ans = 0;
		int t = 0;
		for (int i = 0; i < n; i++) {
		    t  |= array[i];
		    if (t > k) {
		        t = array[i];
		        if (t > k) {
            		System.out.println(-1);
            		return;
		        }
		        ans++;
		    }
		}
		System.out.println(ans + 1);
	}
}

第三题

dp,记录每个位置“11”,“114”,“1145”的数量

import java.util.*;

class Main{
    static int n;
    static String str;
    static int MOD = 1000000007;
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		str = sc.next();
		char[] ch = str.toCharArray();
		
		int[][] dp = new int[n][3];
	    for (int i = 1; i < n; i++) {
	        dp[i][0] = dp[i - 1][0]; // "11"
	        dp[i][1] = dp[i - 1][1]; // "114"
	        dp[i][2] = dp[i - 1][2]; // "1145"
	        
	        if (ch[i] == '1' && ch[i - 1] == '1') {
	            dp[i][0] = (dp[i][0] + 1) % MOD;
	        }
	        else if (ch[i] == '4') {
	            dp[i][1] = (dp[i][1] + dp[i][0]) % MOD;
	        }
	        else if (ch[i] == '5') {
	            dp[i][2] = (dp[i][2] + dp[i][1]) % MOD;
	        }
	    }
	    System.out.println(dp[n - 1][2]);
	}
}

全部评论
哥,这个第三题,我记录某个下标之前11的个数,以及某个下标之后5的个数,然后如果这个位置是4,就乘起来,最后累加,这种做法有什么问题嘛,用例没问题,提交就是0分
1 回复 分享
发布于 08-14 10:33 山西

相关推荐

08-30 21:04
已编辑
东南大学 数据分析师
经纬恒润 解决方案工程师 N+5
点赞 评论 收藏
分享
3 8 评论
分享
牛客网
牛客企业服务