跳到正文

个人记录

【个人记录 #1】个人代码规范

mofan101028约 15 分钟
原文
文章信息与版本
前言

这些都是按照自己的习惯写的,代码风格因人而异。

板子

C++
#include <bits/stdc++.h>
using namespace std;
#define upp(x,y,z) for(x=(y);x<=(z);++x)
#define low(x,y,z) for(x=(y);x>=(z);--x)
#define tt template<typename t0>
#define fs(x) fixed<<setprecision(x)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define debug(x) cout<<"Line "<<__LINE__<<","<<(x)<<"."<<endl
tt inline void chkmax(t0 &x,t0 y){if(x<y){x=y;}}
tt inline void chkmin(t0 &x,t0 y){if(x>y){x=y;}}
const int eps=0xf; const int inf=0x7fffffff; const ll infll=0x7fffffffffffffff; const ull infull=0xffffffffffffffff;
const int N=1e6+eps; const ll mod=998244353;
void code(); void perf();
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); code(); perf();
	return 0;
}
bool memorybegin;
void code(){
	//do something...
}
bool memoryend;
void perf(){
	cout<<"CPU time used:"<<fs(3)<<(double)clock()/CLOCKS_PER_SEC<<"s."<<endl;
	cout<<"Memory used:"<<fs(3)<<abs((&memoryend-&memorybegin)*1.0/1024.0/1024.0)<<"MB."<<endl;
}

板子随时调整。

拍子

内容

板子(可能与最新板子不同):

C++
#include <bits/stdc++.h>
using namespace std;
#define upp(x,y,z) for(x=(y);x<=(z);++x)
#define low(x,y,z) for(x=(y);x>=(z);--x)
#define tt template<typename t0>
#define fs(x) fixed<<setprecision(x)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ull unsigned long long
#define debug(x) cout<<"Line "<<__LINE__<<","<<(x)<<"."<<endl
tt inline void chkmax(t0 &x,t0 y){if(x<y){x=y;}}
tt inline void chkmin(t0 &x,t0 y){if(x>y){x=y;}}
const int eps=0xf; const int inf=0x7fffffff; const ll infll=0x7fffffffffffffff; const ull infull=0xffffffffffffffff;
const int N=1e6+eps;
void code(); void perf();
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); code(); perf();
	return 0;
}
bool memorybegin;
void code(){
	//do something...
}
bool memoryend;
void perf(){
	cout<<"CPU time used:"<<fs(3)<<(double)clock()/CLOCKS_PER_SEC<<"s."<<endl;
	cout<<"Memory used:"<<fs(3)<<abs((&memoryend-&memorybegin)*1.0/1024.0/1024.0)<<"MB."<<endl;
}

正确程序(求 a⊕ba \oplus b ):

C++
#include <bits/stdc++.h>
using namespace std;
#define upp(x,y,z) for(x=(y);x<=(z);++x)
#define low(x,y,z) for(x=(y);x>=(z);--x)
#define tt template<typename t0>
#define fs(x) fixed<<setprecision(x)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ull unsigned long long
#define debug(x) cout<<"Line "<<__LINE__<<","<<(x)<<"."<<endl
tt inline void chkmax(t0 &x,t0 y){if(x<y){x=y;}}
tt inline void chkmin(t0 &x,t0 y){if(x>y){x=y;}}
const int eps=0xf; const int inf=0x7fffffff; const ll infll=0x7fffffffffffffff; const ull infull=0xffffffffffffffff;
const int N=1e6+eps;
void code(); void perf();
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); code(); //perf();
	return 0;
}
bool memorybegin;
void code(){
	freopen("random.in","r",stdin);
	freopen("correct.out","w",stdout);
	ull a,b; cin>>a>>b; cout<<(a^b)<<endl;
}
bool memoryend;
void perf(){
	cout<<"CPU time used:"<<fs(3)<<(double)clock()/CLOCKS_PER_SEC<<"s."<<endl;
	cout<<"Memory used:"<<fs(3)<<abs((&memoryend-&memorybegin)*1.0/1024.0/1024.0)<<"MB."<<endl;
}

正确性待验证程序:

C++
#include <bits/stdc++.h>
using namespace std;
#define upp(x,y,z) for(x=(y);x<=(z);++x)
#define low(x,y,z) for(x=(y);x>=(z);--x)
#define tt template<typename t0>
#define fs(x) fixed<<setprecision(x)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ull unsigned long long
#define debug(x) cout<<"Line "<<__LINE__<<","<<(x)<<"."<<endl
tt inline void chkmax(t0 &x,t0 y){if(x<y){x=y;}}
tt inline void chkmin(t0 &x,t0 y){if(x>y){x=y;}}
const int eps=0xf; const int inf=0x7fffffff; const ll infll=0x7fffffffffffffff; const ull infull=0xffffffffffffffff;
const int N=1e6+eps;
void code(); void perf();
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); code(); //perf();
	return 0;
}
bool memorybegin;
void code(){
	freopen("random.in","r",stdin);
	freopen("solution.out","w",stdout);
	ull a,b; cin>>a>>b; cout<<((a&(~b))|((~a)&b))<<endl;
}
bool memoryend;
void perf(){
	cout<<"CPU time used:"<<fs(3)<<(double)clock()/CLOCKS_PER_SEC<<"s."<<endl;
	cout<<"Memory used:"<<fs(3)<<abs((&memoryend-&memorybegin)*1.0/1024.0/1024.0)<<"MB."<<endl;
}

随机数据生成器:

C++
#include <bits/stdc++.h>
using namespace std;
#define upp(x,y,z) for(x=(y);x<=(z);++x)
#define low(x,y,z) for(x=(y);x>=(z);--x)
#define tt template<typename t0>
#define fs(x) fixed<<setprecision(x)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ull unsigned long long
#define debug(x) cout<<"Line "<<__LINE__<<","<<(x)<<"."<<endl
tt inline void chkmax(t0 &x,t0 y){if(x<y){x=y;}}
tt inline void chkmin(t0 &x,t0 y){if(x>y){x=y;}}
const int eps=0xf; const int inf=0x7fffffff; const ll infll=0x7fffffffffffffff; const ull infull=0xffffffffffffffff;
const int N=1e6+eps;
void code(); void perf();
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); code(); //perf();
	return 0;
}
bool memorybegin;
void code(){
	freopen("random.in","w",stdout);
	srand(time(0)); ull a,b,c,d; a=rand(); b=rand(); c=rand(); d=rand();
	cout<<a*b<<" "<<c*d<<endl;
}
bool memoryend;
void perf(){
	cout<<"CPU time used:"<<fs(3)<<(double)clock()/CLOCKS_PER_SEC<<"s."<<endl;
	cout<<"Memory used:"<<fs(3)<<abs((&memoryend-&memorybegin)*1.0/1024.0/1024.0)<<"MB."<<endl;
}

对拍核心程序(Windows 11 下可用):

C++
#include <bits/stdc++.h>
using namespace std;
#define upp(x,y,z) for(x=(y);x<=(z);++x)
#define low(x,y,z) for(x=(y);x>=(z);--x)
#define tt template<typename t0>
#define fs(x) fixed<<setprecision(x)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ull unsigned long long
#define debug(x) cout<<"Line "<<__LINE__<<","<<(x)<<"."<<endl
tt inline void chkmax(t0 &x,t0 y){if(x<y){x=y;}}
tt inline void chkmin(t0 &x,t0 y){if(x>y){x=y;}}
const int eps=0xf; const int inf=0x7fffffff; const ll infll=0x7fffffffffffffff; const ull infull=0xffffffffffffffff;
const int N=1e6+eps;
void code(); void perf();
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); code(); //perf();
	return 0;
}
bool memorybegin;
void code(){
	int T=10; int i; double t1,t2;
	freopen("bat.out","w",stdout);
	upp(i,1,T){
		system("random.exe");
		t1=(double)clock()/CLOCKS_PER_SEC; system("solution.exe");
		t2=(double)clock()/CLOCKS_PER_SEC; system("correct.exe");
		if(system("fc solution.out correct.out")){
			cout<<"WA in #"<<i<<"."<<endl; return;
		}
		cout<<"AC in #"<<i<<", time used:"<<fs(3)<<t2-t1<<"s."<<endl;
	}
}
bool memoryend;
void perf(){
	cout<<"CPU time used:"<<fs(3)<<(double)clock()/CLOCKS_PER_SEC<<"s."<<endl;
	cout<<"Memory used:"<<fs(3)<<abs((&memoryend-&memorybegin)*1.0/1024.0/1024.0)<<"MB."<<endl;
}

规范

  • 要尽量压行,每个代码块之间空一个空格。
例子

要尽量写成:

C++
int a,b; cin>>a; cin>>b; cout<<a+b<<endl;

而不要写成:

C++
int a,b;
cin>>a;
cin>>b;
cout<<a+b<<endl;
  • 括号里内容比较少就不换行。
例子

如板子中的 chkmax\text{chkmax} 和 chkmin\text{chkmin} 。

一些标识符

pow2[i]表示 2i{2}^{i} 的值。

lg2[i]表示 log⁡2i\log_2i 的值。

原文