原链接:点击打开链接
先上C的:
适用于正int的.
int read(){//输入优化
char ch = ' ';
while(ch < '0' || ch > '9') ch = getchar();
int x = 0;
while(ch >= '0' && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
适用于正负int / LL / double 的. (来自 风神)
template <class T>
bool scan_d(T &ret)
{
char c;
int sgn;
T bit=0.1;
if(c=getchar(), c==EOF)
return 0;
while(c!='-' && c!='.' && (c<'0' || c>'9'))
c=getchar();
sgn=(c=='-')? -1:1;
ret=(c=='-')? 0:(c-'0');
while(c=getchar(), c>='0' && c<='9')
ret=ret*10+(c-'0');
if(c==' ' || c=='\n')
{
ret*=sgn;
return 1;
}
while(c=getchar(), c>='0' && c<='9')
ret+=(c-'0')*bit, bit/=10;
ret*=sgn;
return 1;
}
-----------------------------------------------------------------------------------------------------------
然后是 java 的.(根本不会脚哇233)
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.*;
import java.io.*;
import java.util.*;
class Scanner
{
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream in)
{
br = new BufferedReader(new InputStreamReader(in));
eat("");
}
private void eat(String s)
{
st = new StringTokenizer(s);
}
public String nextLine()
{
try
{
return br.readLine();
}
catch (IOException e)
{
return null;
}
}
public boolean hasNext()
{
while (!st.hasMoreTokens())
{
String s = nextLine();
if(s == null)
return false;
eat(s);
}
return true;
}
public String next()
{
hasNext();
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
}
while(in.hasNext()) 是输入到EOF结束
输入BigInteger 要
BigInteger a=new BigInteger(in.next());
前期一直用这个
后来... 发现另一个更短的...
缓冲输入流
包含了输入输出和输出挂
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
out.close();
}
}
仅能输入string 或单个字符
如输入 int:
int t=new Integer(in.readLine());
至今不会如何EOF...
所以, 综上!
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
out.close();
}
}
class InputReader
{
BufferedReader buf;
StringTokenizer tok;
InputReader()
{
buf = new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext()
{
while(tok == null || !tok.hasMoreElements())
{
try
{
tok = new StringTokenizer(buf.readLine());
}
catch(Exception e)
{
return false;
}
}
return true;
}
String next()
{
if(hasNext()) return tok.nextToken();
return null;
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
BigInteger nextBigInteger()
{
return new BigInteger(next());
}
BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}