1、对于C++加快输入输出:
cin,cout效率低,是因为输入输出的内容要经过缓存,导致效率降低。std::ios::sync_with_stdio(false)可以来打消中间的缓存,节省时间。但这样处理后不能同时混用cout和printf。
#include<iostream>
using namespace std;
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int m;
cin>>m;
cout<<m<<endl;
return 0;
}
2、对于JAVA的高性能输入,但只能每次一行地输入:
package com.teng.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
public class SpeedUp {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int i = Integer.parseInt(in.readLine());/*输入,只能每次一行地输入*/
String s = in.readLine();
double d = Double.parseDouble(in.readLine());
BigInteger b = new BigInteger(in.readLine());
out.println(i);
out.println(s);
out.println(d);
out.println(b);
out.flush();/*关闭输出*/
}
}
3、StreamTokenizer(可极大地加快JAVA输入速度,没有每次必须一行的限制):
package com.teng.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class StreamTokenizerTest {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();/*解析下一个输入对象,每次使用nval或sval前都要解析一次*/
int i = (int) in.nval;/*nval接收的为数字,但接收后类型就是double,需要int的话需要类型转化*/
in.nextToken();
double d = in.nval;
in.nextToken();
String s = in.sval;/*sval接收的为字符串*/
System.out.println(i);
System.out.println(d);
System.out.println(s);
}
}
4、普通的Scanner输入:
package com.teng.test;
import java.math.BigInteger;
import java.util.Scanner;
public class ScannerInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();/*整数*/
BigInteger b = scanner.nextBigInteger();/*更大范围的整数*/
String s1 = scanner.next();/*一个字符串,遇到空格或换行本次输入终止*/
String s2 = scanner.nextLine();/*输入一行字符串*/
double d = scanner.nextDouble();/*双精度浮点数*/
System.out.println(i);
System.out.println(b);
System.out.println(s1);
System.out.println(s2);
System.out.println(d);
}
}