[CodeForces-1225A] Forgetting Things 【构造】

标签: 题解 codeforces题解 构造


题目描述

题目大意

给定\(d_a, d_b\)作为两个数的最高位上(左起第一位)的数,问能不能找到这样的\(a, b\),使得\(a + 1 = b\)。如果不能输出-1。

例如,
给定1 2,我们很容易找到199 + 1 = 200,那么我们就输出199 200。
给定6 2,我们无法找到\(a, b\)满足要求,那么我们就输出-1。

解析

简单构造。

构造答案的方法应该有多种。

分情况讨论一下,
如果给定的两个数相等,那么输出\(d_a \times 10 \;\;d_b \times 10 + 1\)
如果给定的\(d_a + 1 = d_b\),那么直接输出\(d_a \;\; d_b\)即可。
一种特殊情况是,如果给定的\(d_a = 9,d_b = 1\),那么输出\(9, 10\)

通过代码

/*
Status
    Accepted
Time
    31ms
Memory
    12kB
Length
    309
Lang
    GNU G++11 5.1.0
RemoteRunId
    67064706
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int x, y;

    cin >> x >> y;

    if(x == y)
        cout << x * 10 << " " << y * 10 + 1;
    else if(x + 1 == y)
        cout << x << " " << y;
    else if(x == 9 && y == 1)
        cout << "9 10";
    else
        cout << -1;

    return 0;
}
12-25 20:48
查看更多