# 1、返回数组中下标为m的值是第几大,如果相等,则在前面的大
private static int fun(int id,int[] arr){
int res = 0;
int a = arr[id-1];
for (int j : arr) {
if (j > a) {
++res;
}
}
for (int i = id-1; i >= 0; i--) {
if (a == arr[i]){
++res;
}
}
return res;
}
# 2、返回数组中两个乘积大于或等于目标值的方案个数。
private static long fun(int n,long k,long[] nums){
Arrays.sort(nums);
long res = 0;
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if (nums[i]*nums[j] >= k){
res += n-j;
break;
}
}
}
return res*2;
}
# 3、返回树中两两相交节点最多有几对。
public class Main {
static int res = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<List<Integer>> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(new ArrayList<>());
}
for (int i = 0; i < n - 1; i++) {
int a = scanner.nextInt();
list.get(a-1).add(i+2);
}
System.out.println(fun(list));
}
private static int fun(List<List<Integer>> list){
dfs(list,0);
return res;
}
private static int dfs(List<List<Integer>> list,int index){
List<Integer> nums = list.get(index);
if (nums.isEmpty()){
++res;
return -1;
}
for (int i = 0; i < nums.size(); i++) {
int flag = dfs(list, nums.get(i) - 1);
if (flag == -1){
nums.clear();
}
}
return 0;
}
}
#小红书笔试#