-
Bio
判断是否为回文数
bool isPalindrome(int num) { int original = num; int reversed = 0; while (num > 0) { reversed = reversed * 10 + num % 10; num /= 10; } return original == reversed; }
判断是否为素数
bool isPrime(int num) { if (num <= 1) return false; for (int i = 2; i <= std::sqrt(num); ++i) { if (num % i == 0) return false; } return true; }
冒泡排序
for(int i = 0;i < n-1;i++){ for(int j = 0;j < n-i-1;j++){ if(a[j] < a[j+1]){//升序把 < 变为 > int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp;//降序 }//可用swap } }
冒泡排序 #2
//需要头文件#<algorithm> sort(数组名字+起始位置,数组名字+结束位置+1,cmp)//cmp可有可无,没有就是升序
辅助内置(土拨鼠的馈赠)
using namespace std; #define endl '\\n' #define TRACE 1 #define tcout TRACE && cout #define fst ios::sync\_with\_stdio(false); cin.tie(0); cout.tie(0); #define int long long const int P = 998244353; const int Base = 3221225477; const int INF = 0x3f3f3f3f3f3f3f3f; const int N = 1e6 + 10, M = 2e6 + 10; signed main() { }
判断字符串回文
bool f(string s){ int n = s.length(); int l = 0;int r = n-1; for(int i = 0;i < n;i++) { if(s[l] != s[r]){ return false; } l++;r--; } return true; }
cmp内置
bool cmp(int a,int b) { if(x > y){ return 1; }else{ return 0; } }
快速排序模板
using namespace std;//没有swap用不了 int part(int* r, int low, int hight) //划分函数 { int i = low, j = hight, pivot = r[low]; //基准元素 while (i < j) { while (i<j && r[j]>pivot) //从右向左开始找一个 小于等于 pivot的数值 { j--; } if (i < j) { std::swap(r[i++], r[j]); //r[i]和r[j]交换后 i 向右移动一位 } while (i < j && r[i] <= pivot) //从左向右开始找一个 大于 pivot的数值 { i++; } if (i < j) { std::swap(r[i], r[j--]); //r[i]和r[j]交换后 i 向左移动一位 } } return i; //返回最终划分完成后基准元素所在的位置 } void Quicksort(int* r, int low, int hight)//参数:Quicksort(数据名称,起始位置,终止位置);` { int mid; if (low < hight) { mid = part(r, low, hight); // 返回基准元素位置 Quicksort(r, low, mid - 1); // 左区间递归快速排序 Quicksort(r, mid+1, hight); // 右区间递归快速排序 } }
堆排序
#include <iostream> #include <algorithm> using namespace std; void max_heapify(int arr[], int start, int end) { int dad = start; int son = dad * 2 + 1; while (son <= end) { / if (son + 1 <= end && arr[son] < arr[son + 1]) son++; if (arr[dad] > arr[son]) return; else { swap(arr[dad], arr[son]); dad = son; son = dad * 2 + 1; } } } void heap_sort(int arr[], int len) { for (int i = len / 2 - 1; i >= 0; i--) max_heapify(arr, i, len - 1); for (int i = len - 1; i > 0; i--) { swap(arr[0], arr[i]); max_heapify(arr, 0, i - 1); } }
求各数位数字之和
int sum(int n) { int ans = 0; while(n != 0) { ans += n%10; n/=10; } return ans; }
-
Accepted Problems
-
Recent Activities
This person is lazy and didn't join any contests or homework. -
Recent Solutions
This person is lazy and didn't write any solutions. -
Stat
-
Rating
Problem Tags
- CSP-J
- 4
- 2019
- 2
- 2020
- 2
- math
- 1
- *1000
- 1