第十届蓝桥杯Java B组
试题B
答案:100
注意Java的substring的两个参数为起始的下标和结尾的下标+1,所以j = i + 1防止出现空字符串
import java.math.*; import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = "0100110001010001"; int len = s.length(); Set<String> se = new HashSet<>(); for(int i = 0; i < len; i++) { for(int j = i + 1; j <= len; j++) { String tmp = s.substring(i, j); se.add(tmp); } } System.out.println(se.size()); } }
试题C
答案:4659
import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] a = new int[30000000]; a[1] = 1; a[2] = 1; a[3] = 1; for(int i = 4; i <= 20190324; i++) { a[i] = ((a[i-1] + a[i-2])%10000 + a[i-3])%10000; } System.out.println(a[20190324]); } }
试题D
答案:40785
import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int ans = 0; for(int i = 1; i <= 2019; i++) { for(int j = i + 1; j <= 2019; j++) { for(int k = j + 1; k <= 2019; k++) if(check(i) && check(j) && check(k) && (i + j + k) == 2019) ans++; } } System.out.println(ans); } public static boolean check(int x) { while(x > 0) { if(x % 10 == 2 || x % 10 == 4) return false; x /= 10; } return true; } }
试题E
答案
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
import java.math.*; import java.util.*; class node { int x,y; String ans; node(int x,int y, String ans) { this.x = x; this.y = y; this.ans = ans; } } public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = "01010101001011001001010110010110100100001000101010" + "00001000100000101010010000100000001001100110100101" + "01111011010010001000001101001011100011000000010000" + "01000000001010100011010000101000001010101011001011" + "00011111000000101000010010100010100000101100000000" + "11001000110101000010101100011010011010101011110111" + "00011011010101001001001010000001000101001110000000" + "10100000101000100110101010111110011000010000111010" + "00111000001010100001100010000001000101001100001001" + "11000110100001110010001001010101010101010001101000" + "00010000100100000101001010101110100010101010000101" + "11100100101001001000010000010101010100100100010100" + "00000010000000101011001111010001100000101010100011" + "10101010011100001000011000010110011110110100001000" + "10101010100001101010100101000010100000111011101001" + "10000000101100010000101100101101001011100000000100" + "10101001000000010100100001000100000100011110101001" + "00101001010101101001010100011010101101110000110101" + "11001010000100001100000010100101000001000111000010" + "00001000110000110101101000000100101001001000011101" + "10100101000101000000001110110010110101101010100001" + "00101000010000110101010000100010001001000100010101" + "10100001000110010001000010101001010101011111010010" + "00000100101000000110010100101001000001000000000010" + "11010000001001110111001001000011101001011011101000" + "00000110100010001000100000001000011101000000110011" + "10101000101000100010001111100010101001010000001000" + "10000010100101001010110000000100101010001011101000" + "00111100001000010000000110111000000001000000001011" + "10000001100111010111010001000110111010101101111000"; int[][] a = new int[30][50]; int[][] vis = new int[30][50]; int len = s.length(); for(int i = 0; i < len; i++) { a[i/50][i%50] = s.charAt(i) - '0'; } String ans = ""; int x = 0, y = 0; vis[0][0] = 1; int[][] k = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}}; node tmp = new node(0, 0 , ""); Queue<node> q= new LinkedList<>(); q.offer(tmp); while(!q.isEmpty()) { node t = q.poll(); if(t.x == 29 && t.y == 49) { System.out.println(t.ans); break; } for(int i = 0; i <= 3; i++) { int tmpx = t.x + k[i][0], tmpy = t.y + k[i][1]; if(tmpx >= 0 && tmpx < 30 && tmpy >= 0 && tmpy < 50 && vis[tmpx][tmpy] == 0 && a[tmpx][tmpy] == 0) { vis[tmpx][tmpy] = 1; String st = t.ans; if(i == 0) st += 'D'; if(i == 1) st += 'L'; if(i == 2) st += 'R'; if(i == 3) st += 'U'; node t2 = new node(tmpx, tmpy, st); q.offer(t2); } } } } }
试题F
import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int ans = 0; for(int i = 1; i <= n; i++) { if(check(i)) ans += i; } System.out.println(ans); } public static boolean check(int x) { while(x > 0) { if(x % 10 == 2 || x % 10 == 0 || x % 10 == 1 || x % 10 == 9) return true; x /= 10; } return false; } }
试题G
import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Map<Integer, List<Integer>> mp = new HashMap<>(); int n = sc.nextInt(); int m = sc.nextInt(); int t = sc.nextInt(); int[] sum = new int[n+1]; int[] cnt = new int[n+1]; for(int i = 0; i < m; i++) { int tmpt = sc.nextInt(); int tmpid = sc.nextInt(); if(mp.get(tmpt) == null) { List<Integer> tmpl = new ArrayList<>(); tmpl.add(tmpid); mp.put(tmpt, tmpl); } else mp.get(tmpt).add(tmpid); } int last = 0, ans = 0; for(Integer key : mp.keySet()) { int key1 = key; List<Integer> tmpL = mp.get(key1); int[] vis = new int[n+1]; int timespan = key1 - last; for(int j : tmpL) vis[j] += 1; for(int j = 1; j <= n; j++) { if(vis[j] > 0) sum[j] += 2 * vis[j]; else sum[j] = sum[j] - timespan < 0 ? 0 : sum[j] - timespan; if(sum[j] > 5) cnt[j] = 1; else if(sum[j] <= 3) cnt[j] = 0; } last = key1; } for(int i = 1; i <= n; i++) { int timespan = t - last; sum[i] = sum[i] - timespan < 0 ? 0 : sum[i] - timespan; if(sum[i] <= 3) cnt[i] = 0; } for(int i = 1; i <= n; i++) { if(cnt[i] == 1) ans++; } System.out.println(ans); } }
试题H
import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int K = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); String[] word = s.split("\\s+|\\."); int len = word.length; int ans = 0; for(int i = 0; i < len; i++) { if(word[i].equals("Alice")) { for(int j = i + 1; j < len; j++) { if(word[j].equals("Bob")) { int L = 1; for(int k = i + 1; k < j; k++) L += (1 + word[k].length()); if(L <= K) ans++; } } } } for(int i = 0; i < len; i++) { if(word[i].equals("Bob")) { for(int j = i + 1; j < len; j++) { if(word[j].equals("Alice")) { int L = 1; for(int k = i + 1; k < j; k++) L += (1 + word[k].length()); if(L <= K) ans++; } } } } System.out.println(ans); } }