Source of title:
Blue Bridge Cup Exercise System (blog date is 2019.3.23, so maybe when readers see it, they update new questions)
Here is my solution code for each question, for reference only. There will be no ideas and details for problem solving. If necessary, please leave a message to me. I will reply in the message area. The VIP topic originates from dotcpp (the order is the same as the exercise system, but I don't have vip, so I read and solve the questions on the dotcpp website).
Screenshots of test questions:
Introduction training:
1-1 BEGIN-1 A+B problem
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a+b);
}
}
1-2 BEGIN-2 Sequence summation
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
double n = in.nextInt();
double s = (1 + n) / 2;
double t = s * n;
System.out.println((long)t);
}
}
1-3 BEGIN-3 Area of circle
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
final double PI = 3.14159265358979323;
double a = in.nextInt();
double c = a*a*PI;
System.out.printf("%.7f", c);
}
}
1-4 BEGIN-4 Fibonacci sequence
public class Main {
public static void main(String[] ars){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n < 3){
System.out.println(1);
}else {
int[] a = new int[n + 1];
a[1] = 1 % 10007;
a[2] = 1 % 10007;
for (int i = 3; i <= n; i++) {
a[i] = (a[i - 1] + a[i - 2]) % 10007;
}
System.out.println(a[n]);
}
}
}
Basic exercises:
2-1 BASIC-1 Leap year judgement
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int y = in.nextInt();
if((y%4 == 0 && y%100 != 0) || (y%400 == 0)){
System.out.println("yes");
}else {
System.out.println("no");
}
}
}
2-2 BASIC-2 01 string
public class Main {
public static void main(String[] args){
for(int i=0; i<32; i++) {
String str = Integer.toBinaryString(i);
System.out.printf("%05d", Integer.valueOf(str));
System.out.println();
}
}
}
2-3 BASIC-3 Letter graphics
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
String s = "BCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder str = new StringBuilder(s);
str = str.reverse();
str.append("A");
str.append(s);
int a = str.indexOf("A");
int b = a+m;
for(int i=0; i<n; i++){
System.out.println(str.substring(a, b));
a--;
b--;
}
}
}
2-4 BASIC-4 Sequence characteristics
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] s = new int[n];
for(int i=0; i<n; i++){
s[i] = in.nextInt();
}
int max = s[0];
int min = s[0];
int sum = 0;
for(int j=0; j<s.length; j++){
if(s[j] > max){
max = s[j];
}
if(s[j] < min){
min = s[j];
}
sum = sum + s[j];
}
System.out.println(max);
System.out.println(min);
System.out.println(sum);
}
}
2-5 BASIC-5 Lookup integer
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] s = new int[n];
for(int i=0; i<n; i++){
s[i] = in.nextInt();
}
int a = in.nextInt();
in.close();
int count = 0;
for(int j=0; j<s.length; j++){
if(s[j] == a){
count = j+1;
break;
}
}
if(count == 0){
System.out.println(-1);
}else {
System.out.println(count);
}
}
}
2-6 BASIC-6 Yang Hui Triangle
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] a = new int[n][n];
if(n == 1){
System.out.println(1);
}else{
a[0][0] = 1;
for(int i=1; i<n; i++){
a[i][0] = 1;
a[i][i] = 1;
if(n != 2){
for(int j=1; j<=i-1; j++){
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
}
}
// display
for(int i=0; i<n; i++){
for(int j=0; j<=i; j++){
System.out.print(a[i][j]);
if(i!=j){
System.out.print(' ');
}
}
System.out.println();
}
}
}
2-7 BASIC-7 Special numbers
public class Main {
public static void main(String[] args){
for(int dd=100; dd<=999; dd++){
String data = dd + "";
char d0 = data.charAt(0);
char d1 = data.charAt(1);
char d2 = data.charAt(2);
double sum1 = Math.pow((d0-'0'), 3) + Math.pow((d1-'0'), 3) + Math.pow((d2-'0'), 3);
if((int)sum1 == dd){
System.out.println(dd);
}
}
}
}
2-8 BASIC-8 Palindrome number
public class Main { public static void main(String[] args){ StringBuilder a = new StringBuilder(); for(int i=1; i<=9; i++){ for(int j=0; j<=9; j++){ a.append(i); a.append(j); a.append(j); a.append(i); System.out.println(a.toString()); a.delete(0, a.length()); } } } }
2-9 BASIC-9 Special Palindrome Number
2-9-1 Solution I
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=10000; i<=999999; i++){
String data = "" + i;
int sum = 0;
for(int k=0; k<data.length(); k++){
sum = sum + (data.charAt(k)-'0');
}
int len = data.length();
int count = len/2;
// Calculate the same number, if 2, 3 satisfies
StringBuilder flag = new StringBuilder();
for(int j=0; j<count; j++){
if(data.charAt(j) == data.charAt(len-j-1)){
flag.append(1);
}else{
flag.append(0);
}
}
if((flag.toString().equals("111") || flag.toString().equals("11")) && (n == sum)){
System.out.println(i);
}
else{
continue;
}
}
}
}
2-9-2 Solution II
import java.util.Scanner;
public class Main {
public static void f5(int n){
/* Five-digit processing code */
StringBuilder anser1 = new StringBuilder();
// Test the value of bit 1
for(int i=1; i<=9; i++){
// Test the value of bit 2
for(int j=0; j<=9; j++){
// Calculate the value of bit 3
int third_number = n - 2*i - 2*j;
if(third_number>=0 && third_number<=9){
anser1.append(i);
anser1.append(j);
anser1.append(third_number);
anser1.append(j);
anser1.append(i);
System.out.println(anser1);
anser1.delete(0, anser1.length());
}else{
continue;
}
}
}
}
public static void f6(int n){
/* Six-digit processing code */
StringBuilder anser2 = new StringBuilder();
// temp: The value of the first three additions
int temp = n/2;
// Test the value of bit 1
for(int i=1; i<=9; i++){
// Test the value of bit 2
for(int j=0; j<=9; j++){
// Calculate the value of bit 3
int third_number2 = temp-i-j;
if(third_number2>=0 && third_number2<=9){
anser2.append(i);
anser2.append(j);
anser2.append(third_number2);
anser2.append(third_number2);
anser2.append(j);
anser2.append(i);
System.out.println(anser2);
anser2.delete(0, anser2.length());
}else{
continue;
}
}
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
f5(n);
// Even numbers can be six bits.
if(n%2==0){
f6(n);
}
}
}
2-10 BASIC-10 Decimal to hexadecimal
import java.util.Scanner;
public class Main {
public static char f(long a){
char temp = ' ';
if(a<=9){
temp = (char)(a+48);
}else {
switch ((int) a) {
case 10:
temp = 'A';
break;
case 11:
temp = 'B';
break;
case 12:
temp = 'C';
break;
case 13:
temp = 'D';
break;
case 14:
temp = 'E';
break;
case 15:
temp = 'F';
break;
}
}
// System.out.println(temp);
return temp;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
long a = in.nextLong();
StringBuilder ss = new StringBuilder();
if(a == 0){
ss.append(0);
}else {
while (a != 0) {
long number = a % 16;
char s = f(number);
ss.append(s);
a = a / 16;
}
}
System.out.println(ss.reverse().toString());
}
}
2-11 BASIC-11 Hexadecimal to decimal
import java.util.*;
public class Main {
public static int f(char a){
int x=0;
if(a>='0' && a<='9'){
x = a-'0';
}else{
switch (a){
case 'A': x = 10;break;
case 'B': x = 11;break;
case 'C': x = 12;break;
case 'D': x = 13;break;
case 'E': x = 14;break;
case 'F': x = 15;break;
default:break;
}
}
return x;
}
public static void h(String str){
int len = str.length();
double x = 0;
for(int i=0; i<len; i++){
int value = f(str.charAt(i));
x = value*Math.pow(16, len-1-i) + x;
}
System.out.println((long)x);
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String a = in.next();
in.close();
h(a);
}
}
2-12 BASIC-12 Hexadecimal to octal
import java.util.Scanner;
public class Main {
public static String f(String s){
int len = s.length();
StringBuilder str = new StringBuilder();
for(int i=0; i<len; i++){
switch (s.charAt(i)){
case '0': str.append("0000");break;
case '1': str.append("0001");break;
case '2': str.append("0010");break;
case '3': str.append("0011");break;
case '4': str.append("0100");break;
case '5': str.append("0101");break;
case '6': str.append("0110");break;
case '7': str.append("0111");break;
case '8': str.append("1000");break;
case '9': str.append("1001");break;
case 'A': str.append("1010");break;
case 'B': str.append("1011");break;
case 'C': str.append("1100");break;
case 'D': str.append("1101");break;
case 'E': str.append("1110");break;
case 'F': str.append("1111");break;
default: break;
}
}
return str.toString();
}
public static void g(String str){
int len = str.length();
StringBuilder a = new StringBuilder();
for(int i=0; i<len; i+=3){
int temp = (str.charAt(i)-'0')*4+(str.charAt(i+1)-'0')*2+(str.charAt(i+2)-'0');
a.append(temp);
}
System.out.println(a.toString().replaceFirst("^0*", ""));
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] a = new String[n];
for(int i=0; i<n; i++){
a[i] = in.next();
}
in.close();
for(int j=0; j<n; j++){
String str2 = f(a[j]);
int len2 = str2.length();
if(len2 % 3 == 1){str2 = "00" + str2;}
else if(len2 % 3 == 2){str2 = "0" + str2;}
g(str2);
}
}
}
2-13 BASIC-13 Sequence ranking
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int i=0; i<n; i++){
a[i] = in.nextInt();
}
for(int j=0; j<n-1; j++){
for(int k=j+1; k<n; k++){
if(a[j]>a[k]){
int tt = a[k];
a[k] = a[j];
a[j] = tt;
}
}
}
for(int j=0; j<n; j++){
System.out.print(a[j]);
if(j != n-1){
System.out.print(" ");
}
}
}
}
Unfinished... (Updated on March 23, 2019)