HJ129 小红的双生数

知识点数论

校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。

描述

小红定义一个正整数是"双生数",当且仅当该正整数的每个数位的相邻数位中,恰好有一个和该数位的数字相同。

现在小红拿到了一个正整数 xx,她希望你求出不小于 xx 的最小"双生数"。

输入描述:

输入一个正整数 x(1≦x≦10100 000)x(1≦x≦10100000) 代表限制。

输出描述:

输出一个正整数,代表不小于 xx 的最小"双生数"。该数字不包含前导零。

示例1

输入:

复制代码
123

复制输出:

复制代码
1100

复制说明:

复制代码
在这个样例中,11001100 的第一、二个数位相同,第三、四个数位相同。我们可以证明,这是符合要求的最小的双生数。

示例2

输入:

复制代码
114514

复制输出:

复制代码
115500
cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;

vector<int> find(const string& s) {
    int n = s.length();
    vector<int> ans;

    if (n % 2 == 1) { // 奇数长度
        ans.resize(n + 1, 0);
        for (int i = 1; i <= n; i += 2) {
            int val = ((i / 2) & 1) ^ 1;
            ans[i] = ans[i - 1] = val;
        }
    } else { // 偶数长度
        ans.resize(n, 0);
        for (int i = 1; i < n; i += 2) {
            int a = s[i - 1] - '0';
            int b = s[i] - '0';
            if (a == b) {
                ans[i] = ans[i - 1] = a;
            } else {
                int target = a * 10 + b;
                for (int j = 0; j < 10; j++) {
                    if (j * 11 >= target) {
                        ans[i] = ans[i - 1] = j;
                        break;
                    }
                }
                int k = 0;
                for (int j = i + 2; j < n; j += 2) {
                    ans[j] = ans[j - 1] = k;
                    k ^= 1;
                }
                break;
            }
        }

        for (int i = 2; i < n; i += 2) {
            if (ans[i] == ans[i - 1]) {
                bool found = false;
                for (int j = i + 1; j >= 0; j -= 2) {
                    ans[j] += 1;
                    ans[j - 1] += 1;
                    if (j > 2 && ans[j] == ans[j - 2]) {
                        ans[j] += 1;
                        ans[j - 1] += 1;
                    }
                    if (ans[j] < 10) {
                        found = true;
                        int k = 0;
                        for (int p = j + 2; p < n; p += 2) {
                            ans[p] = ans[p - 1] = k;
                            k ^= 1;
                        }
                        break;
                    }
                }
                if (!found) {
                    ans.resize(n + 2, 0);
                    for (int j = 1; j <= n + 1; j += 2) {
                        int val = ((j / 2) & 1) ^ 1;
                        ans[j] = ans[j - 1] = val;
                    }
                    return ans;
                }
            }
        }
    }
    return ans;
}

int main() {
    string x;
    cin >> x;
    vector<int> result = find(x);
    for (int num : result) {
        cout << num;
    }
    cout << endl;
    return 0;
}
相关推荐
To_OC3 小时前
LC 51 N 皇后:我以为难的是回溯,结果栽在了对角线下标
javascript·算法·leetcode
2401_841495643 小时前
【操作系统】进程同步与互斥实验报告
c++·算法·操作系统·进程·并发·同步·互斥
fqbqrr4 小时前
2607C++,soui与安卓
c++·soui
fqbqrr7 小时前
2607C++,使用微软detours勾挂工具
c++
爱刷碗的苏泓舒7 小时前
PPP-AR 中的参考星选取:数学原理、评价指标与切换处理
算法·gnss·模糊度固定·ppp-ar·星间单差·参考星·卫星端偏差
蓝悦无人机10 小时前
C++基础 — 函数总结
开发语言·c++
烬羽10 小时前
递归老写崩?一个"退回"公式,把回溯题变成填空题
javascript·深度学习·算法
闪电悠米10 小时前
力扣hot100-41.缺失的第一个正数-原地哈希详解
数据结构·算法·哈希算法
星空露珠11 小时前
28种颜色对应名称,
开发语言·数据库·算法·游戏·lua