Codeforces Round #588 (Div. 2) B. Ania and Minimizing(构造)

链接:

https://codeforces.com/contest/1230/problem/B

题意:

Ania has a large integer S. Its decimal representation has length n and doesn't contain any leading zeroes. Ania is allowed to change at most k digits of S. She wants to do it in such a way that S still won't contain any leading zeroes and it'll be minimal possible. What integer will Ania finish with?

思路:

模拟构造即可.

代码:

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

const int MAXN = 2e5+10;
char s[MAXN];
int n, k;

int main()
{
    cin >> n >> k;
    cin >> s;
    int p = 0;
    while (k > 0 && p < n)
    {
        if (p > 0 || p == n-1)
        {
            if (s[p] != '0')
            {
                s[p] = '0';
                k--;
            }
        }
        else
        {
            if (s[p] != '1')
            {
                s[p] = '1';
                k--;
            }
        }
        p++;
    }
    cout << s << endl;

    return 0;
}
上一篇:LeetCode 283. Move Zeroes


下一篇:【leetcode】172. 阶乘后的零( Factorial Trailing Zeroes )