AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 2058. 笨拙的手指(每日一题) 0.02 AC币

作者: 作者的头像   fangy ,  2023-03-16 21:13:31 ,  所有人可见 ,  阅读 5


0


#include <iostream>
#include <algorithm>

using namespace std;

int get(string s, int b)
{
    int res = 0;
    for (auto &c : s)
        res = res * b + c - '0';
    return res;
}

string convert(int x, int b)
{
    string res = "";
    while (x)
    {
        res += x % b + '0';
        x /= b;
    }
    reverse(res.begin(), res.end());
    return res;
}

bool check(string s, string p)
{
    int cnt = 0;
    if (s.size() != p.size()) return false;
    for (int i = 0; i < s.size(); ++i)
        if (s[i] != p[i]) cnt++;
    return cnt == 1;
}

int main()
{
    string s, p;
    cin >> s >> p;

    int n = s.size(), m = p.size();

    if (n == 1) cout << (char)(s[0] ^ 1) << endl;
    else if (s[0] == '0')
    {
        s[0] ^= 1;
        cout << get(s, 2) << endl;
    }
    else 
    {
        for (int i = 1; i < n; ++i)
        {
            s[i] ^= 1;
            int val = get(s, 2);
            if (check(convert(val, 3), p)) 
            {
                cout << val << endl;
                return 0;
            }
            s[i] ^= 1;
        }
    }

    return 0;
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息