天梯赛选拔赛2补题
2026/8/18 15:17:11 网站建设 项目流程

--记忆数字(字符串的遍历)

错误

代码混乱

专门用来遍历一个字符串 / 数组 / 容器里的每一个元素

for (char ch : str)

1.cin >> str

  • 读到空格就停止
  • 读不进带空格的句子
  • 比如输入:hello world
  • 只能读到:hello

2.getline(cin, str)

  • 读到回车才停
  • 能读一整行,包括空格
  • 比如输入:I love coding 123 !!!
  • 完整读进去
#include<bits/stdc++.h> char ch; using namespace std; int main() { string str; getline(cin,str); //能读一整行,包括空格 int l=0; for(int i=0;i<str.size();i++) { if(isalpha(str[i]))//遍历判断是不是字母 l++; else if(l!=0) // 遇到非字母,且前面有字母 //如果现在的字符不是字母并且长度l不等于零就输出 { cout<<l%10; l=0; } } if(l!=0) //该条件触发的前提是后面不是字母 // hello world其中world后面没有非字母 遍历过后只出现 5 ;不是55 { cout<<l%10; //保证去个位数 } return 0; }

--预测帕鲁(向下取整)

链接:https://ac.nowcoder.com/acm/contest/131017/E
来源:牛客网

错误代码

上取整写错为下取整

#include<iostream> #include<climits> #include<string> #include<cstdio> using namespace std; int n; int a[505]; int x,y,av; int c; int mins=INT_MAX; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } scanf("%d %d",&x,&y); av=(a[x]+a[y])/2; for(int j=1;j<=n;j++) { if(abs(av-a[j])<mins) { mins=abs(av-a[j]); c=j; } } printf("%d\n",c); return 0; }
#include<iostream> #include<climits> #include<string> #include<cstdio> using namespace std; int n; int a[505]; int x,y,av; int c; int mins=INT_MAX; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } scanf("%d %d",&x,&y); av=(a[x]+a[y]+1)/2; for(int j=1;j<=n;j++) { if(abs(av-a[j])<mins) { mins=abs(av-a[j]); c=j; } } printf("%d\n",c); return 0; }

F统一命名规范

链接:https://ac.nowcoder.com/acm/contest/131017/F
来源:牛客网

理解错题意了 也没抓到准确关键点 忽略了“只会存在以下三种命名规范”写出的代码太乱了 太长

#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef long long ll; #define endl '\n'; using namespace std; int main() { IOS ll n; string op; string s; string ans; cin>>n>>op; if(op=="Camel") { for(ll i=1;i<=n;i++) { cin>>s; ans=""; ans+=tolower(s[0]); bool ok=false; for(ll j=1;j<s.size();j++) { if(ok) { ans+=toupper(s[j]); ok=false; } else if(!isalpha(s[j])) { ok=true; } else { ans+=s[j]; } } cout<<ans<<endl; } } else if(op=="Pascal") { for(ll i=1;i<=n;i++) { cin>>s; ans=""; ans+=toupper(s[0]); bool ok=false; for(ll j=1;j<s.size();j++) { if(ok) { ans+=toupper(s[j]); ok=false; } else if(!isalpha(s[j])) { ok=true; } else { ans+=s[j]; } } cout<<ans<<endl; } } else { for(ll i=1;i<=n;i++) { cin>>s; ans=""; ans+=tolower(s[0]); bool ok=false; for(ll j=1;j<s.size();j++) { if(s[j]>='A'&&s[j]<='Z') { ans+="_"; ans+=tolower(s[j]); } else { ans+=s[j]; } } cout<<ans<<endl; } } return 0; }


需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询