码迷,mamicode.com
首页 > 其他好文 > 详细

Input and Output Method

时间:2017-07-01 18:30:54      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:namespace   names   style   general   important   des   ssi   sim   challenge   

  IO stands for input and output in programming.

  IO is important in programming, especially in Olympic Informatic, due to the policy of it. Therefore, as a contestant in Olympic Informatic, it‘s neccesary for me to sumarize the method of IO, in order to make my programs run more rapidly.

  Generally speaking, I classify the methods according to the real situation, containing the simple systematic one and the faster systematic one. Which one to use decides on the amount of data.

The Simple Systematic Methods

  Read a char c:  c = getchar();

  Read a char wanted:

inline char rdc(void) {
    char c = getchar();
    while (!isalpha(c); c=getchar());
    return c;
}

  Print a char c:  putchar(c);

 

  Read a integer x:  scanf("%d", &x); 

  Print a integer x:  printf("%d", x); 

 

  Read a string s:  scanf("%s", s+1); 

  Print a string s:  printf("%s\n", s+1); 

The Faster Systematic Methods

  The system is used to input integers, char, and output integer, char and string.

  The main idea is to read all data in strings and then transfer it , through using fread and fwrite

 

  Input char and integer: 

namespace Input {
    const int S = 2000000;
    char s[S]; char *h = s+S, *t = h;
    inline char getchr(void) { if (h == t) fread(s, 1, S, stdin), h = s; return *h++; }
    inline char rdc(void) { char c = getchr(); while (!isalpha(c)) c = getchr(); return c; }
    inline int rd(void) {
        int x = 0, f = 1; char c = getchr(); for (; !isdigit(c); c = getchr()) if (c == -) f = -1;
        for (; isdigit(c); c = getchar()) x = x*10+c-0; return x*f;
    }
}
using Input::getchr;
using Input::rdc;
using Input::rd;

 

  Output integer, char and string:

namespace Output {
    const int S = 2000000;
    char s[S]; char *t = s;
    inline void Add(char c) { *t++ = c; }
    inline void put(int x) {
        if (!x) { Add(0), Add(\n); return; }
        if (x < 0) Add(-), x = -x;
        static int a[70]; int len = 0;
        for (; x > 0; x /= 10) a[++len] = x%10;
        while (len > 0) Add(0 + a[len--]);
        Add(\n);
    }
    inline void Flush(void) { fwrite(s, 1, t-s, stdout); }
}
using Output::Add;
using Output::put;
using Output::Flush;

 

  This is the first time that I use English to write an article. It may be really a challenge for me, but bring me with pleasure and happiness !

 

Input and Output Method

标签:namespace   names   style   general   important   des   ssi   sim   challenge   

原文地址:http://www.cnblogs.com/Sdchr/p/7102833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!