本人的代码风格

本文是自 2020 年起开始记录的日志,旨在研究本人的 C++ 代码风格变化。

全部程序都只是输入 \(n\) 个数,然后排序,再顺序输出。只是为了演示代码风格变化。

本人至今仍然有保留这个记录的习惯,不过因为码风趋于稳固,变更的频率也明显下降了。

由于代码风格在复杂代码中更容易体现,所以本文并不能将全部变化一一列出,仅供参考。

2020 年 上半年

这个时期初学 C++ 语法,能跑就行。

#include<iostream>
using namespace std;
int main() {
    int a[11],n;
    cin>>n;
    for(int i = 0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    for(int i = 0;i<n;i++) cout<<a[i]<<" ";
    return 0;
}

2020 年 下半年

这个时期堪称万恶之源,开始 #define int long long 。

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
int a[25];
signed main()
{
  	ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=1; i<=n; i++)
        cout<<a[i]<<" ";
    return 0;
}

2021 年 上半年

这个时期在初步尝试优化常数。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define R register
int a[105],n;
signed main()
{
    scanf("%lld",&n);
    for(R int i=1; i<=n; i++)
        scanf("%lld",&a[i]);
    sort(a+1,a+1+n);
    for(R int i=1; i<=n; i++)
        printf("%lld%c",a[i]," \n"[i==n]);
    return 0;
}

2021 年 下半年

这个时期才刚刚学会最普通的快读快写,通常采用 C 风格读写。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define gc() getchar()
#define pc(a) putchar(a)
template<typename T>inline void read(T &k)
{
	char ch=gc();T x=0,f=1;
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
	k=x*f;
}
template<typename T>inline void write(T k)
{
	if(k<0){k=-k;pc('-');}
	if(k>9)write(k/10);
	pc(k%10+'0');
}
#define MAXN (int)(1e5+5)
int n,a[MAXN];
signed main()
{
	read(n);
	for(int i=1; i<=n; i++)
		read(a[i]);
	sort(a+1,a+1+n);
	for(int i=1; i<=n; i++)
		write(a[i]),pc(" \n"[i==n]);
	return 0;
}

2022 年 上半年

这个时期开始向 C++ 风格转型。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
namespace FastIO
{
    #define gc() readchar()
    #define pc(a) putchar(a)
    #define SIZ (int)(1e6+15)
    char buf1[SIZ],*p1,*p2;
    char readchar()
    {
        if(p1==p2)p1=buf1,p2=buf1+fread(buf1,1,SIZ,stdin);
        return p1==p2?EOF:*p1++;
    }
    template<typename T>void read(T &k)
    {
        char ch=gc();T x=0,f=1;
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}
        while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
        k=x*f;
    }
    template<typename T>void write(T k)
    {
        if(k<0){k=-k;pc('-');}
        static T stk[66];T top=0;
        do{stk[top++]=k%10,k/=10;}while(k);
        while(top){pc(stk[--top]+'0');}
    }
}using namespace FastIO;
#define N (int)(1e5+15)

int n,a[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    cin >> n;
    for(int i=1; i<=n; i++)
        cin >> a[i];
    sort(a+1,a+1+n);
    for(int i=1; i<=n; i++)
        cout << a[i] << " \n"[i==n];
    return 0;
}

2022 年 下半年

这个时期进步飞快,基本构成之后几年的风格。

开始排斥无大括号的换行,偏好强迫症式等长排版风格。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
typedef pair<int,int> pii;
void up(int &x,int y) { x < y ? x = y : 0; }
void down(int &x,int y) { x > y ? x = y : 0; }
#define Fi first
#define Se second
#define N ((int)(2e5+15))

int n,a[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    cin >> n; for(int i=1; i<=n; i++) cin >> a[i];
    sort(a + 1, a + 1 + n);
    for(int i=1; i<=n; i++) cout << a[i] << " \n"[i == n];
    return 0;
}

2023 年 上半年

这个时期彻底转向超多空格的排版模式。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
typedef pair<int,int> pii;
void up(int &x,int y) { x < y ? x = y : 0; }
void down(int &x,int y) { x > y ? x = y : 0; }
#define Fi first
#define Se second
#define N ((int)(2e5+15))

int n, a[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    cin >> n; for(int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n];
    return 0;
}

2023 年 下半年

这个时期其实没有明显变化。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
void up(int &x,int y) { x < y ? x = y : 0; }
void down(int &x,int y) { x > y ? x = y : 0; }
#define N ((int)(2e5 + 15))

int n, a[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n];
    return 0;
}

2024 年 上半年

开始使用宏加速写循环语句的速度,开始尝试脱离 #define int long long ,尝试减少全局变量。

#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define INF 0x3f3f3f3f3f3f3f3f
typedef long long ll;
void up(int &x, int y) { x < y ? x = y : 0; }
void down(int &x, int y) { x > y ? x = y : 0; }
#define rep(i, a, b) for(int i = (a), i##END = (b); i <= i##END; i++)
#define Rep(i, a, b) for(int i = (a), i##END = (b); i >= i##END; i--)
#define N ((int)(1e5 + 15))

int a[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    int n; cin >> n; rep(i, 1, n) cin >> a[i];
    sort(a + 1, a + 1 + n);
    rep(i, 1, n) cout << a[i] << " \n"[i == n];
    return 0;
}

「空位时期」

因为本人的身心健康状况每况愈下,所以这个时期很少有写代码,自己没有什么明显的风格变化。

2026 年 下半年至今

触底反弹的时期,身心健康依旧堪忧,不过稍微比之前好了一些。

这个时期开始转向更清晰可靠的代码风格,试图在强迫症、完美主义和代码准确性之间取得平衡。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
template<typename T> void up(T &x, T y) { x < y ? x = y : 0; }
template<typename T> void down(T &x, T y) { x > y ? x = y : 0; }
#define rep(i, a, b) for(int i = (a), i##END = (b); i <= i##END; i++)
#define Rep(i, a, b) for(int i = (a), i##END = (b); i >= i##END; i--)
#define N ((int)(1e5 + 15))

int n, a[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    cin >> n;
    rep(i, 1, n) cin >> a[i];
    sort(a + 1, a + 1 + n);
    rep(i, 1, n) cout << a[i] << " \n"[i == n];
    return 0;
}