一、朴素解法
#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
typedef long long LL;
struct Person {
int first, second;
} p[N];
int n;
bool cmp(const Person &a, const Person &b) {
return a.first * a.second < b.second * b.first;
}
int main() {
cin >> n >> p[0].first >> p[0].second; //n:表示大臣的人数;a,b:分别表示国王左手和右手上的整数
for (int i = 1; i <= n; i++)cin >> p[i].first >> p[i].second;
sort(p + 1, p + 1 + n, cmp);
LL sum = 1;
LL MAX = 0;
for (int i = 0; i <= n; i++) {
MAX = max(MAX, sum / p[i].second);
sum *= p[i].first;
}
cout << MAX << endl;
return 0;
}
二、高精度题解
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
struct Person {
int left, right;
} person[N];
bool cmp(const Person &a, const Person &b) {
return a.left * a.right < b.left * b.right;
}
//高精度乘法
vector<int> mul(vector<int> &A, int b) {
vector<int> C;
int t = 0;
for (int i = 0; i < A.size() || t; i++) {
if (i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
//高精度除法
vector<int> div(vector<int> &A, int b, int &r) {
vector<int> C;
r = 0;
for (int i = A.size() - 1; i >= 0; i--) {
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
}
reverse(C.begin(), C.end());
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
//获取两个vector<int>中较大的那个
vector<int> max_vec(vector<int> a, vector<int> b) {
if (a.size() > b.size()) return a;
if (a.size() < b.size()) return b;
if (vector<int>(a.rbegin(), a.rend()) > (vector<int>(b.rbegin(), b.rend()))) return a;
return b;
}
int main() {
cin >> n;
//输入
for (int i = 0; i <= n; i++) cin >> person[i].left >> person[i].right;
//排序
sort(person + 1, person + n + 1, cmp);
//队伍中放入国王
vector<int> sum(1, person[0].left);
//结果
vector<int> res(1, 0);
int r;
for (int i = 1; i <= n; i++) {
res = max_vec(res, div(sum, person[i].right, r));
sum = mul(sum, person[i].left);
}
//输出
for (int i = res.size() - 1; i >= 0; i--) cout << res[i];
}