【模板整合计划】基本算法 常用模板

【模板整合计划】基本算法 常用模板


一:【快读,快输 】

快读乃考试必备佳品,能大大优化程序运行时间。

#include<cstdio>
#define LD double
#define Re register int
inline void in(Re &x){
    int f=0;x=0;char c=getchar();
    while(c<'0'||c>'9')f|=c=='-',c=getchar();
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
    x=f?-x:x;
}
inline void print(Re x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);putchar(x%10+'0');
}
inline LD LDin(){
    LD X=0,Y=1.0;int f=0;char c=getchar();
    while(c<'0'||c>'9')f|=c=='-',c=getchar(); 
    while(c>='0'&&c<='9')X=X*10+(c^48),c=getchar(); 
    if(c=='.'){c=getchar();while(c>='0'&&c<='9')X+=(Y/=10)*(c^48),c=getchar();}
    return f?-X:X;
}
int main(){}

二:【快速幂,龟速乘】

【模板】快速幂 \(||\) 取余运算 \([P1226]\)

这个有两种写法:二分,按进制分。按进制分要快些,二分数据范围要小些。

在快速幂的代码中需要做乘法,很有可能会乘爆,所以就需要用龟速乘来缩小数据范围,如果数据范围允许的话,就不用龟速乘,这样可以快些。

#include<cstdio>
#define LL long long
#define Re register int
int a,b,P;
inline int cf_flow(Re a,Re k){//分治 龟速乘
    if(!k)return 0;
    Re x=1,y=cf_flow(a,k>>1);
    x=(LL)y+y%P;
    if(k&1)x=(LL)x+a%P;
    return x%P;
}
inline int cf(Re a,Re b){//按进制分 龟速乘
    Re x=0;
    while(b){
        if(b&1)x=(x+a)%P;
        a=a*2%P,b>>=1;
    }
    return x%P;
}
inline int mi_slow(Re a,Re k){//分治 快速幂
    if(!k)return 1%P;
    Re x=1,y=mi_slow(a,k>>1);
    x=(LL)y*y%P;
    if(k&1)x=(LL)x*a%P;
    return x%P;
}
inline int mi(Re x,Re k){//按进制分 快速幂
    Re s=1;
    while(k){
        if(k&1)s=(LL)s*x%P;
        x=(LL)x*x%P,k>>=1;
    }
    return s%P;
}
int main(){}

三:【三分】

【模板】三分法 \(\text{[P3382]}\)

#include<cstdio>
#define LD double
#define Re register int
const int N=15;
const LD eps=1e-6;
int n;LD l,r,a[N];
inline int dcmp(LD a){return a<-eps?-1:(a>eps?1:0);}
inline LD F(LD x){
	LD s=0;
	for(Re i=0;i<=n;++i)s=s*x+a[i];
	return s;
}
int main(){
	scanf("%d%lf%lf",&n,&l,&r);
	for(Re i=0;i<=n;++i)scanf("%lf",&a[i]);
	while(dcmp(l-r)<0){
		LD mid=(l+r)/2.0;
		if(F(mid+eps)>F(mid-eps))l=mid;
		else r=mid;
	}
	printf("%.5lf",l);
}

四:【ST表求区间最值 (RMQ)】

【模板】ST 表 \([P3865]\)

const int N=1e5+3,logN=17;
int n,x,y,T,lg[N],f[N][logN+1];
inline int ask(Re l,Re r){
	Re k=lg[r-l+1];
	return max(f[l][k],f[r-(1<<k)+1][k]);
}
int main(){
	in(n),in(T),lg[1]=0;
	for(Re i=2;i<=n;i++)lg[i]=lg[i>>1]+1;//预处理log值 
	for(Re i=1;i<=n;++i)in(f[i][0]);
	for(Re p=1;p<=lg[n];++p)
		for(Re i=1;i+(1<<p)-1<=n;++i)
			f[i][p]=max(f[i][p-1],f[i+(1<<p-1)][p-1]);
	while(T--)in(x),in(y),printf("%d\n",ask(x,y));
}

五:【归并排序】

【模板】快速排序 \([P1177]\)

#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
#include<cstdio>
#define Re register int
const int N=5e5+3;
int fu,n,s[N],b[N];long long gs;char ch;
inline void in(int &x){
    fu=x=0;ch=getchar();
    while(ch<'0'||ch>'9')fu|=ch=='-',ch=getchar();
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=fu?-x:x;
}
void merge(Re L,Re mid,Re R){
    Re i=L,j=mid+1,w=L;
    while(i<=mid&&j<=R){
        if(s[i]<=s[j])b[w++]=s[i++];
        else b[w++]=s[j++],gs+=mid-i+1;
    }
    for(;i<=mid;i++)b[w++]=s[i];
    for(;j<=R;j++)b[w++]=s[j];
    for(i=L;i<=R;i++)s[i]=b[i];
}
inline void Sort(Re L,Re R){
    if(L<R){
        Re mid=L+R>>1;
        Sort(L,mid),Sort(mid+1,R);
        merge(L,mid,R);
    }
}
int main(){
    in(n);
    for(Re i=1;i<=n;i++)in(s[i]);
    Sort(1,n);
    for(Re i=1;i<=n;i++)printf("%d ",s[i]); 
//    printf("%lld",gs);//输出逆序对个数 
}

六:【快排】

【模板】快速排序 \([P1177]\)

排序直接用 \(\text{sort}\),求逆序对用归并,快排似乎没啥用哎╮(╯▽╰)╭。

#include<bits/stdc++.h>
using namespace std;
int data[100100],n;
void mysort(int left, int right){
    int mid=data[(left+right)/2];
    int i=left,j=right;
    while(i<=j){
        while(data[i]<mid)i++;
        while(data[j]>mid)j--;
        if(i<=j)swap(data[i++],data[j--]);
    }
    if(j>left)mysort(left,j);
    if(i<right)mysort(i,right);
}
int main(){    
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&data[i]);
    mysort(1,n);
    for(int i=1;i<=n;i++)printf("%d ",data[i]);
} 

七:【n 进制转 m 进制】

【模板】进制转换 \([P1143]\)

这个感觉很少用到哎。。

#include<cstdio>
const int N=1e5+3;
int n,m,x,tmp,ansl;char a[N],ans[N];
inline int n_to_ten(){
    int x=0;
    for(int i=0;a[i];++i){
        x*=n;
        if (a[i]>='A'&&a[i]<='F')x+=(a[i]-'A'+10);
        else x+=(a[i]-'0');
   }
   return x;
}
inline void ten_to_m(){
    do{
        tmp=x%m;
        if(tmp<10)ans[++ansl]='0'+tmp;
        else ans[++ansl]='A'+tmp-10;
        x/=m;
    }while(x);
}
int main(){
   scanf("%d%s%d",&n,a,&m);
   x=n_to_ten();
   ten_to_m();
   for(int i=ansl;i;--i)printf("%c",ans[i]);puts("");
}

八:【高精度】

(1).【高精度加法】

【模板】\(\text{A+B Problem}\)(高精)\(\text{[P1601]}\)

#include<algorithm>
#include<cstring>
#include<cstdio>
#define LL long long
#define Re register int
using namespace std;
template<typename T>inline void in(T &x){
    int f=0;x=0;char c=getchar();
    while(c<'0'||c>'9')f|=c=='-',c=getchar();
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
    x=f?-x:x;
}
const int M=510;
char s[M];
struct Int{
	short a[M];int len;
	inline void In(){
		scanf("%s",s+1),len=strlen(s+1);
		for(Re i=1;i<=len;++i)a[i]=s[len-i+1]-'0';
	}
	inline void out(){for(Re i=len;i;--i)printf("%d",a[i]);}
	inline Int operator+=(const Int &O){
		len=max(len,O.len);
        for(Re i=1;i<=len;++i)a[i]+=O.a[i],a[i+1]+=a[i]/10,a[i]%=10;
		while(a[len+1])++len;
		return *this;
	}
}a,b;
int main(){a.In(),b.In(),(a+=b).out();}

(2).【高精度乘法】

【模板】\(\text{A}*\text{B Problem}\)(高精)\(\text{[P1303]}\)

#include<algorithm>
#include<cstring>
#include<cstdio>
#define LL long long
#define Re register int
using namespace std;
template<typename T>inline void in(T &x){
    int f=0;x=0;char c=getchar();
    while(c<'0'||c>'9')f|=c=='-',c=getchar();
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
    x=f?-x:x;
}
const int M=4010;
char s[M];
struct Int{
	short a[M];int len;
	Int(){len=0;memset(a,0,sizeof(a));}
	inline void In(){
		scanf("%s",s+1),len=strlen(s+1);
		for(Re i=1;i<=len;++i)a[i]=s[len-i+1]-'0';
	}
	inline void out(){for(Re i=len;i;--i)printf("%d",a[i]);}
	inline Int operator*(const Int &O)const{
		Int ans;
        for(Re i=1;i<=len;++i){
			Re x=0;
			for(Re j=1;j<=O.len;++j)
				ans.a[i+j-1]+=a[i]*O.a[j]+x,x=ans.a[i+j-1]/10,ans.a[i+j-1]%=10;
			if(x)ans.a[i+O.len]=x;
		}
		ans.len=len+O.len;
		while(!ans.a[ans.len]&&ans.len>1)ans.len--;
		return ans;
	}
	inline Int operator*=(const Int &O){return *this=*this*O;}
}a,b;
int main(){a.In(),b.In(),(a*=b).out();}

(3).【高精全模板】

【模板】\(\text{A+B Problem}\)(高精)\(\text{[P1601]}\)

【模板】\(\text{A*B Problem}\)(高精)\(\text{[P1303]}\)

【模板】\(\text{A/B Problem}\)(高精)\(\text{[P1480]}\)

【模板】\(\text{A+B A-B A*B A/B A%B Problem [P1932]}\)

(强推这个大佬写的高精全模板

代码量略大,疯狂压了波行,还是有两百多行。

如果嫌代码太长,可以吧一些不必要的删掉,只是要注意像除法,取模这样的运算需要用到减法,加法,所以像只留除,模,删掉加,减这样的行为会让你编译不过的。

#include<cstring>
#include<cstdio>
#define LL long long
#define Re register int
using namespace std;
const int base=1e8;
const int N=1e4+10;
int aux[N<<3];
struct Int{
    int s[N],l;
    inline void CL(){l=0;memset(s,0,sizeof(s));}
    inline void pr(){
        printf("%d",s[l]);
        for(Re i=l-1;i;i--)printf("%08d",s[i]);
    }
    inline void re_l(){
        Re i,x=0,k=1,L=0,fl,o;char c=getchar();
        for(;c<'0'||c>'9';c=getchar());
        for(;c>='0'&&c<='9';c=getchar()){if(!(L-1)&&!aux[L])L--;aux[++L]=c-'0';}
        CL();l=L/8+((o=L%8)>0);
        for(i=1;i<=o;i++)x=x*10+aux[i];
        if(o)s[l]=x;fl=!o?l+1:l;
        for(i=o+1,x=0;i<=L;i++,k++){
            x=x*10+aux[i];
            if(!(k^8))s[--fl]=x,x=k=0;
        }
        if(!l)l=1;
    }
    inline LL toint(){
        LL x=0;
        for(Re i=l;i;i--)x=x*base+s[i];
        return x;
    }
    inline Int operator=(Re b){
        CL();
        do s[++l]=b%base,b/=base;while(b>0);
        return *this;
    }
    inline Int operator=(LL b){
        CL();
        do s[++l]=b%base,b/=base;while(b>0);
        return *this;
    }
    inline Int operator+(const Re &b){
        Int c=*this;LL x=b;
        for(Re i=1;i<=l&&x;i++)x=x+c.s[i],c.s[i]=x%base,x/=base;
        if(x)c.s[++c.l]=x;
        return c;
    }
    inline Int operator+(const LL &b){
        Int c=*this;LL x=b;
        for(Re i=1;i<=l&&x;i++)x=x+c.s[i],c.s[i]=x%base,x/=base;
        if(x)c.s[++c.l]=x;
        return c;
    }
    inline Int operator+(Int &b){
        if(b.l<3)return *this+b.toint();
        Int c;LL x=0;Re k=l<b.l?b.l:l;c.CL(),c.l=k;
        for(Re i=1;i<=k;i++)x=x+s[i]+b.s[i],c.s[i]=x%base,x/=base;
        if(x)c.s[++c.l]=x;
        return c;
    }
    inline Int operator-(const Int &b){
        Int c,d=*this;LL x=0;c.CL();
        for(Re i=1;i<=l;i++){
            if((x=d.s[i])<b.s[i])d.s[i+1]--,x+=base;
            c.s[i]=x-b.s[i];
        }
        c.l=l;
        for(;!c.s[c.l]&&c.l>1;c.l--);
        return c;
    }
    inline Int operator-(const Re &b){Int c;return *this-(c=b);}
    inline Int operator-(const LL&b){Int c;return *this-(c=b);}
    inline Int operator*(const Re &b){
        Int c;LL x=0;c.CL();
        for(Re i=1;i<=l;i++)x=x+1LL*s[i]*b,c.s[i]=x%base,x/=base;
        for(c.l=l;x;x/=base)c.s[++c.l]=x%base;
        return c;
    }
    inline Int operator*(Int&b){
        if(b.l<2)return *this*b.toint();
        Int c;LL x;Re i,j,k;c.CL();
        for(i=1;i<=l;i++){
            x=0;
            for(j=1;j<=b.l;j++)x=x+1LL*s[i]*b.s[j]+c.s[k=i+j-1],c.s[k]=x%base,x/=base;
            if(x)c.s[i+b.l]=x;
        }
        for(c.l=l+b.l;!c.s[c.l]&&c.l>1;c.l--);
        return c;
    }
    inline Int operator*(const LL &b){
        Int c;
        if(b>2e9){c=b;return *this*c;}
        LL x=0;c.CL();
        for(Re i=1;i<=l;i++)x=x+b*s[i],c.s[i]=x%base,x/=base;
        for(c.l=l;x;x/=base)c.s[++c.l]=x%base;
        return c;
    }
    inline Int operator/(const Re &b){
        Int c;LL x=0;c.CL();
        for(Re i=l;i;i--)c.s[i]=(x*base+s[i])/b,x=(x*base+s[i])%b;
        for(c.l=l;!c.s[c.l]&&c.l>1;c.l--);
        return c;
    }
    inline Int operator/(const LL&b){
        Int c;LL x=0;c.CL();
        for(Re i=l;i;i--)c.s[i]=(x*base+s[i])/b,x=(x*base+s[i])%b;
        for(c.l=l;!c.s[c.l]&&c.l>1;c.l--);
        return c;
    }
    inline Int operator/(Int&b){
        if(b.l<2)return *this/b.toint();
        Int c,d;Re i,j,le,r,mid,k;c.CL();d.CL();
        for(i=l;i;i--){
            for(j=++d.l;j>1;j--)d.s[j]=d.s[j-1];
            d.s[1]=s[i];
            if(d<b)continue;
            le=k=0;r=base-1;
            while(le<=r){
                mid=(le+r)>>1;
                if(b*mid<=d)le=mid+1,k=mid;
                else r=mid-1;
            }
            c.s[i]=k,d=d-b*k;
        }
        for(c.l=l;!c.s[c.l]&&c.l>1;c.l--);
        return c;
    }
    inline Int operator%(const Re &b){
        Int c;LL x=0;c.CL();
        for(Re i=l;i;i--)x=(x*base+s[i])%b;
        return c=x;
    }
    inline Int operator%(const LL&b){
        Int c;LL x=0;c.CL();
        for(Re i=l;i;i--)x=(x*base+s[i])%b;
        return c=x;
    }
    inline Int operator%(Int&b){
        if(b.l<2)return *this%b.toint();
        Int c;Re i,j,le,r,mid,k;c.CL();
        for(i=l;i;i--){
            for(j=++c.l;j>1;j--)c.s[j]=c.s[j-1];
            c.s[1]=s[i];
            if(c<b)continue;
            le=k=0,r=base-1;
            while(le<=r){
                mid=(le+r)>>1;
                if(b*mid<=c)le=mid+1,k=mid;
                else r=mid-1;
            }
            c=c-b*k;
        }
        for(;!c.s[c.l]&&c.l>1;c.l--);
        return c;
    }
    inline Int operator+=(Int&b){return *this=*this+b;}
    inline Int operator+=(LL&b){return *this=*this+b;}
    inline Int operator+=(Re &b){return *this=*this+b;}
    inline Int operator-=(Int&b){return *this=*this-b;}
    inline Int operator-=(LL&b){return *this=*this-b;}
    inline Int operator-=(Re &b){return *this=*this-b;}
    inline Int operator*=(Int&b){return *this=*this*b;}
    inline Int operator*=(LL&b){return *this=*this*b;}
    inline Int operator*=(Re &b){return *this=*this*b;}
    inline Int operator/=(Int&b){return *this=*this/b;}
    inline Int operator/=(LL&b){return *this=*this/b;}
    inline Int operator/=(Re &b){return *this=*this/b;}
    inline Int operator%=(Int&b){return *this=*this%b;}
    inline Int operator%=(LL&b){return *this=*this%b;}
    inline Int operator%=(Re &b){return *this=*this%b;}
    inline bool operator<(const Int&b)const{
        if(l^b.l)return l<b.l;
        for(Re i=l;i;i--)if(s[i]^b.s[i])return s[i]<b.s[i];
        return 0;
    }
    inline bool operator<=(const Int&b)const{
        if(l^b.l)return l<b.l;
        for(Re i=l;i;i--)if(s[i]^b.s[i])return s[i]<b.s[i];
        return 1;
    }
    inline bool operator>(const Int&b)const{
        if(l^b.l)return l>b.l;
        for(Re i=l;i;i--)
            if(s[i]^b.s[i])return s[i]>b.s[i];
        return 0;
    }
    inline bool operator>=(const Int&b)const{
        if(l^b.l)return l>b.l;
        for(Re i=l;i;i--)if(s[i]^b.s[i])return s[i]>b.s[i];
        return 1;
    }
    inline bool operator==(const Int&b)const{
        if(l^b.l)return 0;
        for(Re i=l;i;i--)if(s[i]^b.s[i])return 0;
        return 1;
    }
    inline bool operator!=(const Int&b)const{
        if(l^b.l)return 1;
        for(Re i=l;i;i--)if(s[i]^b.s[i])return 1;
        return 0;
    }
    inline bool operator<(LL b)const{Int c;return *this<(c=b);}
    inline bool operator<=(LL b)const{Int c;return *this<=(c=b);}
    inline bool operator>(LL b)const{Int c;return *this>(c=b);}
    inline bool operator>=(LL b)const{Int c;return *this>=(c=b);}
    inline bool operator==(LL b)const{Int c;return *this==(c=b);}
    inline bool operator!=(LL b)const{Int c;return *this!=(c=b);}
    inline bool operator<(Re b)const{Int c;return *this<(c=b);}
    inline bool operator<=(Re b)const{Int c;return *this<=(c=b);}
    inline bool operator>(Re b)const{Int c;return *this>(c=b);}
    inline bool operator>=(Re b)const{Int c;return *this>=(c=b);}
    inline bool operator==(Re b)const{Int c;return *this==(c=b);}
    inline bool operator!=(Re b)const{Int c;return *this!=(c=b);}
};
Int a,b;
int main(){
    a.re_l();
    b.re_l();
    a.pr();printf("+");b.pr();printf("=");(a+b).pr();printf("\n");
    a.pr();printf("-");b.pr();printf("=");
    if(a<b){putchar('-');(b-a).pr();}
    else(a-b).pr();printf("\n");
    a.pr();printf("*");b.pr();printf("=");(a*b).pr();printf("\n");
    a.pr();printf("/");b.pr();printf("=");(a/b).pr();printf("\n");
    a.pr();printf("%%");b.pr();printf("=");(a%b).pr();printf("\n");
    if(a<b)printf("a<b\n");
    if(a<=b)printf("a<=b\n");
    if(a>b)printf("a>b\n");
    if(a>=b)printf("a>=b\n");
    if(a==b)printf("a==b\n");
    if(a!=b)printf("a!=b\n");
}

九:【模拟退火】

(1).【坐标系随机移动】

【模板】 \(\text{A Star not a Tree? [UVA10228]}\)

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LD double
#define LL long long
#define Re register int
using namespace std;
const int N=1e6+3;
int n,HYJ,X[N],Y[N];
inline void in(Re &x){
    int f=0;x=0;char c=getchar();
    while(c<'0'||c>'9')f|=c=='-',c=getchar();
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
    x=f?-x:x;
}
inline LD calc(LD x,LD y){
    LD tmp=0;
    for(Re i=1;i<=n;++i)tmp+=sqrt((x-X[i])*(x-X[i])+(y-Y[i])*(y-Y[i]));
    return tmp;
}
inline void Srand(Re seed){
    srand(seed);Re a=rand()%10+1;
    while(a--)srand(rand());
}
LD ans,ansx,ansy;
const LD deltaT=0.99,maxT=4995,minT=1e-12;
inline void fire(){
    LD x=ansx,y=ansy,nowT=maxT;
    while(nowT>minT){
        LD nx=x+((rand()<<1)-RAND_MAX)*nowT;
        LD ny=y+((rand()<<1)-RAND_MAX)*nowT;
        LD nans=calc(nx,ny),deltaE=nans-ans;
        if(deltaE<0)ans=nans,ansx=x=nx,ansy=y=ny;
        else if(exp(-deltaE/nowT)>(LD)rand()/RAND_MAX)x=nx,y=ny;
        nowT*=deltaT;
    }
}
int main(){
//    freopen("123.txt","r",stdin);
    in(HYJ),Srand(20030825);
    while(HYJ--){
        in(n);
        for(Re i=1;i<=n;++i)in(X[i]),in(Y[i]);
        ans=calc(X[1],Y[1]),ansx=X[1],ansy=Y[1];
        fire(),fire(),fire(),fire(),fire();
        printf("%.lf\n",ans);
        if(HYJ)puts("");
    }
}

(2).【序列随机swap】

【模板】 均分数据 \(\text{[HAOI2006] [P2503]}\)

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LD double
#define LL long long
#define Re register int
using namespace std;
const int N=23;
int n,m,S,A[N];LD x0;
inline void in(Re &x){
    int f=0;x=0;char c=getchar();
    while(c<'0'||c>'9')f|=c=='-',c=getchar();
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
    x=f?-x:x;
}
inline void Srand(Re seed){
    srand(seed);Re a=rand()%10+1;
    while(a--)srand(rand());
}
inline void Random(Re *A,Re n){
    for(Re i=1;i<=n;++i)swap(A[i],A[rand()%n+1]);
}
int x[N];
inline LD calc(Re *A){
    for(Re i=1;i<=m;++i)x[i]=0;
    for(Re i=1;i<=n;++i){
        Re w=1;
        for(Re j=2;j<=m;++j)if(x[j]<x[w])w=j;
        x[w]+=A[i];
    }
    LD tmp=0;
    for(Re i=1;i<=m;++i)tmp+=(x[i]-x0)*(x[i]-x0);
    return tmp/m;
}
int nowA[N],ansA[N];LD ans;
const LD deltaT=0.995,maxT=4995,minT=1e-15;
inline void fire(){
    for(Re i=1;i<=n;++i)nowA[i]=ansA[i];LD nowT=maxT;
    while(nowT>minT){
        Re x=rand()%n+1,y=rand()%n+1;
        swap(nowA[x],nowA[y]);
        LD nans=calc(nowA),deltaE=nans-ans;
        if(deltaE<0){for(Re i=1;i<=n;++i)ansA[i]=nowA[i];ans=nans;}
        else if(exp(-deltaE/nowT)>(LD)rand()/RAND_MAX);
        else swap(nowA[x],nowA[y]);
        nowT*=deltaT;
    }
}
int main(){
//    freopen("123.txt","r",stdin);
    in(n),in(m);
    for(Re i=1;i<=n;++i)in(A[i]),S+=A[i];x0=(LD)S/m;
    for(Re i=1;i<=n;++i)ansA[i]=A[i];
    Srand(965579261),Random(ansA,n),Srand(20030825);
    ans=calc(ansA),fire(),fire(),fire(),fire(),fire();
    printf("%.2lf\n",sqrt(ans));
}

十:【Linux 对拍】

#!/bin/bash #背下来
g++ data.cpp -o data
g++ std.cpp -o std
g++ myprogram.cpp -o myprogram
while true; do
./data>Circuit.in #数据生成器输出数据重定向到Circuit.in
./myprogram #待测程序
./std #正确程序
if diff Circuit.out std.out; then #比较两个文件
printf AC #正确输出AC
else
printf WA #错误输出WA
#cat Circuit.out std.out #显示两个文件
exit 0 #退出
fi #结束if
done #结束while
posted @ 2019-05-27 15:53  辰星凌  阅读(1387)  评论(0编辑  收藏  举报