2021/8/22 爱奇艺笔试 全AC
1、sql 略
2、峰值-谷值问题
import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while(scanner.hasNext()){ String[] line=scanner.nextLine().split(","); int n=line.length; int[] nums=new int[n]; for(int i=0;i<n;i++){ nums[i]=Integer.parseInt(line[i]); } solve(n,nums); } } private static void solve(int n, int[] nums) { int peek=Math.max(nums[0],nums[1]); int down=Math.min(nums[0],nums[1]); int ans=peek-down; for(int i=2;i<n;i++){ if((nums[i]-nums[i-1])*(nums[i-1]-nums[i-2])>=0){ if(nums[i]>nums[i-1]){ peek=nums[i]; }else{ down=nums[i]; } }else{ if(nums[i]>nums[i-1]){ down=nums[i-1]; peek=nums[i]; }else{ down=nums[i]; peek=nums[i-1]; } } ans=Math.max(peek-down,ans); } System.out.println(ans); } }3、n皇后
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=Integer.parseInt(scanner.nextLine()); boolean[] col=new boolean[n]; boolean[] line1=new boolean[2*n-1]; boolean[] line2=new boolean[2*n-1]; List<String> ans=new ArrayList<>(); dfs(ans,n,new ArrayList<Integer>(),0,col,line1,line2); System.out.print("["); for(int i=0;i<ans.size();i++){ System.out.print(ans.get(i)); if(i!=ans.size()-1){ System.out.print(", "); } } System.out.print("]"); } private static void dfs(List<String> ans,int n, ArrayList<Integer> path, int cnt,boolean[] col,boolean[] line1,boolean[] line2) { if(cnt==n){ // String s="["; for(int i=0;i<n;i++){ Integer index = path.get(i); for(int j=0;j<n;j++){ if(j==index){ s+="Q"; }else{ s+="."; } } if(i!=n-1){ s+=", "; } } s+="]"; ans.add(s); return; } for(int i=0;i<n;i++){ if(col[i]) continue; if(line1[i+cnt]) continue; if(line2[cnt-i+n-1]) continue; col[i]=true; line1[i+cnt]=true; line2[cnt-i+n-1]=true; path.add(i); dfs(ans,n,path,cnt+1,col,line1,line2); col[i]=false; line1[i+cnt]=false; line2[cnt-i+n-1]=false; path.remove(path.size()-1); } } }4、多发问题
import java.util.Scanner; import java.util.concurrent.Semaphore; import java.util.function.IntConsumer; public class Main3 { public static void main(String[] args) { final Scanner reader = new Scanner(System.in); int n = reader.nextInt(); ZeroEvenOdd zeroEvenOdd = new ZeroEvenOdd(n); new Thread(() -> { try { zeroEvenOdd.printZero(System.out::print); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); new Thread(() -> { try { zeroEvenOdd.printEven(System.out::print); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); new Thread(() -> { try { zeroEvenOdd.printOdd(System.out::print); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); } } class ZeroEvenOdd { private int n; Semaphore semaphore1=new Semaphore(0); Semaphore semaphore2=new Semaphore(0); Semaphore semaphore3=new Semaphore(0); public ZeroEvenOdd(int n) { this.n = n; } // printNumber.accept(x) outputs "x", where x is an integer. public void printZero(IntConsumer printNumber) throws InterruptedException { for(int i=0;i<n;i++){ if(i!=0){ semaphore1.acquire(); } printNumber.accept(0); if(i%2==0){ semaphore3.release(); } else{ semaphore2.release(); } } } public void printEven(IntConsumer printNumber) throws InterruptedException { for(int i=0;i<n;i++){ if((i+1)%2==0){ semaphore2.acquire(); printNumber.accept(i+1); semaphore1.release(); } } } public void printOdd(IntConsumer printNumber) throws InterruptedException { for(int i=0;i<n;i++){ if((i+1)%2==1){ semaphore3.acquire(); printNumber.accept(i+1); semaphore1.release(); } } } }