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;
}
相关推荐
平凡但不平庸的码农7 小时前
Go Slice 详解
算法·golang
Jasmine_llq10 小时前
《B3867 [GESP202309 三级] 小杨的储蓄》
算法·循环遍历·数组累加(模拟)·索引定位·顺序输出
啦啦啦_999911 小时前
案例之 逻辑回归_电信用户流失预测
算法·机器学习·逻辑回归
风筝在晴天搁浅11 小时前
快手/字节 CodeTop LeetCode 415.字符串相加
算法·leetcode
DragonnAi11 小时前
猫咪如厕检测与分类识别系统系列【十四】 项目结构重新整理-即将开源完整算法
算法·开源
机器视觉_Explorer12 小时前
【halcon】编程技巧:鼠标擦除
图像处理·人工智能·深度学习·算法·视觉检测
灵智实验室12 小时前
PX4状态估计技术EKF2详解(二):EKF2 误差状态动力学与协方差传播
算法·无人机·px 4
米粒113 小时前
力扣算法刷题 Day 64 Floyd算法 & A* 算法 & 总结篇
算法·leetcode·职场和发展
XX風13 小时前
OpenGL中Face culling 面剔除的具体实现
算法·图形渲染
IT猿手13 小时前
光伏模型参数估计:基于山羊优化算法(GOA )的光伏模型参数辨识问题求解研究,免费提供完整MATLAB代码链接
开发语言·算法·matlab·群智能优化算法·智能优化算法·光伏模型参数估计·光伏模型参数辨识