前言
2024天梯赛备赛记录
1 L1-094 剪切粘贴
解题思路:
对字符串的操作,对给出的前两个数字位置的子串用.substring()方法截取并保存,同时用.delete()方法删去字符串中的子串;
查找所给插入位置,若找到,插入;否则插入到串的末尾。
代码示例:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder s = new StringBuilder(); //定义字符串接收
s.append(sc.next());
int n = sc.nextInt();
while(n-->0){
int l = sc.nextInt()-1; //字符串位置从 1 开始编号
int r = sc.nextInt()-1;
String s1 = sc.next();
String s2 = sc.next();
String cx = s.substring(l,r+1);
s.delete(l, r+1);
int index = s.indexOf(s1+s2);
if(index != -1) {
s.insert(index+s1.length(), cx.toCharArray());
}
else {
s.append(cx);
}
}
System.out.println(s);
}
}
2 L1-093 猜帽子游戏
对输入全是数字的题目,尽量使用接收下一个数字
的输入:Scanner.nextInt();
若使用接收一行时会将空格和换行符同时接收。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []c = new int[n];
for(int i = 0; i < n; i ++)
c[i] = sc.nextInt();
int k = sc.nextInt();
while(k-->0) {
int[] g = new int[n];
for(int i = 0; i < n; i ++)
g[i] = sc.nextInt();
int r = 0, w = 0, e = 0;
for(int i = 0; i < c.length; i ++) {
if(c[i] == g[i])
r ++;
else if(g[i] == 0)
e ++;
else
w ++;
}
if(w == 0 && r >= 1)
System.out.println("Da Jiang!!!");
else
System.out.println("Ai Ya");
}
}
}
3 L1-050 倒数第N个字符串
思路:将题目用进制的思想去求解
示例代码:
import java.util.*;
public class Main {
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int n = sc.nextInt();
char []c = new char[27];
//定义字符数组存放26个小写字母,作为每位的表示。
for(int i = 1; i <= 26; i ++) {
c[i] = (char) ('a'+(i-1));
}
StringBuilder s = new StringBuilder(); //对进制求解,即将十进制转换为对应进制时,定义StringBuilder对象以实现“余数倒联级”
long m = (long)Math.pow(26, l); //最大可以表示的十进制数
m=m-n; //倒数第几个,即最大值减去n
while((int)(m/26) != 0) { //除l取余
s.append(c[(int) (m%26)+1]);
m/=26;
}
s.append(c[(int) m+1]); //最后将除数为0时的被除数加入串中
if(s.length() < l) { //位数不足时高位补‘a’
for(int i = 0; i < l-s.length(); i ++){
System.out.print('a');
}
}
System.out.println(s.reverse());
}
}
4 L1-062 幸运彩票
思路正确并且样例可过,但拿不到满分时,去想特例。
本题特例:
当彩票号码为0时,不可用int接收,必须用字符串直接接收。
import java.util.*;
public class Main {
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-->0) {
// int m = sc.nextInt();
StringBuilder s = new StringBuilder();
s.append(sc.next());
int f = 0;
for(int i = 0; i < 3;i ++) {
f+=s.charAt(i)-'0';
}
int e = 0;
for(int i = s.length()-3;i<s.length();i++)
e+=s.charAt(i)-'0';
System.out.print(f == e ?"You are lucky!\n":"Wish you good luck.\n");
}
}
}
5 L2-003 月饼
思路:
先对每种月饼求出单价并进行排序,得到按单价降序排序的序列;对序列进行遍历,求出最大利益。
package code;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); //种类数
int d = sc.nextInt(); //市场最大需求量
double [][]s = new double[n][5];
for(int i = 0; i < n; i ++) {
s[i][0] = sc.nextDouble();
}
for(int i = 0; i < n; i ++) {
double x = sc.nextDouble();
s[i][2] = x;
s[i][1] = x/s[i][0];
}
Arrays.sort(s,(o1,o2)->Double.compare(o2[1], o1[1]));
double sum = 0;
for(int i = 0; i < n; i ++) {
if(d>=s[i][0]) {
sum += s[i][2];
d-=s[i][0];
}
else {
sum += s[i][1]*d;
break;
}
}
System.out.printf("%.2f",sum);
}
}
二维数组的排序
- 二维数组可以由n个一维数组组成。定义二维数组的行数n和列数m,那么数组的每一行由一个长度为m的一维数组构成,且共有n行。
- 对二维数组的排序,就可以实现由其中某一列,对行进行排序。
- Arrays.sort(s,lamda);
当要排序数组元素的做差结果返回不为int,可以用对应包装类的.compare()方法。Arrays.sort(s,(o1,o2) -> Double.compare(o2[1] - o1[1]));