标签:style blog http color io os ar java strong
先上C的:
适用于正int的.
1 int read() 2 { 3 char ch=‘ ‘; 4 int ans=0; 5 while(ch<‘0‘ || ch>‘9‘) 6 ch=getchar(); 7 while(ch<=‘9‘ && ch>=‘0‘) 8 { 9 ans=ans*10+ch-‘0‘; 10 ch=getchar(); 11 } 12 return ans; 13 }
适用于正负int / LL / double 的. (来自 风神)
1 template <class T> 2 bool scan_d(T &ret) 3 { 4 char c; 5 int sgn; 6 T bit=0.1; 7 if(c=getchar(), c==EOF) 8 return 0; 9 while(c!=‘-‘ && c!=‘.‘ && (c<‘0‘ || c>‘9‘)) 10 c=getchar(); 11 sgn=(c==‘-‘)? -1:1; 12 ret=(c==‘-‘)? 0:(c-‘0‘); 13 while(c=getchar(), c>=‘0‘ && c<=‘9‘) 14 ret=ret*10+(c-‘0‘); 15 if(c==‘ ‘ || c==‘\n‘) 16 { 17 ret*=sgn; 18 return 1; 19 } 20 while(c=getchar(), c>=‘0‘ && c<=‘9‘) 21 ret+=(c-‘0‘)*bit, bit/=10; 22 ret*=sgn; 23 return 1; 24 }
-----------------------------------------------------------------------------------------------------------
然后是 java 的.
1 import java.util.StringTokenizer; 2 import java.io.BufferedReader; 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 import java.math.*; 6 import java.io.*; 7 import java.util.*; 8 9 class Scanner 10 { 11 BufferedReader br; 12 StringTokenizer st; 13 public Scanner(InputStream in) 14 { 15 br = new BufferedReader(new InputStreamReader(in)); 16 eat(""); 17 } 18 private void eat(String s) 19 { 20 st = new StringTokenizer(s); 21 } 22 public String nextLine() 23 { 24 try 25 { 26 return br.readLine(); 27 } 28 catch (IOException e) 29 { 30 return null; 31 } 32 } 33 public boolean hasNext() 34 { 35 while (!st.hasMoreTokens()) 36 { 37 String s = nextLine(); 38 if(s == null) 39 return false; 40 eat(s); 41 } 42 return true; 43 } 44 public String next() 45 { 46 hasNext(); 47 return st.nextToken(); 48 } 49 public int nextInt() 50 { 51 return Integer.parseInt(next()); 52 } 53 long nextLong() 54 { 55 return Long.parseLong(next()); 56 } 57 double nextDouble() 58 { 59 return Double.parseDouble(next()); 60 } 61 }
while(in.hasNext()) 是输入到EOF结束
输入BigInteger 要
BigInteger a=new BigInteger(in.next());
前期一直用这个
后来... 发现另一个更短的...
缓冲输入流
包含了输入输出和输出挂
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 5 public class Main 6 { 7 public static void main(String[] args) throws NumberFormatException, IOException 8 { 9 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 10 PrintWriter out = new PrintWriter(System.out); 11 12 13 14 out.close(); 15 } 16 }
仅能输入string 或单个字符
如输入 int:
int t=new Integer(in.readLine());
至今不会如何EOF...
所以, 综上!
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 5 public class Main 6 { 7 8 public static void main(String[] args) 9 { 10 InputReader in = new InputReader(); 11 PrintWriter out = new PrintWriter(System.out); 12 13 14 15 out.close(); 16 } 17 } 18 class InputReader 19 { 20 BufferedReader buf; 21 StringTokenizer tok; 22 InputReader() 23 { 24 buf = new BufferedReader(new InputStreamReader(System.in)); 25 } 26 boolean hasNext() 27 { 28 while(tok == null || !tok.hasMoreElements()) 29 { 30 try 31 { 32 tok = new StringTokenizer(buf.readLine()); 33 } 34 catch(Exception e) 35 { 36 return false; 37 } 38 } 39 return true; 40 } 41 String next() 42 { 43 if(hasNext()) return tok.nextToken(); 44 return null; 45 } 46 int nextInt() 47 { 48 return Integer.parseInt(next()); 49 } 50 long nextLong() 51 { 52 return Long.parseLong(next()); 53 } 54 double nextDouble() 55 { 56 return Double.parseDouble(next()); 57 } 58 BigInteger nextBigInteger() 59 { 60 return new BigInteger(next()); 61 } 62 BigDecimal nextBigDecimal() 63 { 64 return new BigDecimal(next()); 65 } 66 }
标签:style blog http color io os ar java strong
原文地址:http://www.cnblogs.com/Empress/p/3997210.html