度小满笔试情况,最后一题有大佬会做吗
2.18,前两题常规,最后一题不知道咋做。收拾收拾准备做广联达了
import java.util.Scanner;
/*
第一题
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scan.nextInt();
}
int res=0;
for (int i = 0; i < n; i++) {
int max = nums[i];
int min = nums[i];
for (int j = i; j < n; j++) {
max = Math.max(max,nums[j]);
min = Math.min(min,nums[j]);
if(max==min*k){
res++;
}
}
}
System.out.println(res);
}
}
/*
第二题
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
while(T-->0){
int n = scan.nextInt();
int m = scan.nextInt();
int k = scan.nextInt();
long x = scan.nextLong();
int[][] nums = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
nums[i][j] = scan.nextInt();
}
}
if(dfs(nums,0,0,0,x)){
System.out.println("yes");
}else{
System.out.println("no");
}
}
}
public static boolean dfs(int[][] nums , int i,int j,long sum,long target){
if(i>= nums.length||j>=nums[0].length)return false;
sum +=nums[i][j];
if(sum==target && i==nums.length-1 && j==nums[0].length-1){
return true;
}
return dfs(nums,i+1,j,sum,target)||dfs(nums,i,j+1,sum,target);
}
} 