Question A: Calculation of the week【Fill in the blank】
Answer: 7
Analysis: directly take the remainder of the given number, and then directly add 6 (note: you can’t directly make 20^22+6 and then take the remainder of 7, this is wrong, this 6 can be regarded as already taken The remainder is over.) If you take the remainder directly, you can use Math.pow (number, power) that comes with java, or use BigInteger (the difference between BigInteger and int is that the range of the former is not limited, so it can be used directly).
Implementation code:
Method 1: Use BigInteger solution
public class one {
public static void main(String[] args) {
BigInteger bigInteger = BigInteger.valueOf(20).pow(22).mod(BigInteger.valueOf(7));
int ha = (bigInteger.intValue()+6)%7;
System.out.println(ha);
}
}
Method 2: Use Math.pow() directly
public static void main(String[] args) {
System.out.println(Math.pow(20, 22)%7+6);
}
Question B: Mountain【Fill in the blank】
Answer: 3138
Analysis: By analyzing this question, although a range is given, the test is still to judge whether a number is a palindrome and the first half is non-decreasing. After we implement the method of judging palindrome and non-decreasing, we pass a The for loop can find the result. Because the amount of data is relatively large, it is time-consuming, about one minute faster.
Implementation code:
Method 1: Judging whether a palindrome string uses a double pointer to judge:
public class two {
public static void main(String[] args) {
int count = 0;
for(int i =2022;i<=2022222022;i++) {
if(check(i) && isHuiWen(i)) {
count++;
}
}
System.out.println(count);
}
private static boolean check(int num) {
String s = num+"";
for(int i =0;i<s.length()/2;i++) {
if(s.charAt(i)>s.charAt(i+1)) return false;
}
return true;
}
private static boolean isHuiWen(int num) {
String s = num+"";
for(int i =0,r=s.length()-1;i<r;i++,r--) {
if(s.charAt(i) != s.charAt(r)) return false;
}
return true;
}
}
Method 2: Use StringBuilder.reverse().toString.equals() to judge.
Here is the difference between the three usages:
public class two {
public static void main(String[] args) {
int count = 0;
for(int i =2022;i<=2022222022;i++) {
if(check(i) && isHuiWen1(i)) {
count++;
}
}
System.out.println(count);
}
private static boolean check(int num) {
String s = num+"";
for(int i =0;i<s.length()/2;i++) {
if(s.charAt(i)>s.charAt(i+1)) return false;
}
return true;
}
private static boolean isHuiWen1(int num) {
String snum = num+"";
StringBuilder stringBuilder = new StringBuilder(snum);
return stringBuilder.reverse().toString().equals(snum);
}
}
Test question C: Character statistics [code question]
Analysis: Method 1: You can record the number of occurrences of each character by defining an array with a capacity of 26, and then find out the character with the largest number of times, and output it in alphabetical order by traversing the for loop.
Supplementary knowledge point:
when typing on the keyboard, the difference between next and nextline, because an empty string may be entered, so nextline is used here to receive.
Code:
public class three {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
int[] arr = new int[26];
String str1 = "";
for(int i =0;i<str.length();i++) {
arr[str.charAt(i)-'A']++;
}
int max = Integer.MIN_VALUE;
for(int i =0;i<26;i++) {
max = Math.max(max, arr[i]);
}
for(int i=0;i<26;i++) {
if(arr[i] == max) {
str1=str1+""+(char)(i+'A');
}
}
System.out.println(str1);
}
}