1.排序:
快排
#include <iostream>
#include <vector>
using namespace std;
//以左边第一个数为标志进行划分
int partition(vector<int> &a, int l, int r) {
int p = a[l];
while (l < r)//不断循环直到左右指针相撞。
{
while (a[r] > p && l < r) r--; //从右边开始找到第一个比P小的数
if (l < r)
{
a[l] = a[r];//找到后放在原先标志p的位置,左指针后移
l++;
}
while (a[l] < p && l < r) l++;//继续从左开始找比P大的数。
if (l < r)
{
a[r] = a[l];//放到之前的位置。
r--;
}
}
a[l] = p; //r 和 l相同
return l;
}
//递归进行快排
void quick_sort(vector<int> &a, int l, int r) {
if (l < r) {
int p = partition(a, l, r);
quick_sort(a, l, p - 1);
quick_sort(a, p + 1, r);
}
}
int main() {
ios::sync_with_stdio(false);
cout.tie(NULL);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
quick_sort(v, 0, n - 1);
for (int i = 0; i < n; i++)
{
if (i == 0) cout << v[0];
else cout << ' ' << v[i];
}
return 0;
}
归并
#include <iostream>
#include <vector>
using namespace std;
//确定中点
//排序左右
//归并
vector<int> tmp, v;
void merge_sort(vector<int> &a, int l, int r) {
if (r <= l) return;
int mid = (l + r) >> 1;
//先递归到最左与最右
merge_sort(a, l, mid);
merge_sort(a, mid + 1, r);
int i = l, j = mid + 1, k = 0;//i 左半边的起点,j 右半边的起点, k 辅助数组的索引
while (i <= mid && j <= r)
if (a[i] <= a[j]) tmp[k++] = a[i++];
else tmp[k++] = a[j++];
while (i <= mid) tmp[k++] = a[i++];
while (j <= r) tmp[k++] = a[j++];
for (i = l, k = 0; i <= r; i++, k++) a[i] = tmp[k];
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
tmp.resize(n);
v.resize(n);
for (int i = 0; i < n; i++)
cin >> v[i];
merge_sort(v, 0, n - 1);
for (int i = 0; i < n; i++)
{
if (i == 0) cout << v[0];
else cout << ' ' << v[i];
}
return 0;
}
整数二分法(求边界)
#include <iostream>
using namespace std;
const int N = 100010;
int n, m;
int q[N];
int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++) scanf("%d", &q[i]);
while(m--)
{
int x;
scanf("%d", &x);
//左边界
int l = 0, r = n-1;
while(l < r)
{
int mid = l + r >> 1;//这里不需要加1。判断要不要加1,看r-l = 1时会不会死循环。
if(q[mid] >= x) r = mid;//r-l = 1时r赋值为l
else l = mid + 1;//r-l = 1时l赋值为l+1即r
}
if(q[l] != x) cout<<"-1 -1"<<endl;
else
{
cout<<l<<' ';
//右边界
l = 0, r = n-1;
while(l < r)
{
int mid = l+r+1 >> 1;//这里要+1。r-l = 1时,不加1 l = mid = l。死循环
if(q[mid] <= x) l = mid;
else r = mid - 1;
}
cout << l << endl;
}
}
return 0;
}
高精度(+-*/)
加法:
#include <iostream>
#include <vector>
using namespace std;
vector<int> add(vector<int>& A, vector<int>& B) {
vector<int> C;
int t = 0;
for (int i = 0; i < A.size() || i < B.size(); i++)
{
if (i < A.size()) t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t) C.push_back(1);
return C;
}
int main() {
string a, b;
vector<int> A, B;
cin >> a >> b;
for (int i = a.size() - 1; i >= 0; i--)
A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--)
B.push_back(b[i] - '0');
auto C = add(A, B);
for (int i = C.size() - 1; i >= 0; i--) cout << C[i];
return 0;
}
减法:
#include <iostream>
#include <vector>
using namespace std;
const int N = 1e6 + 10;
vector<int> sub(vector<int>& A, vector<int>& B)
{
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i++)
{
t = A[i] - t;
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t < 0) t = 1;
else t = 0;
}
while (C.size() > 1 && C.back() == 0)C.pop_back();
return C;
}
bool cmp(vector<int> a, vector<int> b)
{
if (a.size() != b.size()) return a.size() > b.size();
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != b[i]) return a[i] > b[i];
return true;
}
int main() {
string a, b;
vector<int> A, B;
cin >> a >> b;
for (int i = a.size() - 1; i >= 0; i--)
A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--)
B.push_back(b[i] - '0');
if (cmp(A, B))
{
auto C = sub(A, B);
for (int i = C.size() - 1; i >= 0; i--) cout << C[i];
}
else
{
cout << '-';
auto C = sub(B, A);
for (int i = C.size() - 1; i >= 0; i--) cout << C[i];
}
return 0;
}