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;
}
相关推荐
stolentime1 小时前
洛谷P10515 转圈题解
c++·算法·数学建模·贪心算法
和裕2 小时前
蜂窝板 vs 七层瓦楞重型纸箱:大件工业设备运输性能与成本全对比
大数据·运维·网络·人工智能·算法
衡石科技2 小时前
Data_Agent记忆机制与经验复用衡石分析智能体会话记忆与长期学习技术解析
人工智能·科技·学习·算法·企业级bi
shirsl4 小时前
算法 Day 2 滑动窗口 + 栈 / 单调栈
开发语言·python·算法
土司大王5 小时前
LeetCode hot100 回溯专题总结:Java 通用模板、决策树模型
java·算法·leetcode·决策树
2601_962218615 小时前
万象生鲜系统订单全生命周期状态同步技术实现业务可视
大数据·数据库·人工智能·python·算法
那年窗外下的雪.5 小时前
AIDC 学习日志|第 22 天|All-Active/Single-Active 故障演练设计
服务器·网络·学习·算法·哈希算法
wzdark6 小时前
基于启发式搜索的最优路径规划算法研究4
算法
l1t6 小时前
用superpi、tinypi、tpi等工具计算圆周率的比较
c语言·算法
a187927218316 小时前
【算法】链表(二):链表上的双指针——变速、异链与定距,和一份路程账本
数据结构·算法·leetcode·链表·go·指针·环形链表