1. Integer.toString(int i,int radix);
把数i转换成radix进制表示
System.out.println(Integer.toString(8,2));
运行结果:
2. Intger.parseInt(String s,int radix);
System.out.println(Integer.parseInt("2022",9));
3. 用BigInteger求gcd(最大公约数)
System.out.println(new BigInteger("20").gcd(new BigInteger("15")));
4.用BigInteger(val).isProbablePrime(certainty)判断是否是素数
System.out.println(new BigInteger("998244353").isProbablePrime(10));
certainty 值越大,越精确,准确率越高
一般用来判断大数是否是素数,小数也可以
5.用BigInteger(val).nextProbablePrime()判断该数的下一个素数
System.out.println(new BigInteger("998244353").nextProbablePrime());
val也可以不是素数
6.用modPow求快速幂
System.out.println(new BigInteger("2").modPow(new BigInteger("10"),new BigInteger("1000007")));
求2的10次方再对1000007取模
看不懂可参考下面(来源:Java.math.BigInteger.modPow()方法实例 - Java.math包 (yiibai.com))
package com.yiibai;
import java.math.*;
public class BigIntegerDemo {
public static void main(String[] args) {
// create 3 BigInteger objects
BigInteger bi1, bi2, bi3;
// create a BigInteger exponent
BigInteger exponent = new BigInteger("2");
bi1 = new BigInteger("7");
bi2 = new BigInteger("20");
// perform modPow operation on bi1 using bi2 and exp
bi3 = bi1.modPow(exponent, bi2);
String str = bi1 + "^" +exponent+ " mod " + bi2 + " is " +bi3;
// print bi3 value
System.out.println( str );
}
}
//更多请阅读:https://www.yiibai.com/java/math/biginteger_modpow.html
7.用modInverse乘法逆元
不太会用
System.out.println(new BigInteger("2").modInverse(new BigInteger("998244353")));
8.Word文档
查找1-2022中2有几个
遍历出来复制到word文档里,ctrl+F查找
加个小题
将一串字符中的小写字母转换为大写字母。
输入:LanQiao
输出:LANQIAO
方法一:
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
char c[] = new char[s.length()];
for(int i=0;i<s.length();i++) {
int n = (int)s.charAt(i);
if(n>=97&&n<=122) {
n=n-32;
}
c[i]=(char)(n);
}
for(int i=0;i<c.length;i++) {
System.out.print(c[i]);
}
}
}
方法二:
import java.util.*;
public class shuzupingfang {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
for(int i=0;i<s.length();i++) {
char x = s.charAt(i);
if(x>='a'&&x<='z') {
x^=32;
}
else if(x>='A'&&x<='Z') {
x^=32;
}else {
}
System.out.print(x);
}
}
}
2.