關閉同步流
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
求二進制 1的個數。
int n = 15; //二進制爲1111 二進制1的個數
cout<<__builtin_popcount(n)<<endl;//輸出4
int n = 15;//二進制爲1111 //求二進制奇偶
int m = 7;//111
cout<<__builtin_parity(n)<<endl;//偶數個,輸出0
cout<<__builtin_parity(m)<<endl;//奇數個,輸出1
int n = 1;//1 //求二進制最後一個1的位置
int m = 8;//1000
cout<<__builtin_ffs(n)<<endl;//輸出1
cout<<__builtin_ffs(m)<<endl;//輸出4
int n = 1;//1 //求二進制末尾0的個數
int m = 8;//1000
cout<<__builtin_ctzll(n)<<endl;//輸出0
cout<<__builtin_ctz(m)<<endl;//輸出3
__builtin_clz (unsigned int x) //求二進制前導零的個數
//判斷longlong 後面加個 ll
__builtin_popcountll(111111111111111111);
全排列
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e3+5;
int k[maxn][maxn];
int n;
int main() {
for(n=1;n<=12;n++) {
memset(k,0, sizeof(k));
int ans[12] = {1, 2, 3, 4, 5, 6, 7, 8,9,10,11,12};
do {
int flag=1;
for (int i = 1; i < n; i++) {
if(abs(ans[i]-ans[i-1])>2)flag=0;
}
if(flag)k[ans[0]][ans[n-1]]++;
} while (next_permutation(ans, ans + n));
for(int i=1;i<n;i++){
for(int j=i+1;j<n;j++){
printf("%d ",k[i][j]);
}
puts("");
}
}
return 0;
}
求三角斜邊
hypot(3.0,4.0);
Hash Mod 數
1610612741
402653189
201326611
C++ 控制精度
cout<<fixed<<setprecision(20)<<ans<<"\n";
Unordermap與map
Unordermap 快速查找慢建立
Map 快速建立慢查找
Multiset刪除操作
刪除迭代器刪除指定位置
刪除值,刪除所有與之相等的值
Int128的使用
//typedef __int128 LLL;
template<typename T>
void read(T &w) {//讀入
char c, k = 1;
while (!isdigit(c = getchar()))c == '-' ? k = -1 : k = k;
w = c & 15;
while (isdigit(c = getchar()))
w = w * 10 + (c & 15);
w *= k;
}
template<typename T>
void output(T x) {
if (x < 0)
putchar('-'), x = -x;
int ss[55], sp = 0;
do
ss[++sp] = x % 10;
while (x /= 10);
while (sp)
putchar(48 + ss[sp--]);
}
修改對象對齊問題
對齊主要處理兼容,和速度問題。
struct student
{
char name[7];
uint32_t id;
char subject[5];
} __attribute__ ((aligned(4)));
struct student
{
char name[7];
uint32_t id;
char subject[5];
} __attribute__ ((packed));
#pragma pack(n)
#pragma pack()
inline
inline 建議內聯,默認是局部可見,不可extern。