机试笔试
# 机试笔试
# 1、读取输入数据
使用Scanner类进行数据的读取:
Scanner sc=new Scanner(System.in);
1
# 读取整数
int in=sc.nextInt();
long in2=sc.nextLong();
1
2
2
# 读取字符串
区分next()和nextLine()两个函数。
next():直接读取一个字符串,以空格和换行符分割,不会读取空格和换行符。适合一个个读、
nextLine():直接读取一整行的数据作为字符串,直到换行符,一般需要通过split()根据空格划分成字符串数组。
String nextStr=sc.next();
String line=sc.nextLine();
String[] strs=line.split(" ");
1
2
3
2
3
# 读取浮点数
Double num=sc.nextDoublt();
1
# 输出保留两位小数
System.out.printf("%.4f",num);
1
此外,也可以通过String保留两位小数,然后再进行输出
String out=String.format("%.4f",num);
System.out.print(out);
1
2
2
# MOD运算出现负数
对于负数而言,进行模运算之后还是负数,个别题目中需要加上MOD保证结果恒为整数:
long post=(a*curr%1000000007-pre*b%1000000007+1000000007)%1000000007;
1
# 2.常规思路
常规问题采用的数据结构:
- 任意连续子数组之和——前缀和+哈希
- 最长递增子序列——①贪心+二分②动规
编辑 (opens new window)
上次更新: 2024/04/08, 16:52:36