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);//获取not的n的下标。
a.erase(y,3);//删除。
}
x++; //一轮结束,加加。
}
cout<<a<<endl<<x;
return 0;
}
T4
1.错误原因:
真的不会写,只能拿子任务的60分。
2.错误代码:
C++#include<bits/stdc++.h>
using namespace std;
bool g(string ab){
int l=ab.size()-1,r=0;
while(r<l){
if(ab[l]!=ab[r]){
return false;
}
l--;
r++;
}
return true;
}
int main(){
string a,b;
cin>>a>>b;
if(a==b){
if(g(a)){
cout<<1;
}else{
cout<<0;
}
}
return 0;
}
3.正确思路:
不能直接枚举年、月、日,会TLE。只枚举月和日,算出年,判断是否合规,再判断是否回文,是则ans++。
4.正确代码:
C++#include<bits/stdc++.h>
using namespace std;
// 存放每个月天数
int day[15]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int main(){
int l,r;
cin>>l>>r;
int ans=0;
// 遍历12个月份
for(int i=1;i<=12;i++){//枚举月
for(int j=1;j<=day[i];j++){//枚举日
int x=i*100+j;
int xx=x,y=0,f=4;//xx为x的备份,以免x的值发生改变,f为循环次数,因为是四位数,所以是4
while(f--){
y=y*10+xx%10;//翻转
xx/=10;
}
int date=y*10000+x; // 拼接为8位年月日
if(date>=l&&date<=r) ans++;
}
}
cout<<ans;
return 0;
}