/* 归并排序:不断将数组切分为两部分递归切分直到数组长度为1,之后逐渐有序合并 */ #include <cstdio> #include <iostream> #include <cmath> using namespace std;//归并 小-->大 void Merge(int a[], int s, int m, int e, int tmp[]) {int iter = s;int it1 = s, it2 = m+1;while(it1 <=m && it2 <= e) {if(a[it1] < a[it2]) tmp[iter++] = a[it1++];else tmp[iter++] = a[it2++];}while(it1 <= m)tmp[iter++] = a[it1++];while(it2 <= e)tmp[iter++] = a[it2++];for(int i=s; i<=e; ++i) {a[i] = tmp[i];}return; } void MergeSort(int a[], int s, int e, int tmp[]) {if(s < e) {int m = s + (e-s)/2;MergeSort(a, s, m, tmp);MergeSort(a, m+1, e, tmp);Merge(a, s, m, e, tmp);}return; } int a[] = {13, 12, 23, 5, 2, 34, 11, 34, 13}; int tmp[100]; int main() {int size = sizeof(a) / sizeof(int);MergeSort(a, 0, size-1, tmp);for(int i=0; i<size; ++i)printf("%d ", a[i]); }
/*快排:主要思想,寻找切分元素之后两边分治处理 */ #include <iostream> #include <cstdio> using namespace std; const int maxn = 1e5 + 10; int num[maxn]; int n; inline void quik_sort(int s, int e) {//mid为切分元素int mid = num[s+(e-s)/2];// i 左侧指针,找到第一个不小于切分元素mid的数,i指向>=mid// j 右侧指针,找到第一个不大于切分元素mid的数,j指向<=midint i = s, j = e;while(i <= j) {while(num[i] < mid) i += 1;while(num[j] > mid) j -= 1;if(j >= i) {swap(num[i], num[j]);i += 1, j -= 1;}}//跳出后j < i, 分成两部分继续if(s < j) quik_sort(s, j);if(e > i) quik_sort(i, e);return; } int main() {scanf("%d", &n);for(int i=1; i<=n; ++i) scanf("%d", num+i);quik_sort(1, n);for (int i = 1; i <= n; ++i){printf("%d ", num[i]); }return 0; }
#include <cstdio> #include <iostream> using namespace std; int Mod ;//将指数二进制化,也可以尝试其他进制 int quick_pow(int base, int t) {int ans = 1;while(t) {if(t & 1) {ans = (ans%Mod)*(base%Mod);
ans %= Mod;}base = (base*base) % Mod;t >>= 1;}return ans; } int main() {int base, t;scanf("%d %d %d", &base, &t, &Mod);printf("%d^%d mod %d=%d\n", base, t, Mod, quick_pow(base, t));return 0; }