贝壳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
第三题 得到物品的总价值数 。。
第四题 最佳配置的矩阵质量
。。。
滴滴公司福利 1809人发布