小米笔试9/19
第一题:55%正确率
package org.example;
import java.util.Scanner;
public class Main {
public static boolean flag ;
public static void main(String[] args) {
int m;
Scanner sc = new Scanner(System.in);
m =sc.nextInt();
while (m-->0){
flag =true;
int N = sc.nextInt();
int n = sc.nextInt();
int c =sc.nextInt();
int arr[]=new int[n];
for(int i=0;i arr[i]=sc.nextInt();
}
int dp[]=new int[N+1];
if(N==0){
System.out.println("YES");
continue;
}
dfs(N,n,c,arr,0,0);
if(flag){
System.out.println("NO");
}else{
System.out.println("YES");
}
}
}
public static void dfs(int N,int n,int c,int arr[],int v,int step){
if(step==n){
return;
}
if(v>N)return;
if(v+c>=N){
flag=false;
return;
}
dfs(N,n,c,arr,v+arr[step],step+1);
dfs(N,n,c,arr,v,step+1);
}
}
这个是dfs,听说有的人剪到了100%,
第二题:100%
package org.example;
import java.util.Arrays;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
int m ;
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
while (m-- > 0) {
int k = sc.nextInt();
int[]b1=new int[k];
int[] b2=new int[k];
for(int i=0;i b1[i]=sc.nextInt();
}
for(int i=0;i b2[i]=sc.nextInt();
}
int[]a1=new int[k];
int[] a2=new int[k];
for(int i=0;i a1[i]=b1[k-1-i];
a2[i]=b2[k-1-i];
}
boolean flag=work(b1,b2,k);
if(flag){
System.out.println("YES");
continue;
}
flag=work(a1,a2,k);
if(flag){
System.out.println("YES");
continue;
}
System.out.println("NO");
}
}
private static boolean work(int[] a, int[] b, int n) {
boolean tip = true;
int c[]=new int[n];
for(int i =0;i if(i==0){
c[i]=Math.min(a[i],b[i]);
}else{
if(a[i] tip = false;
break;
}else if(a[i]>=c[i-1]&&b[i]>=c[i-1]){
c[i]=Math.min(a[i],b[i]);
}else if(a[i]>=c[i-1]){
c[i]=a[i];
}else if (b[i]>=c[i-1]){
c[i]=b[i];
}
}
}
return tip;
}
}
左边扫一下,右边扫一下,就相当于一个升序的一个降序的,可以进行模拟
然后贴一下别人ak的链接:https://www.nowcoder.com/discuss/666334391592910848?sourceSSR=users
欢迎大家和我分享第一题的解法~~~,和你的疑惑
#牛客创作赏金赛##软件开发笔面经#(求生:这个不是zuobi,已经比赛完了,球球不要把我关进小黑屋)
package org.example;
import java.util.Scanner;
public class Main {
public static boolean flag ;
public static void main(String[] args) {
int m;
Scanner sc = new Scanner(System.in);
m =sc.nextInt();
while (m-->0){
flag =true;
int N = sc.nextInt();
int n = sc.nextInt();
int c =sc.nextInt();
int arr[]=new int[n];
for(int i=0;i
}
int dp[]=new int[N+1];
if(N==0){
System.out.println("YES");
continue;
}
dfs(N,n,c,arr,0,0);
if(flag){
System.out.println("NO");
}else{
System.out.println("YES");
}
}
}
public static void dfs(int N,int n,int c,int arr[],int v,int step){
if(step==n){
return;
}
if(v>N)return;
if(v+c>=N){
flag=false;
return;
}
dfs(N,n,c,arr,v+arr[step],step+1);
dfs(N,n,c,arr,v,step+1);
}
}
这个是dfs,听说有的人剪到了100%,
第二题:100%
package org.example;
import java.util.Arrays;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
int m ;
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
while (m-- > 0) {
int k = sc.nextInt();
int[]b1=new int[k];
int[] b2=new int[k];
for(int i=0;i
}
for(int i=0;i
}
int[]a1=new int[k];
int[] a2=new int[k];
for(int i=0;i
a2[i]=b2[k-1-i];
}
boolean flag=work(b1,b2,k);
if(flag){
System.out.println("YES");
continue;
}
flag=work(a1,a2,k);
if(flag){
System.out.println("YES");
continue;
}
System.out.println("NO");
}
}
private static boolean work(int[] a, int[] b, int n) {
boolean tip = true;
int c[]=new int[n];
for(int i =0;i
c[i]=Math.min(a[i],b[i]);
}else{
if(a[i]
break;
}else if(a[i]>=c[i-1]&&b[i]>=c[i-1]){
c[i]=Math.min(a[i],b[i]);
}else if(a[i]>=c[i-1]){
c[i]=a[i];
}else if (b[i]>=c[i-1]){
c[i]=b[i];
}
}
}
return tip;
}
}
左边扫一下,右边扫一下,就相当于一个升序的一个降序的,可以进行模拟
然后贴一下别人ak的链接:https://www.nowcoder.com/discuss/666334391592910848?sourceSSR=users
欢迎大家和我分享第一题的解法~~~,和你的疑惑
#牛客创作赏金赛##软件开发笔面经#(求生:这个不是zuobi,已经比赛完了,球球不要把我关进小黑屋)
全部评论
第一题背包啊
相关推荐