1 template<typename type> 2 inline void read(type &x) 3 { 4 x=0;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?putchar('\n'):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?putchar('\n'):putchar(' '); 22 } 23 24 signed main() 25 { 26 int n; 27 read(n); 28 write(n,0);//不换行 29 return 0; 30 }
一些细节:
int a;read(a);write(a,1);
当输入2147483648时,会输出:-?
当输入2147483649时,会输出:-2147483647
当输入-2147483649时,会输出:2147483647
当输入-2147483650时,会输出:2147483646
而如果
int a;cin>>a;cout<<a<<endl;
当输入2147483648时,会输出:2147483647
当输入2147483649时,仍会输出:2147483647
当输入-2147483649时,会输出:-2147483648
当输入-2147483650时,仍会输出:-2147483648
至于原因,至今未明……