Python/Go/Java 算法常用模板、数据结构
记录一些个人在刷leetcode和CF中用的比较多的语法
初始化数组
Golang
go语言中定义二维数组和初始化比较特殊,需要先初始化行数再初始化列,定义一个mxn数组::
mat := make([][]int, m)
for i := range mat {
mat[i] = make([]int, n)
}
Python
定义一个mxn数组:
mat = [[0] * n for _ in range(m)]
本文来自博客园,作者:Jayvee,转载请注明原文链接: https://www.cnblogs.com/cenjw/p/java-algorithm-competition-input-model.html
输入输出
GO
package main
import (
"bufio"
"fmt"
"os"
)
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
func main() {
// STDOUT MUST BE FLUSHED MANUALLY!!!
defer writer.Flush()
var a, b int
scanf("%d %d\n", &a, &b)
printf("%d\n", a+b)
}
Java
实例化后调用即可。
class Read{
private BufferedReader bf;
private StringTokenizer st;
public Read(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public String nextLine() throws IOException{
return bf.readLine();
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public double nextDouble() throws IOException{
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
}
本文来自博客园,作者:micromatrix,转载请注明原文链接:https://www.cnblogs.com/cenjw/p/java-algorithm-competition-input-model.html
posted on 2022-08-07 20:37 micromatrix 阅读(163) 评论(0) 编辑 收藏 举报