#include <iostream>
using namespace std;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a; //gcd(a,b) = gcd(b,a%b) gcd(a,0) = a
}
int main()
{
int a, b;
cin >> a >> b;
cout << gcd(a, b);
}
2020.2
求第k大的元素
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int quick_sort(int l, int r,int q[], int k)
{
if (l == r)
return q[l];
int mid = (l + r) >> 1;
int x = q[mid];
int i = l - 1;
int j = r + 1;
while (i < j)
{
do i++; while (q[i] < x);
do j--; while (q[j] > x);
if (i < j) swap(q[i], q[j]);
}
int s = j - l + 1;
if (s < k)
return quick_sort(j + 1, r, q, k - s);
else
return quick_sort(l, j, q, k);
}
int main()
{
ifstream ifs("array.in");
int n, k;
const int N = 100010;
ifs >> n >> k;
int q[N];
for (int i = 0; i < n; i++)
ifs >> q[i];
cout << quick_sort(0,n - 1, q, k);
}
2020.3
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
static bool cmp(const string & a, const string & b)
{
return a + b > b + a; //使ab > ba即可 可以证明这样使abcdef最大
}
int main()
{
ifstream ifs("number.in");
vector<string> strs;
string s;
while (ifs >> s)
{
strs.push_back(s);
}
sort(strs.begin(), strs.end(), cmp);
for (auto str : strs)
cout << str;
}