zoukankan      html  css  js  c++  java
  • 各种各样的输入输出挂!!!

    先上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 }
    View Code

    适用于正负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=='
    ')
    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 }
    View Code

     输出

    1 inline void out(int x)
    2 {
    3     if(x>9)
    4         out(x/10);
    5     putchar(x%10+'0');
    6 }
    View Code

    还有种很神的fread输入挂

    (其中HDOJ 5392  使用普通输入挂 7000msTLE     多交几遍能卡到6900+msAC   使用fread输入挂1500-ms。。。太神了)

     1 const int MAXBUF = 10000;
     2 char buf[MAXBUF], *ps = buf, *pe = buf+1;
     3 inline void rnext()
     4 {
     5     if(++ps == pe)
     6         pe = (ps = buf)+fread(buf,sizeof(char),sizeof(buf)/sizeof(char),stdin);
     7 }
     8 
     9 template <class T>
    10 inline bool in(T &ans)
    11 {
    12     ans = 0;
    13     T f = 1;
    14     if(ps == pe) return false;//EOF
    15     do{
    16         rnext();
    17         if('-' == *ps) f = -1;
    18     }while(!isdigit(*ps) && ps != pe);
    19     if(ps == pe) return false;//EOF
    20     do
    21     {
    22         ans = (ans<<1)+(ans<<3)+*ps-48;
    23         rnext();
    24     }while(isdigit(*ps) && ps != pe);
    25     ans *= f;
    26     return true;
    27 }
    View Code

     fread输出:

     1 const int  MAXOUT=10000;
     2 char bufout[MAXOUT], outtmp[50],*pout = bufout, *pend = bufout+MAXOUT;
     3 inline void write()
     4 {
     5     fwrite(bufout,sizeof(char),pout-bufout,stdout);
     6     pout = bufout;
     7 }
     8 inline void out_char(char c){ *(pout++) = c;if(pout == pend) write();}
     9 inline void out_str(char *s)
    10 {
    11     while(*s)
    12     {
    13         *(pout++) = *(s++);
    14         if(pout == pend) write();
    15     }
    16 }
    17 template <class T>
    18 inline void out_int(T x) {
    19     if(!x)
    20     {
    21         out_char('0');
    22         return;
    23     }
    24     if(x < 0) x = -x,out_char('-');
    25     int len = 0;
    26     while(x)
    27     {
    28         outtmp[len++] = x%10+48;
    29         x /= 10;
    30     }
    31     outtmp[len] = 0;
    32     for(int i = 0, j = len-1; i < j; i++,j--) swap(outtmp[i],outtmp[j]);
    33     out_str(outtmp);
    34 }
    View Code

    用法:

    int main()
    {
        int t, ca=1;
        in(t);
        while(t--)
        {
            int n;
            in(n);
            
            out_str("Case #");
            out_int(ca++);
            out_str(": ");
            out_int(n), out_char('
    ');
        }
        write(); // 一定要记得 write()啊!!!!! 
        return 0;
    }

    -----------------------------------------------------------------------------------------------------------

    然后是 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 }
    View Code

    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 }
    View Code

    仅能输入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 }
    View Code

    C++控制格式的输出

    http://www.cnblogs.com/walfud/articles/2047096.html

    这篇博客很好

  • 相关阅读:
    使用SWFUpload进行多文件上传
    TSQL递归
    Silverlight之视频录制
    Silverlight之摄像头麦克风使用
    Silverlight之文件上传组件
    SQL FOR XML
    XAML开发入门之XAML核心语法
    Ajax技术三种实现方式之xmlhttp+httphandler篇 (三)
    Ext中超时设定 ext.ajax.timeout
    后台执行js先执行前端的JS函数,再执行后台函数的按钮实
  • 原文地址:https://www.cnblogs.com/Empress/p/3997210.html
Copyright © 2011-2022 走看看