9.14百度B题 1 0.32

第一题

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

int fanZhuan(int a)
{
    int res = 0;
    while (a > 0)
    {
        res += a % 10;
        a = a / 10;
        if (a > 0)
            res *= 10;
    }

    return res;
}

int main()
{
    int N, K;
    cin >> N >> K;

    int maxRes = INT_MIN;

    int curRes;
    int temp = 0;
    for (int i = 0; i != K; ++i)
    {
        temp += N;
        curRes = fanZhuan(temp);
        maxRes = max(maxRes, curRes);
    }

    cout << maxRes << endl;

    system("pause");
    return 0;
}

第二题

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

string input;
int resLeft;
int resRight;

bool hasNeed(int left, int right, vector<int>& dp)
{
    int OneCount = dp[right] - dp[left - 1];
    while (right < dp.size())
    {
        ++right;
        left++;
        if (right <= dp.size() && OneCount == dp[right] - dp[left - 1])
        {
            resLeft = left;
            resRight = right;

            return true;
        }
    }
    return false;
}

int main()
{
    cin >> input;
    int n = input.size();   // n个数

    vector<int> dp(n + 1, 0);
    for (int i = 0; i != n; ++i)
    {
        if (input[i] == 1)
            dp[i + 1] = dp[i] + 1;
        else
            dp[i + 1] = dp[i];
    }

    int left = 1, right = n - 1;
    while (right >= left)
    {
        if (hasNeed(left, right, dp))
            break;
        --right;
    }

    cout << left << " " << right << " " << resLeft << " " << resRight << endl;

    return 0;
}
全部评论

相关推荐

10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务