解题思路
注意特判第k位为9的情况
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN = 1e5 + 5;
//char str[MAXN], s1[MAXN];
int main()
{
string str;
int n, k;
cin >> n >> k;
cin >> str;
string s1;
for (int i = 0; i < n; ++i)
{
if (i < k)
{
//cout << str[i] << endl;
s1 += str[i];
}
else
{
s1 += s1[i - k];
}
}
if (s1 >= str)
{
cout << s1.size() << endl;
cout << s1 << endl;
return 0;
}
else
{
if (str[k - 1] == '9')
{
int j = k - 1;
while (str[j] == '9')
{
s1[j] = '0';
j--;
}
s1[j] = str[j] + 1;
}
else
{
s1[k - 1] = str[k - 1] + 1;
}
}
cout << n << endl;
for (int i = 0; i < n; ++i)
{
cout << s1[i % k];
}
cout << endl;
return 0;
}