求最大公约数
int gcd(int a, int b) { if (a == 0 || b == 0) return 0; int tmp = 0; while (b != 0) { tmp = b; b = a%b; a = tmp; } return a; }
交换swap
template <class T> void swap(T &a, T &b) { T c(std::move(a)); a = std::move(b); b = std::move(c); }
a = a ^ b; b = a ^ b; a = a ^ b;
求某个区间随机数
int RandomlnRange() { if (start > end) return -1; return rand() % (end - start + 1) + start; }
分割数组Partition
int Partition_v1(vector<int>& num, int low, int high) { int pivot = num[low]; while (low < high) { while (low < high && num[high] >= pivot) --high; num[low] = num[high]; while (low < high && num[low] <= pivot) ++low; num[high] = num[low]; } num[low] = pivot; return low; }
分解质因数(面试题考过)
void Analyse(int n) { //打印出 int i; for (i = 2; i <= sqrt(static_cast<double>(n)); i++) { if (n % i == 0) { n = n / i; cout << i << "*"; i--; } } cout << n << endl; }
不用加号计算两数和
int add(int a, int b) { if (b == 0) return a; int sum = a^b; int carry = (a&b) << 1; return add(sum, carry); }
strcpy
char *strcpy(char *strDest, const char *strSrc) { assert(strDest != NULL && strSrc != NULL); char *address = strDest; while ((*strDest++ = *strSrc++) != '