Simulation problem of national college computer ability challenge
1. Count the number of squares between 1 and N (inclusive) and output this number.
Tip: the number of squares. For example, 4 is the square of 2, 16 is the square of 4, and 5 is not the square.
Input Description: an integer n (n < 100000);
Output Description: number of squares
Input example: 50
Output example: 7
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); sc.close(); int count = 0; for (int i = 1; i <= m ; i++) { if(Math.sqrt(i) % 1 == 0){ count++; } } System.out.println(count); } }
2. For the given positive integer array with length n (n < 1000), the interval satisfying that three consecutive elements are composite is called 3-composite interval, and the number of 3-composite intervals in the array is calculated.
Input Description: the first line, the number of elements in the array N, the second line, N positive integers, separated by spaces.
Output Description: number of 3-compound number intervals
Input example: 7
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int array[] = new int[m]; int count = 0; for (int i = 0; i < array.length; i++) { array[i] = sc.nextInt(); } sc.close(); for (int i = 0; i < array.length - 2; i++) { if(is(array[i]) && is(array[i+1]) && is(array[i+2])){ count++; } } System.out.println(count); } static boolean is(int number){ for (int i = 2; i < number ; i++) { if(number % i == 0){ return true; } } return false; } }
3. Look at the letters. Given a string composed of lowercase English letters (length < 1000), if two consecutive letters in the string are the same, the two letters can be eliminated at the same time, and the operation can be repeated until they cannot be eliminated. Please program to determine whether the string can be completely eliminated.
Input Description: a string.
Output Description: if it can be completely eliminated, output "YES". If not, output the eliminated result.
Input example 1: abacddcaba
Output example 1: YES
Input example 2: asdfghhgf
Output example 2: asd
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String string = sc.nextLine(); sc.close(); String result = remove(string); if(result.length() == 0){ System.out.println("YES"); }else{ System.out.println(result); } } public static String remove(String string){ String s = string; for (int i = 0; i < string.length() - 1; i++) { if(string.charAt(i) == string.charAt(i + 1)){ s = s.substring(0,i) + s.substring(i+2,string.length()); } } if(s.equals(string)){ return string; }else { return remove(s); } } }
4. An array composed of N (N < = 10000) integers, in which K (k < = 200) consecutive elements form an interval, which is called K interval. The sum of all prime numbers in a K interval is recorded as Sk. Please calculate the maximum Sk value in all k intervals in the whole array and output it.
Input Description: the first line is two integers N and K, and the second line inputs N numbers to represent the elements in the array.
Output Description: maximum Sk value
Input example: 8 2
12 23 27 34 19 17 45 8
Output example: 36
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); int array[] = new int[N]; for (int i = 0; i < array.length; i++) { array[i] = sc.nextInt(); } sc.close(); int max = 0; for (int i = 0; i < N - K; i++) { int temp = 0; for (int j = i; j < i + K; j++) { if(is(array[j])){ temp = temp + array[j]; max = Math.max(max,temp); } } } System.out.println(max);; } public static boolean is(int number){ for (int i = 2; i < number; i++) { if(number % i == 0){ return false; } } return true; } }
5. There are several new batches of materials in the warehouse. Only the quantity and unit price of each batch of materials are known. Please write a program and output them from high to low according to the total value of each material.
Input Description: an integer N in line 1 indicates the batch quantity of materials
In line 2-N+1, the category, quantity and unit price of each batch of materials are separated by a space, in which the category is distinguished by A-Z.
Output Description: the sorting results are output in descending order of material value, and one material is output in each line.
Input example: 5
A 5 10.00
B 3 2.00
A 5 8.00
B 3 2.50
C 10 3.50
Output example: A 90.00
C 35.00
B 13.50
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); Object [][] array = new Object[5][3]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 3; j++) { array[i][j] = sc.next(); } } sc.close(); Map<String,Double> map = new HashMap<>(); for (int i = 0; i < N; i++) { double sum = Integer.parseInt((String) array[i][1]) * Double.parseDouble((String)array[i][2]); String key = (String) array[i][0]; if(map.get(key) == null){ map.put(key,sum); }else{ double temp = map.get(key); map.put(key,temp + sum); } } System.out.println(map.get("A")); List<Map.Entry<String, Double>> infoIds = new ArrayList<Map.Entry<String, Double>>(map.entrySet()); //Sort by value in descending order Collections.sort(infoIds, new Comparator<Map.Entry<String, Double>>() { @Override public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) { return (int) (o2.getValue() - o1.getValue()); } }); //Note that the traversal here is list, that is, we will map The entry is put into the list and the sorted collection for (Map.Entry s : infoIds) { System.out.println(s.getKey() + " " + String.format("%.2f",s.getValue())); } } }
6. Count the number of all cubes from 1 to N (inclusive) and output this number.
Tip: the number of cubic numbers. For example, 8 is the cubic number of 2, 27 is the cubic number of 3, and 9 is not the cubic number.
Input Description: an integer n (n < 100000);
Output Description: number of cubes
Input example: 200
Output example: 5
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); sc.close(); int count = 0; for (int i = 1; i <= N ; i++) { for (int j = 1; j <= i ; j++) { if(j * j * j == i){ count++; } } } System.out.println(count); } }
7. Count the sum of all non even numbers in the integer interval [n, M] (n, m < 100000) and output this number.
Input Description: two integers N and M;
Output Description: combined number of non even numbers
Input example: 2 16
Output example: 2
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M = sc.nextInt(); sc.close(); int count = 0; for (int i = N; i <= M ; i++) { if(i % 2 != 0){ for(int j = 2;j < i;j++){ if(i % j == 0 ){ count++; break; } } } } System.out.println(count); } }
8. For a given character array (the number of characters is less than 10000), count the occurrence times of characters of letter type, number type and symbol type, where the letter type is the character between English letters a-z (case insensitive); The number type is a character between 0 and 9; Symbol types are characters other than English letters, numbers and spaces.
Input Description: a character sequence;
Output Description: output in three lines: the first line is the letter type, marked with a-z; The number type in the second line is marked with 0-9; The third line is the symbol type, identified by others. The format of each line is as follows:
Number of occurrences of category ID (separated by a space)
Input example: Hello World!
Output example: a-z 10
0-9 0
others 1
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); char[]chars = s.toCharArray(); sc.close(); int count1 = 0; int count2 = 0; int count3 = 0; for (int i = 0; i < chars.length; i++) { int temp = chars[i]; if (temp == 32){ continue; } else if(temp >= 65 && temp <= 122){ count1++; continue; }else if(temp >= 48 && temp <= 57){ count2++; continue; }else { count3++; } } System.out.println("a-z" + " " + count1); System.out.println("0-9" + " " + count2); System.out.println("others " + " " + count3); } }
9. An array composed of N (N < = 10000) integers, in which K (k < = 200) consecutive elements form an interval, which is called K interval. Find the absolute value of the difference between any two numbers in a K interval, and the maximum absolute value is recorded as Dk. Please calculate the maximum Dk value in all k intervals in the whole array and output it.
Input Description: the first line is two integers N and K, and the second line inputs N numbers to represent the elements in the array.
Output Description: maximum Dk value.
Input example: 8 2
12 23 27 34 35 36 8 45
Output example: 37
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); int array [] = new int [N]; for (int i = 0; i < N; i++) { array[i] = sc.nextInt(); } sc.close(); int max = 0; for (int i = 0; i < N - K ; i++) { for (int j = i; j < i + K; j++) { max = Math.abs((array[j] - array[j+1])); } } System.out.println(max); } }
10. Given a legal mathematical expression (length < 1000) containing only 0-9, '+', and '', it is specified that the plus sign '+' has higher priority than the multiplication sign ', please output the calculation result.
Input Description: Legal mathematical expression
Output Description: outputs the calculation result of the expression
Input example: 123 + 122
Output example: 360
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String string = sc.nextLine(); sc.close(); String [] multiplication = string.split("\\*"); int result = 1; for (int i = 0; i < multiplication.length; i++) { String[] add = multiplication[i].split("\\+"); int count = 0; for (int j = 0; j < add.length; j++) { count += Integer.parseInt(add[j]); } result *= count; } System.out.println(result); } }