2023 ICPC 亚洲区域赛 济南站 D. Largest Digit

D. Largest Digit

time limit per test: 1 second

memory limit per test: 1024 megabytes

Let f(x) be the largest digit in the decimal representation of a positive integer x. For example, f(4523)=5 and f(1001)=1.

Given four positive integers la, ra, lb and rb such that la≤ra and lb≤rb, calculate the maximum value of f(a+b), where la≤a≤ra and lb≤b≤rb.

Input

There are multiple test cases. The first line of the input contains an integer T (1≤T≤) indicating the number of test cases. For each test case:

The first and only line contains four integers la, ra, lb and rb (1≤la≤ra≤, 1≤lb≤rb≤).

Output

For each test case output one line containing one integer indicating the maximum value of f(a+b).

Example

input

2

178 182 83 85

2 5 3 6

output

7

9

Note

For the first sample test case, the answer is f(182+85)=f(267)=7.

For the second sample test case, the answer is f(4+5)=f(9)=9.

【思路分析】

签到题。显然,所求区间转换为[lb+la,rb+ra],遍历该区间会tle,当答案为9时进行剪枝即可。

cpp 复制代码
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <algorithm>
#include <climits>
#include <stack>
#include <cstring>
#include <iomanip>
#include <set>
#include <queue>

#define i64 long long

using namespace std;

typedef pair<i64,i64> pii;

void solve() {
    i64 la,ra,lb,rb,res = 0;
    cin>>la>>ra>>lb>>rb;
    for (i64 i = la+lb; i <= ra+rb; ++i) {
        for (const auto &item: to_string(i)){
            res = max(res,(i64)(item-'0'));
            if (res == 9) goto flag;
        }
    }
    flag:
    cout<<res<<endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
相关推荐
一起努力啊~1 分钟前
算法刷题--长度最小的子数组
开发语言·数据结构·算法·leetcode
rchmin5 分钟前
限流算法:令牌桶与漏桶详解
算法·限流
leoufung13 分钟前
LeetCode 221:Maximal Square 动态规划详解
算法·leetcode·动态规划
黑符石15 分钟前
【论文研读】Madgwick 姿态滤波算法报告总结
人工智能·算法·机器学习·imu·惯性动捕·madgwick·姿态滤波
源代码•宸17 分钟前
Leetcode—39. 组合总和【中等】
经验分享·算法·leetcode·golang·sort·slices
好易学·数据结构18 分钟前
可视化图解算法77:零钱兑换(兑换零钱)
数据结构·算法·leetcode·动态规划·力扣·牛客网
AlenTech32 分钟前
226. 翻转二叉树 - 力扣(LeetCode)
算法·leetcode·职场和发展
Tisfy36 分钟前
LeetCode 1458.两个子序列的最大点积:动态规划
算法·leetcode·动态规划·题解·dp
求梦82036 分钟前
【力扣hot100题】合并区间(9)
算法·leetcode·职场和发展
汽车仪器仪表相关领域1 小时前
工况模拟精准检测,合规减排赋能行业 ——NHASM-1 型稳态工况法汽车排气检测系统项目实战经验分享
数据库·算法·单元测试·汽车·压力测试·可用性测试