T3:
1.错误原因:
只写出了统计删除次数,不知如何写统计轮数。
2.错误代码:
C++#include<bits/stdc++.h>
using namespace std;
int main(){
int t,x=0;
string a;
cin>>t>>a;
while(a.find("not")!=-1){
int y=a.find("not");
a.erase(y,3);
x++;
}
cout<<a<<endl<<x;
return 0;
}
3.正确思路:
定义一个外层循环,记录轮数。内层则为一轮删除。
主要代码:
C++while(a.find("not")!=-1){
int y=0;
while(a.find("not",y)!=-1){
y=a.find("not",y);
a.erase(y,3);
}
x++;
}
4.正确代码:
C++#include<bits/stdc++.h>
using namespace std;
int main(){
int t,x=0;
string a;
cin>>t>>a;
while(a.find("not")!=-1){//判断字符串是否还有not。
int y=0;
while(a.find("not",y)!=-1){//一轮删除,注意:要从y开始往后删除,仅删除目前已出现not。
y=a.find("not",y);
a.erase(y,3);
}
x++; //一轮结束,加加。
}
cout<<a<<endl<<x;
return 0;
}