传智杯赛后复盘
大家好 我是寸铁👊
2023年第六届传智杯程序设计挑战赛(个人赛)B组 赛后复盘
喜欢的小伙伴可以点点关注 💝
1. 字符串拼接
细节:一定要清楚nextLine()
和next()
的区别
nextLine()
是遇到回车会停下来
next
是遇到空格会停下来
很明显这里必须得选nextLine()
踩坑实录…
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str1 = in.nextLine();
String str2 = in.nextLine();
StringBuffer s1 = new StringBuffer(str1);
StringBuffer s2 = new StringBuffer(str2);
s1.append(s2);
System.out.println(s1);
}
}
2. 差值
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建Scanner对象用于接收输入
Scanner in = new Scanner(System.in);
// 输入战士数量
int n = in.nextInt();
int[] strengths = new int[n];
for (int i = 0; i < n; i++) {
strengths[i] = in.nextInt();
}
// 对战士战斗力进行排序 以便比较相邻两位战士的战力之差
Arrays.sort(strengths);
// 初始化最小差值为一个较大的值
int minDif = 0x3f3f3f3f;
// 枚举相邻两名战士战斗力之差的最小值
for (int i = 0; i < n - 1; i++) {
int currentDif = strengths[i + 1] - strengths[i];
//更新战力之差的最小值
if (currentDif < minDif) {
minDif = currentDif;
}
}
System.out.println(minDif);
in.close();
}
}
3. . 红色和紫色
很有趣的一题,奇数和偶数的区别
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();