竞赛——【传智杯】2021年第四届“传智杯”全国大学生IT技能大赛(初赛Java)题解
题目洛谷链接(自测ac):https://www.luogu.com.cn/contest/58542#problems
1、组原成绩
题目描述:

测试用例
输入:50 100 100
输出:90
题解代码:
//简单计算+转型 import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); int h = scanner.nextInt(); int e = scanner.nextInt(); int w =(int)( t * 0.2 + h * 0.3 + e * 0.5); System.out.println(w); } }
2、报告赋分
题目描述:

测试用例
输入:
2
70 17
80 10
输出:
70
70
题解代码:
import java.util.Scanner; public class Test2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); //行数 int[][] arr = new int[T][2]; //创建一个存储T行数据的数组 for (int i = 0; i < T; i++) { //遍历输入数据存入数组 int a = scanner.nextInt(); int p = scanner.nextInt(); arr[i][0] = a; arr[i][1] = p; } //计算得分 int count = 0; for (int i = 0; i < T; i++) { //分为大于20,小于16,在[16,20]三种情况计算得分 if(arr[i][1] < 16){ count = arr[i][0] - 10; }else if (arr[i][1] > 20){ count = arr[i][0] - (arr[i][1] - 20); }else{ count = arr[i][0]; } //判断得分如果被扣小于0,最低为0分 if (count < 0){ count = 0; } System.out.println(count); } } }
3、竞争得分
题目描述:

测试用例:
输入:
3
1 2 3
输出:
0 50 100
题解代码:
import java.util.Scanner; public class Test3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] arr = new int[n];//数组用于保存输入数据(原始分) //求最大最小(因为只需得到最大最小,为降低时间复杂度,不冒泡对全部排序) int min = 1001; int max = -1; for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); if(arr[i] > max) max = arr[i]; if(arr[i] < min) min = arr[i]; } //根据公式计算 int count; for (int i = 0; i < n; i++) { count = (100* (arr[i] - min))/(max - min); System.out.print(count + " "); } } }
4、小卡和质数2
题目:

测试用例:
输入:
9
5
6
7
8
9
10
100
1000
10000
输出:
2
4
4
2
2
4
22
163
1132
题解代码:
//质数(又称素数),指在大于1的自然数中除了1和该数自身外,无法被其他自然数整除的数 //java中 ^ 表示异或运算符,两数异或运算得到一个新的数 import java.util.Scanner; public class Test4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long startTime = System.currentTimeMillis(); //获取开始时间 int arr; int[] tol = new int[n]; int count = 0; for (int i = 0; i < n; i++) { arr = sc.nextInt(); for (int j = 0; j < arr; j++) { if (isPrime(arr ^ j) == 1) { count++; } } tol[i] = count; count = 0; } for (int k = 0; k < n; k++) { System.out.println(tol[k]); } } //判断是否为质数的函数(质数:除了1和本身不能被其他整数整除) public static int isPrime(int n){ int i=2; for(;i< n ;i++){ if(n%i==0){ return 0; } } if(n==i){ return 1; } return 0; } }
5、萝卜数据库
题目:

测试样例:
输入
4 5
1 2 1 2 2 4
2 2 1 5
1 2 3 5 4 6
2 4 7 8输出:
1
0
题解代码:
//双数组 + 循环 import java.util.Scanner; public class Test5 { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int n = scanner.nextInt(); //共有n次操作 int k = scanner.nextInt(); //支持k个字段 int[] arr = new int[k + 1]; //存储数值的数组,字段名从1开始,而数组下标从0开始,故容量为K+1 int[] arr1 = new int[n]; //存记录满足查询条件的值的个数的数组,用于统一输出 int number = n; //记录操作次数, int number1 = 0; //用作数组arr1[]的索引 while (number > 0) { int num = scanner.nextInt(); //选择插入或查询操作(1或2) //插入操作 if (num == 1) { int p = scanner.nextInt(); //插入多少个字段 for (int i = 0; i < p; i++) { int x = scanner.nextInt(); //字段名 int y = scanner.nextInt(); //字段值 arr[x] = y; //字段名x值为y } //查询操作 } else if (num == 2) { int count = 0; //统计满足查询条件的个数 int temp = scanner.nextInt(); //要查询的值 int temp1 = scanner.nextInt(); //查询字段名起点min int temp2 = scanner.nextInt(); //查询字段名终点max for (int i = temp1; i <= temp2; i++) { //从[min,max]中查找 if(i < k){ if (arr[i] == temp) { count++; } } } arr1[number1] = count; //存储每次满足查询条件的值的个数 number1++; } number--; //操作次数 } for (int i = 0; i < number1; i++) { //输出查询结果 System.out.println(arr1[i]); } } }
