欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
T1: 原来没有满分的代码:
#include<bits/stdc++.h>
using namespace std;
string s,t;
int main(){
int n,m;
cin>>n>>m;
if(m+1<n){
cout<<"NO";
return 0;
}
cin>>s>>t;
int i;
for(i=0;s[i]!='*';i++){
if(s[i]!=t[i]){
cout<<"NO";
return 0;
}
}
i++;
int j=s.size()-1;
int tj=t.size()-1;
for(;j>=i;j--,tj--){
if(s[j]!=t[tj]){
cout<<"NO";
return 0;
}
if(tj<i-1){
cout<<"NO";
return 0;
}
}
for(;tj>=1;tj--){
if(t[tj]>='a'&&t[tj]<='z'){
} else{
cout<<"NO";
return 0;
}
}
cout<<"YES";
return 0;
}
相同成分重复过多 同一变量反复使用导致值出错 代码格式随意,影响了后期的更改 只是过了样例,没有考虑所有可能的情况
T6
#include<bits/stdc++.h>
using namespace std;
struct node{
int p,t;
};
int n,k;
vector<node>arr;
map<node,int>mp;
bool cmp(node a,node b){
if(a.p>b.p)return 1;
else if(a.p==b.p){
return a.t<b.t;
} else{
return 0;
}
}
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){
node tmp;
cin>>tmp.p>>tmp.t;
if(mp.find(tmp)==mp.end()){
arr.push_back(tmp);
}
mp[tmp]++;
}
sort(arr.begin(),arr.end(),cmp);
node tmp=arr[k-1];
cout<<mp[tmp];
return 0;
}
原来是想到一个点就去写代码,没有考虑到可以用更简单的算法和数据结构解决。以后做题时要先想好用什么数据结构。 错误的主要原因是我的结构体重载运算符不会写,但是如果使用更简单的方法,就不会出现这种情况。
#include<bits/stdc++.h>
using namespace std;
struct node{
int p,t;
};
int n,k;
vector<node>arr;
bool cmp(node a,node b){
if(a.p!=b.p)return a.p>b.p;
else return a.t<b.t;
}
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){
node tmp;
cin>>tmp.p>>tmp.t;
arr.push_back(tmp);
}
sort(arr.begin(),arr.end(),cmp);
node po=arr[k-1];
int cnt=0;
for(int i=0;i<n;i++){
if(po.t==arr[i].t&&po.p==arr[i].p)cnt++;
}
cout<<cnt;
return 0;
}
改完后就只用了结构体和结构体排序,原来是用map自定义排序加上vector数组。
总结: 1.不要想到某个点就开始写,写完全部思路后再开始写代码。 2.不要想的太过复杂,以免实现上出现问题 3.使用数组时注意下标从0开始,还是从1开始
0 条评论
目前还没有评论...
Be the first to comment!
返回讨论列表
107
通过题目
10
发帖数