美团9.13笔试
有些题目忘了,我都没存在IDEA上,哭了
都是重写的不要杠我,我也不记得提交的是啥
1.对称矩阵(大体是这样,不确定对不对,重写的)
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Solution{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int[][] arr=new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j]=sc.nextInt(); } } int up=0; int down=arr.length-1; while(up!=down&&check(up,down,arr)){ down=down/2; } for (int i = 0; i <=down; i++) { for (int j = 0; j < m; j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } } static boolean check(int up,int down,int[][] arr){ while(up<=down){ for(int i=0;i<arr[0].length;i++){ if(arr[up][i]!=arr[down][i]) return false; } up++; down--; } return true; } }
3.n k d
static int dfs(int n,int k,int d,boolean flag){ if(n==0&&flag) return 1; if(n<0) return 0; int res=0; for(int i=1;i<=k;i++){ if(i>=d) flag=true; res+=dfs(n-i,k,d,flag); } return res; }
5.齿轮 只是题目长,思路挺简单的
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Solution{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); List<String> list=new ArrayList<>(); while(sc.hasNext()){ String temp=sc.nextLine(); String str=sc.nextLine(); list.add(str); } for(String s:list){ int res=1; for(int i=0;i<s.length();i++){ res+=count(s,i); } System.out.println(res); } } static int count(String str,int index){ if(index==0){ if(str.charAt(0)=='Z'||(str.length()>=2&&str.charAt(1)=='A')){ return 0; }else{ return 1; } }else if(index==str.length()-1){ if(str.charAt(index)=='Z'||(str.length()-2>=0&&str.charAt(index-1)=='A')){ return 0; }else{ return 1; } }else { int res=0; if(str.charAt(index)=='Z') return 0; if(str.charAt(index-1)!='A') res++; if(str.charAt(index+1)!='A') res++; return res; } } }