白板上左右写上两个数,每个字的形式表示为:一个正整数 \(x\) 和 \(p\) 个末尾 \(0\) 。
询问左右两个数的大小关系。
假设左边为 \(x_1, p_1\) ,右边为 \(x_2, p_2\) 。
先看数位长度
- 若 \(dig_{x_1} + p_1 < dig_{x_2} + p_2\) ,则关系为 \(<\) 。
- 若 \(dig_{x_1} + p_1 > dig_{x_2} + p_2\) ,则关系为 \(>\) 。
否则,数位长度相等,可以使用字典序比较。不妨拆分 \(x_1, x_2\) 的数位,对 \(x_1, x_2\) 补末尾 \(0\) 到数位为 \(max_{x_1.length, x_2.length}\) 。从左往右扫。
- 若 \(x_1[i] > x_2[i]\) ,则 \(>\) ,终止循环。
- 若 \(x_1[i] < x_2[i]\) ,则 \(<\) ,终止循环。
- 未层终止循环则 \(=\) 。
view
#include <bits/stdc++.h>
void solve() {
int x1, p1; std::cin >> x1 >> p1;
int x2, p2; std::cin >> x2 >> p2;
auto get_digt = [&](int x) {
int cnt = 0;
while (x) {cnt++;x/=10;}
return cnt;
};
if (get_digt(x1) + p1 > get_digt(x2) + p2) std::cout << ">" << '\n';
else if (get_digt(x1) + p1 < get_digt(x2) + p2) std::cout << "<" << '\n';
else {
std::vector<int> d1, d2;
while (x1) {
d1.push_back(x1 % 10);
x1 /= 10;
}
std::reverse(d1.begin(),d1.end());
while (x2) {
d2.push_back(x2 % 10);
x2 /= 10;
}
std::reverse(d2.begin(),d2.end());
while (d1.size() < d2.size()) d1.push_back(0);
while (d2.size() < d1.size()) d2.push_back(0);
int m = d1.size();
for (int i = 0; i < m; i++) {
if (d1[i] < d2[i]) {
std::cout << "<" << '\n';
return;
}
else if (d1[i] > d2[i]) {
std::cout << ">" << '\n';
return;
}
}
std::cout << "=" << '\n';
}
}
signed main() {
int _ = 1; std::cin >> _;
while (_--) solve();
return 0;
}
- Educational Codeforces Comparison Round Ratededucational codeforces round rated educational codeforces comparison round round codeforces rated based educational codeforces comparison string educational codeforces round 151 construction educational codeforces round educational codeforces round 147 cf-educational educational codeforces round educational codeforces round 158 educational codeforces contest round