2020.08.11 晚上 笔试 2h
1)回文串构造
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String num = sc.nextLine(); String str = sc.nextLine(); int ans = 0; int n = Integer.valueOf(num); int left = 0, right = n - 1; while(left < right) { if(str.charAt(left++) != str.charAt(right--)) { ans++; } } System.out.println(ans); } }
2)方格染色
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int[] a = new int[t]; for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m = sc.nextInt(); if(n == 1 && m == 1) { a[i] = 1; continue; } if(n % 2 == 0 || m % 2 == 0) { a[i] = 2; continue; } if(n % 3 == 0 || m % 3 == 0) { a[i] = 3; continue; } if(n == 1 || m == 1) { for (int j = 5; j <= Math.max(n, m); j++) { if(j % 2 == 0 || j % 3 == 0) { continue; } if(n % j == 0 || m % j == 0) { a[i] = j; break; } } continue; } for (int j = 5; j <= Math.min(n, m); j++) { if(j % 2 == 0 || j % 3 == 0) { continue; } if(n % j == 0 || m % j == 0) { a[i] = j; break; } } } for(int num : a) { System.out.println(num); } } }
3)最大子段或
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int res = Integer.MAX_VALUE; int max = 0; for (int i = 0; i < n; i++) { int tmp = 0; for (int j = i; j < n; j++) { tmp = tmp | a[j]; if(tmp >= max) { max = tmp; res = Math.min(res, j - i + 1); } } } System.out.println(res); } }
第一题100%,第二题100%,第三题暴力0%,第四题没做,菜是原罪。
全部评论
(6) 回帖