最近偶然刷到了B站UP主 Erik_Tse 的视频,了解到了一些代码抽象艺术,感觉很有意思,
所以特地开篇博客总结记录一下,欢迎大家补充~
抽象小技巧
- 使用奇怪的变量名
- 滥用宏定义
- 简单操作复杂化
- 。。。。。。
抽象案例
用位运算代替加法
a + b = a ^ b + ((a & b) << 1)
1
2
3
4
5
|
// 计算 x + y
int add(int a,int a){
if (!a) return b;
return add((x&y)<<1, x^y);
}
|
使用I和l组合作为变量名
1
2
3
4
5
6
|
int main(){
int IIIIIlllllIlIlllIIIIIlllllIlIlll;
cin >> IIIIIlllllIlIlllIIIIIlllllIlIlll;
cout << IIIIIlllllIlIlll << '\n';
return 0;
}
|
使用奇怪宏定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <iostream>
#define false 1
#define true 0
#define _ int
#define ___ 1
using namespace std;
_ main(){
_ llllIlIlllIIIIllIlIl;cin >> llllIlIlllIIIIllIlIl;
_ llllIlIlllIlllIlIlllIIIII = 0;
for (_ __ = ___;__ <= llllIlIlllIIIIllIlIl / __;__ += false){
if (llllIlIlllIIIIllIlIl % __ == 0){
llllIlIlllIlllIlIlllIIIII += false;
if (__ != llllIlIlllIIIIllIlIl){
llllIlIlllIlllIlIlllIIIII += false;
}
}
}
cout << llllIlIlllIlllIlIlllIIIII;
return 0;
}
|
让人摸不着头脑
尊嘟假嘟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <iostream>
#define false 1
#define true 0
using namespace std;
struct ZDJD{
bool O(bool x){
return x ? true : false;
}
}o;
int mian(){
int a[] = {1, 2, 3, 4, 5};
int ans = 0;
for (int i = 0; i < 5; i++){
if(o.O(*&(i - 1)[i + 1] % 2 == 0)){
ans++;
}
}
cout << ans << '\n';
return 0;
}
|
抽象实践
题目:求数组内偶数个数:
思路:遍历数组,挨个判断元素是否为偶数
正常代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
using namespace std;
int mian(){
int a[] = {1, 2, 3, 4, 5};
int ans = 0;
for(int i = 0;i < 5;i++){
if (a[i] % 2 == 0){
ans++;
}
}
cout << ans << '\n';
return 0;
}
|
抽象代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include <iostream>
#define false 1
#define true 0
#define _ int
#define ___ 0
using namespace std;
struct ZDJD{
bool O(bool IIllIIlllIIIIIIllIlIll){
return IIllIIlllIIIIIIllIlIll ? true : false;
}
}o;
_ mian(){
_ IIIIIllIIlllllIIIl[] = {1, 2, 3, 4, 5};
_ IIIIIllIIIIIIllIlIlllIIlIlI = ___;
for(_ __ = ___;__ < 5;__-=0xffffffff){
if (o.O(*&(__ - 1)[__ + 1] % 2 == 0)) {
IIIIIllIIIIIIllIlIlllIIlIlI-=0xffffffff;
}
}
cout << IIIIIllIIIIIIllIlIlllIIlIlI << '\n'
return 0;
}
|