码风记录(2022/10)
Untitled10032 · · 个人记录
趁比较闲记录一下现在的码风。很多之前写的代码都找不着了,看不到我小时候在主函数里开大数组的样子了,就很遗憾。
好多都是从 这位大佬的博客 里学来的。
-
不会
#include <bits/stdc++.h>,头文件要用哪个就写哪个。 -
使用
using namespace std;想在考完这次 csp 之后去掉。 -
输出方式在
cin/cout和scanf/getchar/printf/puts/putchar之间摇摆不定,但如果用cin/cout一定会关同步(ios::sync_with_stdio(false);)。 -
所有前大括号不换行。
-
变量和操作符之间有空格(下标中括号、
::等特殊情况不空格)。 -
所有变量名都用英文(或英文缩写),不用汉语拼音。不知道就上网查。
-
if、for、while和后面的小括号之间有一个空格,但声明函数时变量名与参数列表的小括号之间没有空格。 -
if、for、while等 和 声明函数时 小括号和前大括号之间有空格。 -
不用
goto。 -
一般不压行。除了一些
dfs里面很短的continue或者break。 -
数据范围最大值可能用
#define也可能用constexpr int。之前起名老是用maxn,后来看很少有人这么用就改成大写N了。 -
长变量名用驼峰命名法。一些用来标记的
bool值和大模拟里的变量会用长变量名。 -
自定义数据类型(
struct、class)名字首字母需要大写。 -
高精度会用面向对象的方式封装起来。
-
if和某些赋值语句如果很长会多换几行。 -
#include喜欢用头文件名字长度从长到短(或从短到长)排起来。 -
dp数组名用
f,存返回值常用ret和res,存答案常用ans和res,循环用 i、j、k(几乎是废话)。
比较有代表性的代码(题目:T267961 序列整理):
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
constexpr int maxNumLen = 110;
class __int114514_t {
public:
int digits[maxNumLen];
int len;
__int114514_t() {
len = 0;
memset(digits, 0, sizeof(digits));
}
void read() {
char t = getchar();
while (t < '0' || t > '9') t = getchar();
for (; t >= '0' && t <= '9'; t = getchar(), len++)
digits[len] = t ^ '0';
reverse(digits, digits + len);
}
void write() {
if (len == 0)
putchar('0');
else
for (int i = len - 1; i >= 0; putchar(digits[i--] | '0'));
putchar('\n');
}
int& operator [] (int x) {
return digits[x];
}
};
bool operator >= (__int114514_t x, __int114514_t y) {
if (x.len != y.len)
return x.len > y.len;
for (int i = x.len - 1; i >= 0; i--)
if (x[i] != y[i])
return x[i] > y[i];
return true;
}
__int114514_t operator + (__int114514_t x, __int114514_t y) {
const int mxlen = max(x.len, y.len) + 1;
for (int i = 0; i < mxlen; i++)
x[i] += y[i];
x.len = 0;
for (int i = 0; i < mxlen; i++) {
x[i + 1] += x[i] / 10;
x[i] %= 10;
if (x[i])
x.len = i;
}
x.len++;
return x;
}
__int114514_t operator - (__int114514_t x, __int114514_t y) {
const int mxlen = max(x.len, y.len);
for (int i = 0; i < mxlen; i++)
x[i] -= y[i];
x.len = 0;
for (int i = 0; i < mxlen; i++) {
if (x[i] < 0) {
x[i + 1]--;
x[i] += 10;
}
if (x[i])
x.len = i;
}
x.len++;
return x;
}
constexpr int N = 502;
__int114514_t a[N], f[N];
void update(__int114514_t &dst, __int114514_t src) {
if (src >= dst)
dst = src;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
a[i].read();
f[0] = a[0];
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
if (a[i] >= a[j])
update(f[i], f[j] + a[i]);
__int114514_t sum, res;
for (int i = 0; i < n; i++) {
sum = sum + a[i];
update(res, f[i]);
}
(sum - res).write();
return 0;
}
关于 __int114514_t:第一次写臭精度,一般都会用 BigInteger。
update:找到了好久以前写的代码(2020/10/07)(还是在码酷上)
功能:高精度加法
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
char a[1024], b[1024];
//ifstream cin("add.in");
//ofstream cout("add.out");
void qhjiaohuan(char in[], char c) //前后交换
{
int i=0, j=strlen(in)-1;
while(i<strlen(in))
{
if(c=='a') a[i]=in[j];
else b[i]=in[j];
i++;
j--;
}
}
int main(int argc, char *argv[])
{
int i, j, lena, lenb;
char a1[1024], b1[1024];
bool aaa=false;
cin>>a1>>b1;
qhjiaohuan(a1, 'a');
qhjiaohuan(b1, 'b');
lena=strlen(a);
lenb=strlen(b);
if(lena>lenb)
for(i=lenb; i<lena; i++)
b[i]='0';
else if(lenb>lena)
for(i=lena; i<lenb; i++)
a[i]='0';
else
i=lena;
a[i]=b[i]='0';
a[i+1]=b[i+1]='\0';
for(i=0; i<strlen(a); i++)
{
if(a[i]-'0'+b[i]-'0'<10)
a[i]=a[i]+b[i]-'0';
else
{
a[i]=a[i]+b[i]-'0'-10;
a[i+1]++;
}
}
a[i]='\0';
for(i=strlen(a)-1; i>=0; i--)
if(a[i]!='0'||aaa!=false)
{
aaa=true;
cout<<a[i];
}
else
aaa=true;
return 0;
}