简单但是缓慢的输入输出:
对于java初学者来说,最先接触的输入输出方式大概是Scanner方法和System.out.println,但是这些方法的运行速度是很缓慢的。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
sc.nextLine();
int n = sc.nextInt();
String str = sc.next();
double nn = sc.nextDouble();
System.out.println(n+" "+str+" "+nn+" "+str1);//printf(), print()......
}
}
快速的输入输出方法,包装成类,java流的使用
这里使用的是java的BufferedReader, StringTokenizer, PrintWriter等方法实现快速的输入输出。在各种编程比赛是,如果TLE的话,很有可能是没有使用快速的输入输出方法。
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.math.BigDecimal;
import java.math.BigInteger;
public class Main{
public static void main(String[] args){
quickin in = new quickin();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
String str = in.next();
int a = in.nextInt();
double b = in.nextDouble();
long c = in.nextLong();
BigDecimal d = in.nextBigDecimal();
BigInteger e = in.nextBigInteger();
pw.println(str);
pw.println(a);
pw.println(b);
pw.println(c);
pw.println(d);
pw.println(e);
pw.flush();
}
}
class quickin{
BufferedReader br;
StringTokenizer st;
quickin(){
br = new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext(){
while(st==null||!st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch(Exception e){
return false;
}
}return true;
}
String next(){
if(hasNext()) return st.nextToken();
return null;
}
int nextInt(){
return Integer.parseInt(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
long nextLong(){
return Long.parseLong(next());
}
BigInteger nextBigInteger(){
return new BigInteger(next());
}
BigDecimal nextBigDecimal(){
return new BigDecimal(next());
}
}