9.14携程笔试后端
第一题算面积的 import java.util.Scanner; public class first { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(),k = sc.nextInt(); double v = n+0D; if(k==1){ System.out.printf("%.2f%n",v*v/2); return; } int x = k>>1; int y = k-x; double res = (v*v)/((x+1)*(y+1)); System.out.printf("%.2f%n",res); } }
2.ab矩阵最小交换次数
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Scanner; public class second { static int[][] dist = {{-1,0},{1,0},{0,-1},{0,1}}; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int[][] a = new int[][]{{sc.nextInt(),sc.nextInt()},{sc.nextInt(),sc.nextInt()}}; int[][] b = new int[][]{{sc.nextInt(),sc.nextInt()},{sc.nextInt(),sc.nextInt()}}; if(cout(a)!=cout(b)){ bw.write("-1"); continue; } int time = Integer.MAX_VALUE>>1; for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { time = Math.min(getTime(a, b,j,k),time); } } if(time>=Integer.MAX_VALUE>>1) bw.write("-1"); else bw.write(Integer.toString(time)); if(i<t-1) bw.newLine(); } bw.flush(); } private static int cout(int[][] b) { int res = 0; for (int[] ints : b) { for (int anInt : ints) { if(anInt==1)res++; } } return res; } private static int getTime(int[][] a, int[][] b, int x, int y) { if(isVaild(a,b)) return 0; if(x<0||x>=2||y<0||y>=2) return Integer.MAX_VALUE>>1; int res = Integer.MAX_VALUE>>1; for (int[] ints : dist) { swap(x,y,x+ints[0],y+ints[1],a); res = Math.min(res,getTime(a,b,(y==1?x+1:x),(y==1?0:y+1))+1); swap(x,y,x+ints[0],y+ints[1],a); } return res; } private static void swap(int x, int y, int x1, int y1, int[][] a) { if(x1<0||x1>1||y1<0||y1>1)return; int k = a[x][y]; a[x][y] = a[x1][y1]; a[x1][y1] = k; } private static boolean isVaild(int[][] a, int[][] b) { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if(a[i][j]!=b[i][j]) return false; } } return true; } }
import java.io.*; public class fourth { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { int n = Integer.parseInt(br.readLine()); String[] arr = br.readLine().split(" "); String s = br.readLine(); int rmax = 0,rmin = Integer.MAX_VALUE; int bmax = 0,bmin = Integer.MAX_VALUE; for (int j = 0; j < n; j++) { if(s.charAt(j)=='R'){ rmax = Math.max(Integer.parseInt(arr[j]),rmax); rmin = Math.min(Integer.parseInt(arr[j]),rmin); }else { bmax = Math.max(Integer.parseInt(arr[j]),bmax); bmin = Math.min(Integer.parseInt(arr[j]),bmin); } } int max = Math.max(rmax - rmin, bmax - bmin); max = Math.max(max,rmax-bmin); bw.write(Long.toString(max)); bw.newLine(); } bw.flush(); } }
第4题n*n矩阵最大权值
import java.util.Scanner; public class third { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextInt(); long u = n*n;int mod = (int)1e9+7; long len =( u-(n-1)*4)%mod; long res = (((2*u-len+1))%mod*len*2)%mod; long le = u - len - 4; long prenum = (u-len+5); if((le&1)==1){ prenum = (prenum>>1)%mod; res = (res+(prenum*(le%mod)*3)%mod)%mod; }else { prenum = prenum%mod; le = (le>>1)%mod; } res = (res+20+(prenum*(le%mod)*3)%mod)%mod; System.out.println(res); } }