OPPO笔试
第一题过桥,就是最长连续0子序列。
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
int juice = 0;
int curLength = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
// int cur = arr[i];
int j = i;
while (j < n && arr[j] == 0) {
j++;
}
curLength = j - i;
juice = Math.max(juice, curLength);
}
}
juice+=1;
System.out.println(juice);
}
}
第二题分割数组,每一段满足位相加小于k,求最小段数。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); //长度
int k = in.nextInt(); //每段f函数值的上限
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
int curSum = 0;
int pieces = 0;
for (int i = 0; i < n; i++) {
if (arr[i] > k) {
System.out.println(-1);
return;
}
curSum += arr[i];
if (curSum > k) {
curSum = arr[i];
pieces += 1;
} else {
continue;
}
}
pieces+=1;
System.out.println(pieces);
}
}
第三题1145子串,先算后缀和再求连续11子串。
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
String str = in.nextLine();
long[] four_five = new long[n + 2];
int fiveCount = 0;
four_five[n + 1] = 0;
four_five[n] = 0;
for (int i = n - 1; i >= 0; i--) {
if (str.charAt(i) == '5') {
fiveCount += 1;
}
four_five[i] = four_five[i + 1];
if (str.charAt(i) == '4') {
four_five[i] += fiveCount;
}
}
long juice = 0;
for (int i = 0; i < n - 1; i++) {
if (str.charAt(i) == '1' && str.charAt(i + 1) == str.charAt(i)) {
juice += four_five[i + 2];
}
}
juice %= 1000000007;
System.out.println(juice);
}
}
挺不难的,但是挺多人全a也挂的

#牛客AI配图神器#
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
int juice = 0;
int curLength = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
// int cur = arr[i];
int j = i;
while (j < n && arr[j] == 0) {
j++;
}
curLength = j - i;
juice = Math.max(juice, curLength);
}
}
juice+=1;
System.out.println(juice);
}
}
第二题分割数组,每一段满足位相加小于k,求最小段数。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); //长度
int k = in.nextInt(); //每段f函数值的上限
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
int curSum = 0;
int pieces = 0;
for (int i = 0; i < n; i++) {
if (arr[i] > k) {
System.out.println(-1);
return;
}
curSum += arr[i];
if (curSum > k) {
curSum = arr[i];
pieces += 1;
} else {
continue;
}
}
pieces+=1;
System.out.println(pieces);
}
}
第三题1145子串,先算后缀和再求连续11子串。
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
String str = in.nextLine();
long[] four_five = new long[n + 2];
int fiveCount = 0;
four_five[n + 1] = 0;
four_five[n] = 0;
for (int i = n - 1; i >= 0; i--) {
if (str.charAt(i) == '5') {
fiveCount += 1;
}
four_five[i] = four_five[i + 1];
if (str.charAt(i) == '4') {
four_five[i] += fiveCount;
}
}
long juice = 0;
for (int i = 0; i < n - 1; i++) {
if (str.charAt(i) == '1' && str.charAt(i + 1) == str.charAt(i)) {
juice += four_five[i + 2];
}
}
juice %= 1000000007;
System.out.println(juice);
}
}
挺不难的,但是挺多人全a也挂的
全部评论
相关推荐
点赞 评论 收藏
分享
04-03 10:21
南京理工大学 算法工程师 点赞 评论 收藏
分享
点赞 评论 收藏
分享