Solving 2020 Blue Bridge Cup Java Group B Provincial Contest Problems
Problem Statement:
Calculate how many digit '2's are needed to create doorplates numbered from 1 to 2020. Each digit is counted separately (e.g., doorplate 1017 contains one '0', two '1's, and one '7').
Solution:
public class DigitCounter {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 2020; i++) {
int current = i;
while(current > 0) {
if(current % 10 == 2) {
count++;
}
current /= 10;
}
}
System.out.println(count);
}
}
Problem E: String Sorting Analysis
Answer: jonmlkihgfedcba
Approach:
Analyze the maximum number of swaps needed for different string lengths. For length 15, the maximum is 105 swaps. Position the sixth character at the beginning.
public static int countSwaps(String str) {
int swaps = 0;
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length - 1; i++) {
for (int j = 0; j < chars.length - 1 - i; j++) {
if(chars[j] > chars[j + 1]) {
char temp = chars[j];
chars[j] = chars[j + 1];
chars[j + 1] = temp;
swaps++;
}
}
}
return swaps;
}
Problem F: Exam Score Statistics
Problem:
Calculate maximum, minimum, and average scores from a list of exam results.
import java.util.Arrays;
import java.util.Scanner;
public class ScoreAnalyzer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] scores = new int[n];
double total = 0;
for(int i = 0; i < n; i++) {
scores[i] = sc.nextInt();
total += scores[i];
}
Arrays.sort(scores);
System.out.println(scores[n-1]);
System.out.println(scores[0]);
System.out.printf("%.2f", total/n);
}
}
Problem G: Letter Frequency Analysis
Problem:
Find the most frequent letter in a word (with alphabetical tie-breaker) and its count.
import java.util.Scanner;
public class LetterAnalyzer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] frequency = new int[26];
String word = sc.next();
for (int i = 0; i < word.length(); i++) {
frequency[word.charAt(i) - 'a']++;
}
int maxCount = 0;
for (int count : frequency) {
if(count > maxCount) {
maxCount = count;
}
}
for (int i = 0; i < frequency.length; i++) {
if(frequency[i] == maxCount) {
System.out.println((char)('a' + i));
break;
}
}
System.out.println(maxCount);
}
}
Problem H: Number Triangle Path
Problem:
Find the maximum sum path through a number triangle with left-right movement constraints.
import java.util.Scanner;
public class TrianglePath {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int[][] triangle = new int[size + 1][size + 1];
for(int row = 1; row <= size; row++) {
for(int col = 1; col <= row; col++) {
triangle[row][col] = sc.nextInt();
}
}
for(int row = 1; row <= size; row++) {
for(int col = 1; col <= row; col++) {
triangle[row][col] += Math.max(triangle[row-1][col-1], triangle[row-1][col]);
}
}
if(size % 2 == 0) {
System.out.println(Math.max(triangle[size][size/2], triangle[size][size/2 + 1]));
}
else {
System.out.println(triangle[size][size/2 + 1]);
}
}
}
Problem I: Substring Dsitinct Count Sum
Problem:
Calculate the sum of distinct character counts for all possible substrings.
import java.util.HashSet;
import java.util.Scanner;
public class SubstringAnalyzer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.next();
long total = 0;
for(int i = 0; i < input.length(); i++) {
for(int j = i; j < input.length(); j++) {
total += countDistinct(input.substring(i, j + 1));
}
}
System.out.println(total);
}
private static int countDistinct(String s) {
HashSet<character> unique = new HashSet<>();
for(char c : s.toCharArray()) {
unique.add(c);
}
return unique.size();
}
}</character>