主要就是先將梯子移動到最左邊或者最右邊
k>n/2時移動到最右邊
k<=n/2時移動到最左邊
然後遍歷一遍
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n,k; cin >> n >> k; string poster ; cin >> poster; if(k > n/2){ for(int i = k-1 ; i < n-1; ++ i){ cout<<"RIGHT"<<endl; } for(int i = n-1; i > 0; --i){ cout<<"PRINT "<<poster[i]<<endl; cout<<"LEFT"<<endl; } cout<<"PRINT "<<poster[0]<<endl; }else{ for(int i = k-1; i>0; --i) cout<<"LEFT"<<endl; for(int i = 0; i < n-1; ++i){ cout<<"PRINT "<<poster[i]<<endl; cout<<"RIGHT"<<endl; } cout<<"PRINT "<<poster[n-1]<<endl; } }