大模板!!!
NOIP大模板(第一版)
一.文件输入,输出
1 #include<cstdio> 2 using namespace std; 3 int main(void) 4 { 5 freopen("","r",stdin); 6 freopen("","w",stdout); 7 //... 8 fclose(stdin); 9 fclose(stdout); 10 return 0; 11 }
二.输入,输出优化!!!(快读&快输)
(1)快读
1.整数读取 (需要调用 <iostream> <cstdio> )
inline int read() { int x=0,w=0; char ch=0; while(!isdigit(ch)) w|=ch=='-',ch=getchar(); //while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();以前以为位运算能快点,但问了大佬才发现,其实没啥差别 while(isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return w?-x:x; }
2.浮点数读取(需要调用 <iostream> <cstdio> )
1 inline double read() 2 { 3 int w=0,y=0; 4 double x=0,t=0.1; 5 char ch=0; 6 while(!isdigit(ch)) w|=ch=='-',ch=getchar(); 7 while(isdigit(ch)) x=x*10+(ch^48),ch=getchar(); 8 y|=ch=='.'; 9 if(y) 10 { 11 ch=getchar(); 12 while(isdigit(ch)) x+=(ch^48)*t,t*=0.1,ch=getchar(); 13 } 14 return w?-x:x; 15 }
(2)快输
1.整数输出(需要调用 <iostream> <cstdio> )
1 inline void write(int x) 2 { 3 if(x<0) 4 { 5 putchar('-'); 6 x=-x; 7 } 8 if(x>9) write(x/10); 9 putchar(x%10+'0'); 10 }
2.浮点数输出(暂无)
三.数论
(1)GCD(最大公约数)
1 inline int GCD(int a,int b) 2 { 3 while(b!=0) 4 { 5 int temp=a;; 6 a=b; 7 b=temp%a; 8 } 9 return a; 10 }
(2)LCM(最小公倍数)
1 inline int LCM(int a,int b) 2 { 3 return a/GCD(a,b)*b; 4 }
(3)拓展欧几里得
四.图论
(1)并查集
int Father[];//建立并查集 inline void Set(int x)//初始化 { for(int i=1;i<=x;++i) Father[i]=i; } int Find(int x)//查找x的祖先 { if(Father[x]!=x) Father[x]=Find(Father[x]); return Father[x]; } inline void Unit(int x,int y) { Father[y]=x;//二者有公共祖先 }