第二届全国高校计算机技能竞赛——Java赛道
小赛跳高
签到题
import java.util.*;
public class Main{
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
double n = sc.nextDouble();
for(int i = 0; i < 4; i++) {
n = n * 0.9;
}
System.out.printf("%.2f", n);
}
}
找数
签到题
import java.util.Scanner;
public class Main{
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
if((i % 3 == 1) && (i % 5 == 1) && (i % 7 == 1)) {
System.out.print(i + " ");
}
}
}
}
打分
签到题
注意cnt等于0的情况
最大值取-0x3f3f3f3f
最小值取0x3f3f3f3f
注意当cnt = 0时,说明无人打出大于0的分数。
此时选手的分数为0分,注意保留2位小数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int maxsco = -0x3f3f3f3f;
int minsco= 0x3f3f3f3f;
int sum = 0;
int cnt = 0;
while (n-->0) {
int sco = sc.nextInt();
if (sco > 100 || sco < 1) {
continue;
}
sum += sco;
cnt++;
if (sco > maxsco) {
maxsco = sco;
}
if (sco < minsco) {
minsco = sco;
}
}
if(cnt>0) {
System.out.printf("%.2f",(double) (sum - maxsco - minsco) / (cnt - 2));
}
else {
System.out.println(0.00);
}
}
}
找子串
不断截取子串,判断后,找出最大串和最小串
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String minstr = str1;
String maxstr = "";
for (int i = 0; i < str1.length(); i++) {
for (int j = i + 1; j <= str1.length(); j++) {
String substr = str1.substring(i, j);
if (substr.compareTo(minstr) < 0) {
minstr = substr;
}
if (substr.compareTo(maxstr) > 0) {
maxstr = substr;
}
}
}
System.out.println(minstr);
System.out.println(maxstr);
}
}
矩阵距离
宽搜bfs 队列存点对,往上下左右四个方向搜索
注意初始化所有距离数组dis[][]为-1文章来源:https://uudwc.com/A/9d1VD
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Main {
static final int[][] dt = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
scanner.nextLine(); // 读取换行符
char[][] g = new char[n][m];
for (int i = 0; i < n; i++) {
String line = scanner.nextLine();
for (int j = 0; j < m; j++) {
g[i][j] = line.charAt(j);
}
}
bfs(g, n, m);
}
static void bfs(char[][] g, int n, int m) {
int[][] dis = new int[n][m];
Queue<int[]> q = new LinkedList<>();
for(int i = 0; i < n;i++) {
for(int j = 0; j < m;j++) {
dis[i][j] = -1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] == '1') {
dis[i][j] = 0;
q.offer(new int[]{i, j});
}
}
}
while (!q.isEmpty()) {
int[] t = q.poll();
int x = t[0];
int y = t[1];
for (int i = 0; i < 4; i++) {
int dx = x + dt[i][0];
int dy = y + dt[i][1];
if (dx >= 0 && dx < n && dy >= 0 && dy < m && dis[dx][dy] == -1) {
dis[dx][dy] = dis[x][y] + 1;
q.offer(new int[]{dx, dy});
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(dis[i][j] + " ");
}
System.out.println();
}
}
}
总结
难度中下,签到题3道,medium2道。
总体难度没去年难,考查基础思维。
注意代码实现细节和边界情况文章来源地址https://uudwc.com/A/9d1VD