C#:程序应替换子字符串 1 次。
string s = new string('1', 45) + new string('2', 45);
while (s.Contains("111"))
{
s = s.Replace("111", "2");
s = s.Replace("222", "1");
}
Console.WriteLine(s);
21
Python:
s = '1' * 45 + '2' * 45
while '111' in s:
s = s.replace('111', '2', 1)
s = s.replace('222', '1', 1)
print(s)
12
正如我已经在评论中写给您的,for
C#可以用来Regex.Replace获得与 Python 中相同的结果: