蜗蜗接力游戏
唉,这个题数组开小了
错误代码
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
#define endl '\n';
void gmin(int &x,int y){x=min(x,y);}
void gmax(int &x,int y){x=max(x,y);}
string pt(int st,bool x){return st==1?x?"Yes":"No":x?"YES":"NO";}
int n,m,t;
int now[101];
void solve(){
cin>>n>>m>>t;
for(int i=1;i<=n;i++) now[i]=i;
int res=n;
int px=1;
bool b=1;
for(int i=1;i<=m;i++){
for(int j=1;j<=t;j++){
int bpx;
if(b) bpx=px+1;
else bpx=px-1;
if(bpx>res||bpx<1) b=!b;
if(b) bpx=px+1;
else bpx=px-1;
px=bpx;
//if(i==m) cout<<px<<endl;
}
cout<<now[px]<<' ';
now[px]=0;
int bpx;
if(b) bpx=px+1;
else bpx=px-1;
if(bpx>res||bpx<1) b=!b;
if(b) bpx=px+1;
else bpx=px-1;
int fd=now[bpx];
for(int i=px;i<=res;i++) swap(now[i],now[i+1]);//这个题放i+1可能会访问到n+1,会越界
res--;
for(int i=1;i<=res;i++){
if(now[i]==fd) px=i;
}
//if(i==m-1) cout<<px<<endl;
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int t=1;
//cin>>t;
while(t--) solve();
}
正解代码如下
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
#define endl '\n';
void gmin(int &x,int y){x=min(x,y);}
void gmax(int &x,int y){x=max(x,y);}
string pt(int st,bool x){return st==1?x?"Yes":"No":x?"YES":"NO";}
int n,m,t;
int now[1001];//这里要开大
void solve(){
cin>>n>>m>>t;
for(int i=1;i<=n;i++) now[i]=i;
int res=n;
int px=1;
bool b=1;
for(int i=1;i<=m;i++){
for(int j=1;j<=t;j++){
int bpx;
if(b) bpx=px+1;
else bpx=px-1;
if(bpx>res||bpx<1) b=!b;
if(b) bpx=px+1;
else bpx=px-1;
px=bpx;
//if(i==m) cout<<px<<endl;
}
cout<<now[px]<<' ';
now[px]=0;
int bpx;
if(b) bpx=px+1;
else bpx=px-1;
if(bpx>res||bpx<1) b=!b;
if(b) bpx=px+1;
else bpx=px-1;
int fd=now[bpx];
for(int i=px;i<=res;i++) swap(now[i],now[i+1]);
res--;
for(int i=1;i<=res;i++){
if(now[i]==fd) px=i;
}
//if(i==m-1) cout<<px<<endl;
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int t=1;
//cin>>t;
while(t--) solve();
}

浙公网安备 33010602011771号