CCF 学生排队

水题
反复swap交换就好了
中文题意不解释了

#include<iostream>
using namespace std;
int a[100000] ;
      int n ;
 void init(){
      for(int i = 1 ; i <= n ; i++){
        cout << a[i] << " " ;
     }
     cout << endl ;
 }
 void solve(int q , int y){
     int x ;
     for(int i = 1 ; i <= n ; i++) if(a[i] == q ){x = i ; break ;}
      if(y > 0 ){
        for(int i = x ; i < x + y ; i++){
            swap(a[i] , a[i+1] ) ;

        }
      }
      else if(y < 0){
        for(int i = x ; i > x + y ; i--){
            swap(a[i] , a[i-1]) ;

        }
      }


  return ;
 }
int main() {

     cin >> n ;
     for(int i = 1 ; i <= n ; i++) a[i] = i ;
     int m ;
     cin >> m ;
     int x , y ;
     for(int i = 0 ; i < m ; i++){
        cin >>x >> y ;
       solve(x , y ) ;
     //  init() ;
     }
      init() ;
     cout << endl ;
     return 0 ;
   }

猜你喜欢

转载自blog.csdn.net/qq_42894605/article/details/82503462