目录
基础知识点
数对结构
BigInteger
split()
题
1.商品库存管理 - 蓝桥云课
2.回文字符串 - 蓝桥云课
3.握手问题 - 蓝桥云课
基础知识点
数对结构
Java中类似C++大的pair,自定义
public class Pair<A, B> {private final A first;private final B second;public Pair(){}public Pair(A first, B second) {this.first = first; this.second = second;}
}
BigInteger
1.创建
● 从字符串创建:适用于较大的数字或直接从文本输入获取的数字
BigInteger bigInt = new BigInteger("12345678901234567890");
● 从字节数组创建
byte[] bytes = {0, 1, 2, 3, 4};
BigInteger bigInt = new BigInteger(bytes);
● 从长整型值创建
BigIntger bigInt = BigInteger.valueOf(123456789L);
2.基本运算
● 加法(add):两个BigInteger的和
BigInteger sum = a.add(b);
● 减法(subtract):两个BigInteger的差
BigInteger diff = a.subtract(b);
● 乘法(multiply):两个BigInteger的积
BigInteger product = a.multiply(b);
● 除法(divide):两个BigInteger的商
BigInteger quotient = a.divide(b);
● 取模(mod):计算一个BigInteger对另一个BigInteger的模
BigInteger reaminder = a.mod(b);
● 幂运算(pow):计算BigInteger的幂次方
BigInteger power = a.pow(100); // 计算a的100次方
3.比较操作
● 比较大小(compareTo):返回-1 0 1 分别表示 小于、等于或大于
int result = a.compareTo(b);
// -1 小于
// 0 等于
// 1 大于
● 检查是否为零(signum):返回-1 0 1 分别表示负数、零或正数
int sign = a.signum();
其他实用方法
● 最大公约数(gcd):返回两个BigInteger的最大公约数
BigInteger gcd = a.gcd(b);
● 绝对值(abs):返回当前BigInteger的绝对值
BigInteger absValue = a.abs();
● 取反(negate):返回当前BigInteger的相反数
BigInteger n = a.negate();
● 转换为字符串或其他类型(toString,intValue,longValue等):将BigInteger转换为其他形式以便于显示或进一步处理
String strRepresentation = bigInt.toString();
split()
split()方法允许根据指定的分隔符将一个字符串分割成多个子字符串,并将这些子字符串存储在一个字符串数组中返回
一个参数:
public String[] split(String regex)
两个参数:
public String[] split(String regex, int limit)
- 根据给定的正则表达式作为分隔符来分割字符串
- limit控制模式应用的次数,从而影响分割后的数组长度
- 如果limit为n,则模式最多被应用n-1次,因此数组的最大长度为n
- 如果limit为0或负数,则不限制分割次数,且会去除尾部的空字符串
String text = "one,two,three,four,five";
String[] parts = text.split(",", 3);
for (String part : parts) {System.out.println(part);
}
/*
one
two
three,four,five
*/
题
1.商品库存管理 - 蓝桥云课
import java.util.*;public class Main {static int[] difs = new int[1000000]; // 差分数组public static void insert(int l, int r, int c) {difs[l] += c;difs[r+1] -= c;}public static void main(String[] args) {Scanner scan = new Scanner(System.in);int n = scan.nextInt();int m = scan.nextInt();int[] nums = new int[n+1];int[][] ops = new int[m+1][2]; // 操作区间for(int i = 1; i <= m; i++) {int l = scan.nextInt();int r = scan.nextInt();insert(l, r, 1);ops[i][0] = l;ops[i][1] = r; } // 对差分数组进行前缀和计算得到所有操作后的商品状态int[] pres = new int[n+1];int cur = 0;pres[0] = 0;for(int i = 1; i <= n; i++) {cur += difs[i];pres[i] = cur;}// 计算所有操作完毕后,库存为1的商品// cnt[i] 记录的是前i个库存为1的商品数量 也就是从1到i-1个// 所以数组长度是n+2int[] cnt1 = new int[n+2];for(int i = 1; i <= n+1; i++) {if(pres[i-1] == 1)cnt1[i] = cnt1[i-1] + 1;elsecnt1[i] = cnt1[i-1] + 0;// cnt[i] = cnt1[i-1] + (pre[i-1] == 1 ? 1 : 0);}// 统计所有操作都进行后库存仍然为0的商品数量// 因为你所有的操作后都为0了,撤销某个操作更加是0int cnt0 = 0;for(int i = 1; i <= n; i++){if(pres[i] == 0)cnt0++;}for(int i = 1; i <= m; i++){int l = ops[i][0];int r = ops[i][1];int newCnt = cnt1[r+1] - cnt1[l];System.out.println(newCnt + cnt0);}scan.close();}
}
2.回文字符串 - 蓝桥云课
import java.util.Scanner;public class Main {public static boolean isH(String str) {int i = 0, j = str.length()-1;while (i < j) {if (str.charAt(i++) != str.charAt(j--))return false;}return true;}public static void main(String[] args) {Scanner scan = new Scanner(System.in);//在此输入您的代码...int n = scan.nextInt();for (int j = 0; j < n; j++) {String str = scan.next();if (isH(str)) {System.out.println("Yes");continue;}int l = 0, r = str.length()-1;for (int i = 0; i < str.length(); i++) {if (str.charAt(i) == 'l' || str.charAt(i) == 'q' || str.charAt(i) == 'b')l++;elsebreak;}for (int i = str.length()-1; i >= 0 ; i--) {if (str.charAt(i) == 'l' || str.charAt(i) == 'q' || str.charAt(i) == 'b')r--;elsebreak; //tqnzlqbqql}if(l > r) {System.out.println("Yes");continue;}String tmp = str.substring(l, r+1);if (isH(tmp))System.out.println("Yes");elseSystem.out.println("No");}scan.close();}}
思考问题不全面,只知道看样例来解决问题,那样例以外的呢?要找到一般规律
应该是找到第一个和最后一个不是lqb的字符位置,然后判断剩下的是不是回文字符串,剩下的也是的话那就可以进行转换,剩下的不是那没办法咯,因为剩下的字符串不是lqb三者之中的,是怎么也构不成回文字符串的。
3.握手问题 - 蓝桥云课
public class Main {public static void main(String[] args) {int sum = 0;// 自己不能与自己握手,所以是42for(int i = 1; i <= 42; i++)sum += i;sum += 7*43;System.out.println(sum);}
}
可以这么想,7个人彼此之间不握手,那么剩下的43个人之间都要握手,然后7个人在分别与这43个人握手。
首先假设五十个人都可以握手,是个完全连通图:50 * (50-1) / 2 = 1225
然后七个人之间不能握手,减去这七个人所构成的完全连通图的边:7 * (7-1) / 2 = 21
所以一共进行:1225-21=1204 次