1. 程式人生 > 其它 >快讀、快寫模板(持續更新)

快讀、快寫模板(持續更新)

快讀:

1 template<typename type>
2 inline void read(type &x)
3 {
4     x=0;static bool flag(0);char ch=getchar();
5     while(!isdigit(ch)) flag=ch=='-',ch=getchar();
6     while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
7     flag?x=-x:0;
8 }

用法:read(x)

快寫:

1 template<typename type>
2
inline void write(type x,bool mode)//0為空格,1為換行 3 { 4 x<0?x=-x,putchar('-'):0;static short Stack[50],top(0); 5 do Stack[++top]=x%10,x/=10; while(x); 6 while(top) putchar(Stack[top--]|48); 7 mode?puts(""):putchar(' '); 8 }

用法:write(x,0)或write(x,1)

0為空格,1為換行

example

 1 #include<iostream>
 2
#include<cstdio> 3 4 using namespace std; 5 6 template<typename type> 7 inline void read(type &x) 8 { 9 x=0;static bool flag(0);char ch=getchar(); 10 while(!isdigit(ch)) flag=ch=='-',ch=getchar(); 11 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
12 flag?x=-x:0; 13 } 14 15 template<typename type> 16 inline void write(type x,bool mode)//0為空格,1為換行 17 { 18 x<0?x=-x,putchar('-'):0;static short Stack[50],top(0); 19 do Stack[++top]=x%10,x/=10; while(x); 20 while(top) putchar(Stack[top--]|48); 21 mode?puts(""):putchar(' '); 22 } 23 24 signed main() 25 { 26 int n; 27 read(n); 28 write(n,0);//不換行 29 return 0; 30 }