蓝桥杯
一、语法
1.字符串挨个输入
直接输入到数组 toCharArray()
//数组
char[][] map=new char[10][10];
String temp[] = {
"UDDLUULRUL", "UURLLLRRRU", "RRUURLDLRD", "RUDDDDUUUU", "URUDLLRRUU", "DURLRLDLRL",
"ULLURLLRDU", "RDLULLRDDD", "UUDDUDUDLL", "ULRDLUURRR" };
for (int i = 0; i < 10; i++) {
map[i] = temp[i].toCharArray();
System.out.println(map[i]);
}
Scanner scanner = new Scanner(System.in);
String[] s = scanner.next().split("/");
scanner.close();
String a = s[0];
String b = s[1];
String c = s[2];
输出为:
UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR
2.数组
数组:
double [][] list={
{
180.90,0.88}};
3.判断素数
public static boolean isPrime(int num){
for(int i = num-1;i>1;i--){
if(num%i==0) //大除小
return false;
}
return true;
}
注意Boolean拼写
4.Java 的输入
Scanner scan=new Sacnner(System.in);
a=scan.nextInt();
5.ArrayList数组队列
ArrayList 是一个数组队列,提供了相关的添加、删除、修改、遍历等功能 https://www.runoob.com/java/java-arraylist.html LocalDate: 泛型数据类型,用于设置 objectName 的数据类型,只能为引用数据类型。
objectName: 对象名 mn
Collections是一个工具类,sort是其中的静态方法,是用来对List类型进行排序的,
(1)升序Arrays.sort
Arrays.sort 本身是升序排列
//r是数组
if (p == 0)
//倒序排列的用法
Arrays.sort(r, 1, q + 1, Collections.reverseOrder());//左闭右开
else
Arrays.sort(r, q, r.length);//升序
(2)降序
Arrays.sort(r, 1, q + 1, Collections.reverseOrder());//左闭右开
6.日期问题Date
日期问题
Scanner scanner = new Scanner(System.in);
String[] s = scanner.next().split("/");
scanner.close();
String a = s[0];
String b = s[1];
String c = s[2];
输入02/03/04,使用split,可以直接分别存到字符串数组中。
https://blog.csdn.net/weixin_43914593/article/details/112728088
public class A06_航班时间_编程 {
private static Scanner in;//静态域
public static void main(String[] args) throws ParseException {
in = new Scanner(System.in);
int T=in.nextInt();
in.nextLine();//去除换行符
for(int i=0;i<T;i++) {
long time1=getTime();
long time2=getTime();
long t=(time1+time2)/2;
System.out.printf("%02d:%02d:%02d\n",t/3600,t/60%60,t%60);
}
}
private static long getTime() throws ParseException {
String line = in.nextLine();
String[] split = line.split(" ");//以空格为分隔符
//时间解析的API SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
//format.parse(split[0])解析字符串为日期
Date t1 = format.parse(split[0]);
Date t2 = format.parse(split[1]);
int d = 0;
if(split.length == 3){
//(+1) split[2].substring(2,3) 字符串获取1,用Integer 将String转换为整数型
d = Integer.parseInt(split[2].substring(2,3));// split[2].substring(2,3)-'0'
}
return d*24*3600+t2.getTime()/1000-t1.getTime()/1000;//getTime 得到时间代表的毫秒数
}
}
7.Math
1.Math.pow(2, 20) 2的20次方
2.Math.round四舍五入
double a=11.5
Math.round(a);
//输出为12
8.BigInteger 大数计算
BigInteger bi1 = new BigInteger("123456789") ; // 声明BigInteger对象
BigInteger bi2 = new BigInteger("987654321") ; // 声明BigInteger对象
System.out.println("加法操作:" + bi2.add(bi1)) ; // 加法操作
System.out.println("减法操作:" + bi2.subtract(bi1)) ; // 减法操作
System.out.println("乘法操作:" + bi2.multiply(bi1)) ; // 乘法操作
System.out.println("除法操作:" + bi2.divide(bi1)) ; // 除法操作
System.out.println("最大数:" + bi2.max(bi1)) ; // 求出最大数
System.out.println("最小数:" + bi2.min(bi1)) ; // 求出最小数
BigInteger result[] = bi2.divideAndRemainder(bi1) ; // 求出余数的除法操作
System.out.println("商是:" + result[0] + ";余数是:" + result[1]) ;
加法操作:1111111110
减法操作:864197532
乘法操作:121932631112635269
除法操作:8
最大数:987654321
最小数:123456789
商是:8;余数是:9
9、无法暴力输出的,直接打印结果
public class Main0{
public static void main(String[] args) throws Exception {
int a=100;
System.setOut(new PrintStream(new File("E:\\out3.txt")));
//拦截输入到E盘下的txt中
System.out.println(a);
}
}
10、String
public static void main(String[] args) {
String mounth;
mounth=""+1; // 将int型变为字符串
System.out.println(mounth);
Integer.parseInt(mouth)=1//转化为 int 型
}
(1)string是否包含字符
str='2019'
str.contains("2");//返回是否:true
str.indexOf(