首页 > 代码库 > Input and Output Method
Input and Output Method
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