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;
}
相关推荐
ulias21236 分钟前
单元最短路问题
数据库·c++·算法·动态规划
崎岖Qiu37 分钟前
leetcode380:RandomizedSet - O(1)时间插入删除和获取随机元素(数组+哈希表的巧妙结合)
java·数据结构·算法·leetcode·力扣·散列表
ZLRRLZ39 分钟前
【数据结构】图
数据结构·算法·图论
Ada_疯丫头1 小时前
杰伊·温格罗教我数据结构与算法
算法
小白开始进步1 小时前
机器人集群调度算法简介与实现思路
算法·机器人
好易学·数据结构1 小时前
可视化图解算法60: 矩阵最长递增路径
数据结构·算法·leetcode·力扣·递归·回溯算法·牛客
SamsongSSS2 小时前
JavaScript逆向SM国密算法
javascript·算法·逆向
图灵信徒2 小时前
2025 ICPC Gran Premio de Mexico 3ra Fecha
c++·算法·codeforcrs
大锦终2 小时前
【算法】栈专题
数据结构·c++·算法·leetcode
haogexiaole2 小时前
资源图分配算法
算法