爱奇艺2018春招Java工程师编程题题解
字典序最大子序列
package aiqiyi.demo1;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
char[] charArray = str.toCharArray();
int maxPos = 0;
StringBuffer sb = new StringBuffer();
while (maxPos < charArray.length) {
int maxVlue = Integer.MIN_VALUE;
for (int i = maxPos; i < charArray.length; i++) {
if (charArray[i] > maxVlue) {
maxVlue = charArray[i];
maxPos = i;
}
}
sb.append((char) maxVlue);
maxPos++;
}
System.out.println(sb.toString());
}
}
三个整数
package aiqiyi.demo2;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[3];
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
Arrays.sort(arr);
if ((arr[2] - arr[1] + arr[2] - arr[0]) % 2 == 0) {
System.out.println((arr[2] - arr[1] + arr[2] - arr[0]) / 2);
} else {
System.out.println((arr[2] - arr[1] + arr[2] - arr[0] + 3) / 2);
}
}
}
牛牛配糖果
package aiqiyi.demo3;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();// 颜色种类
int m = scanner.nextInt();// 盒子放置m个糖果
int[] l = new int[n];
int[] r = new int[n];
int least = 0;
int[] rem = new int[n];// 表示每一种颜色剩余可以加的
for (int i = 0; i < r.length; i++) {
l[i] = scanner.nextInt();
r[i] = scanner.nextInt();
least += l[i];
rem[i] = r[i] - l[i];
}
int target = m - least;
long[][] dp = new long[n + 1][target + 1];
for (int i = 0; i < dp.length; i++) {
dp[i][0] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= target; j++) {
for (int k = 0; k <= rem[i - 1]; k++) {
if (j - k >= 0)
dp[i][j] += dp[i - 1][j - k];
}
}
}
System.out.println(dp[n][target]);
}
}
#春招##实习##笔试题目##爱奇艺#