2025寒假天梯赛训练5
- 人工智能
- 2025-09-08 13:06:02

L1-3 敲笨钟 - 2025寒假天梯赛训练5
思路:一般ex的模拟题,主要是找好空格的位置进行修改替换。
#include <bits/stdc++.h> using namespace std; #define int long long #define endl "\n" #define sz(x) (int)x.size() #define e emplace_back //void solve(){ // cout<<"PTA shi3 wo3 jing1 shen2 huan4 fa1 !"; //} //void solve(){ // string str; // getline(cin,str); cout<<str<<endl; // for(int i=0;i<sz(str);i++){ // if(str[i]=='6'){ // int cnt=0; // for(int j=i;j<sz(str);j++){ // if(str[j]=='6') cnt++; // else break; // } // if(cnt>3&&cnt<=9) cout<<"9",i+=cnt-1; // else if(cnt>9) cout<<"27",i+=cnt-1; // else cout<<str[i]; // } // else cout<<str[i]; // } //} void solve(){ int n; cin>>n; cin.ignore(); string str; for(int i=1;i<=n;i++){ getline(cin,str); // cout<<str<<endl; for(int j=0;j<sz(str);j++){ if(str[j]==','){ if(str[j-3]=='o'&&str[j-2]=='n'&&str[j-1]=='g'){ continue; } else { cout<<"Skipped"<<endl; break; } } else if(str[j]=='.'){ if(str[j-3]=='o'&&str[j-2]=='n'&&str[j-1]=='g'){ int idx=0,cnt=0; for(int f=j;f>=0;f--){ if(str[f]==' '){ cnt++; if(cnt==3){ idx=f; break; } } } cout<<str.substr(0,idx+1)+"qiao ben zhong."<<endl; } else cout<<"Skipped"<<endl; } } } } int32_t main() { ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr); int t=1; // cin>>t; while(t--){ solve(); } return 0; } L1-8 估值一亿的AI核心代码 - 2025寒假天梯赛训练5思路:称得上最ex的pta模拟题,居然是20分的题。
对于这种大便模拟题,最好封装一下各个功能。
如代码中一样,把不同的功能封装实现。还是要注意各种细节。
#include <bits/stdc++.h> using namespace std; #define int long long #define endl "\n" #define sz(x) (int)x.size() #define e emplace_back //void solve(){ // cout<<"PTA shi3 wo3 jing1 shen2 huan4 fa1 !"; //} //void solve(){ // string str; // getline(cin,str); cout<<str<<endl; // for(int i=0;i<sz(str);i++){ // if(str[i]=='6'){ // int cnt=0; // for(int j=i;j<sz(str);j++){ // if(str[j]=='6') cnt++; // else break; // } // if(cnt>3&&cnt<=9) cout<<"9",i+=cnt-1; // else if(cnt>9) cout<<"27",i+=cnt-1; // else cout<<str[i]; // } // else cout<<str[i]; // } //} //void solve(){ // int n; cin>>n; // cin.ignore(); // string str; // for(int i=1;i<=n;i++){ // getline(cin,str); cout<<str<<endl; // for(int j=0;j<sz(str);j++){ // if(str[j]==','){ // if(str[j-3]=='o'&&str[j-2]=='n'&&str[j-1]=='g'){ // continue; // } // else { // cout<<"Skipped"<<endl; // break; // } // } // else if(str[j]=='.'){ // if(str[j-3]=='o'&&str[j-2]=='n'&&str[j-1]=='g'){ // int idx=0,cnt=0; // for(int f=j;f>=0;f--){ // if(str[f]==' '){ // cnt++; // if(cnt==3){ // idx=f; // break; // } // } // } // cout<<str.substr(0,idx+1)+"qiao ben zhong."<<endl; // } // else cout<<"Skipped"<<endl; // } // } // } //} //void solve(){ // int x,y; cin>>x>>y; // cout<<100*100/2-(x*y)/2-(100-x)*(100-y)/2-(100-x)*y; //} //void solve(){ // double x,y; cin>>x>>y; // double num=x/(y*y); // cout<<fixed<<setprecision(1)<<num<<endl; // if(num>25) cout<<"PANG"; // else cout<<"Hai Xing"; //} //int ctoi(char x){ return x-'0'; } //void solve(){ // int n; cin>>n; // string str; // for(int i=1;i<=n;i++){ // cin>>str; // if(ctoi(str[0])+ctoi(str[1])+ctoi(str[2])==ctoi(str[3])+ctoi(str[4])+ctoi(str[5])){ // cout<<"You are lucky!"<<endl; // } // else cout<<"Wish you good luck."<<endl; // } //} //void solve(){ // int n; cin>>n; // for(int i=1;i<=n;i++){ // int sex,tall,weight; // cin>>sex>>tall>>weight; // if(sex){ // if(tall<130) cout<<"duo chi yu! "; // else if(tall==130) cout<<"wan mei! "; // else if(tall>130) cout<<"ni li hai! "; // if(weight<27) cout<<"duo chi rou!"<<endl; // else if(weight==27) cout<<"wan mei!"<<endl; // else if(weight>27) cout<<"shao chi rou!"<<endl; // } // else{ // if(tall<129) cout<<"duo chi yu! "; // else if(tall==129) cout<<"wan mei! "; // else if(tall>129) cout<<"ni li hai! "; // if(weight<25) cout<<"duo chi rou!"<<endl; // else if(weight==25) cout<<"wan mei!"<<endl; // else if(weight>25) cout<<"shao chi rou!"<<endl; // } // } //} string s; bool ok(int idx){ return !(s[idx]>='a'&&s[idx]<='z')&&s[idx]!='I'&&!(s[idx]>='0'&&s[idx]<='9'); } // 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉; void eraseEmpty(){ // 删去开头的空格 for(int i=0;i<sz(s);i++){ if(s[i]!=' '){ s=s.substr(i); break; } } // 删去尾部的空格 for(int i=sz(s)-1;i>=0;i--){ if(s[i]!=' '){ s=s.substr(0,i+1); break; } } string s0=""; bool cc=false; for(int i=0;i<sz(s);i++){ if(s[i]!=' ') { // 如果空格后面不是标点符号 if(cc&&!ok(i)) s0+=' '; if(cc) cc=false; s0+=s[i]; } else if(s[i]==' ') cc=true; } s=" "+s0+" "; //为了后面好处理,暂时加一个空格 } // 把原文中所有大写英文字母变成小写,除了 I; void toLower(){ for(int i=0;i<sz(s);i++){ if(s[i]>='A'&&s[i]<='Z'&&s[i]!='I'){ s[i]+='a'-'A'; } } } // 把原文中所有独立的 can you、could you 对应地换成 I can、I could—— // 这里“独立”是指被空格或标点符号分隔开的单词; void change1(){ string s0=""; for(int i=1;i<sz(s);i++){ // 漏了ok(i+7)和ok(i+9)扣一分。。。。究极恶心 if(s.substr(i,7)=="can you"&&ok(i-1)&&ok(i+7)) s0+="F CAN",i+=6; else if(s.substr(i,9)=="could you"&&ok(i-1)&&ok(i+9)) s0+="F COULD",i+=8; else s0+=s[i]; } s=" "+s0; } // 把原文中所有独立的 I 和 me 换成 you; void change2(){ string s0=""; for(int i=1;i<sz(s)-1;i++){ if(s[i]=='I'&&ok(i-1)&&ok(i+1)) s0+="you"; else if(s.substr(i,2)=="me"&&ok(i-1)&&ok(i+2)) s0+="you",i++; else s0+=s[i]; } s=s0; } // 把原文中所有的问号 ? 换成惊叹号 !; void change3(){ for(int i=0;i<sz(s);i++) if(s[i]=='?') s[i]='!'; } // 在一行中输出替换后的句子作为 AI 的回答。 void answer(){ cout<<"AI: "; for(int i=0;i<sz(s);i++){ if(s[i]=='F') cout<<"I"; else if(s[i]!='I'&&s[i]>='A'&&s[i]<='Z') s[i]+='a'-'A',cout<<s[i]; else cout<<s[i]; } cout<<endl; } void solve(){ int n; cin>>n; cin.ignore(); for(int i=1;i<=n;i++){ getline(cin,s); // 无论用户说什么,首先把对方说的话在一行中原样打印出来; cout<<s<<endl; toLower(); eraseEmpty(); change1(); change2(); change3(); answer(); } } int32_t main() { ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr); int t=1; // cin>>t; while(t--){ solve(); } return 0; }2025寒假天梯赛训练5由讯客互联人工智能栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“2025寒假天梯赛训练5”