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;
}
相关推荐
AI视觉网奇1 小时前
Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr
开发语言·c++·算法
ghie90901 小时前
ECG波形检查与分析系统
算法
智者知已应修善业1 小时前
【输入两个数字,判断两数相乘是否等于各自逆序数相乘】2023-10-24
c语言·c++·经验分享·笔记·算法·1024程序员节
Shingmc32 小时前
【Linux】进程控制
linux·服务器·算法
阿正的梦工坊3 小时前
DreamGym:通过经验合成实现代理学习的可扩展化
人工智能·算法·大模型·llm
小武~3 小时前
Leetcode 每日一题C 语言版 -- 45 jump game ii
c语言·算法·leetcode
行云流水6263 小时前
前端树形结构实现勾选,半勾选,取消勾选。
前端·算法
laocooon5238578864 小时前
一个C项目实现框架
c语言·算法
c#上位机5 小时前
halcon图像增强——图像取反
图像处理·算法·c#·halcon
zheyutao5 小时前
割点和桥
算法·图论