1 solutions

  • 0
    @ 2023-1-29 19:54:26
    #include<bits/stdc++.h>
    using namespace std;
    /*
    归纳规律:
    1、弹牌:
    原来 1 ~ n/2位置之间的数,会存入新数组的i*2+1位置
    原来n/2+1 ~ n位置之间的数,会存入新数组的i*2位置
    2、切牌
    将指定区间的元素放到数组的开始位置 
    */
    int a[100],b[100];
    int n, m;
    //将b数组的内容拷贝回a数组
    void fun(){
    	for(int i = 1; i <= n; i++){
    		a[i] = b[i];
    	}
    } 
    int main()
    {
    	cin >> n >> m;
    	for(int i = 1; i <= n; i++){
    		cin >> a[i];
    	}
    	//m次操作
    	int order,x,y;
    	int k;//代表b数组长度
    	while(m--){
    		cin >> order;
    		//切牌
    		if(order == 1){
    			cin >> x >> y;
    			k = 0;
    			//将x ~ y之间的拷贝
    			for(int i = x; i <= y; i++){
    				k++;
    				b[k] = a[i];
    			} 
    			//将剩余的拷贝
    			for(int i = 1; i <= n; i++){
    				if(i < x || i > y){
    					k++;
    					b[k] = a[i];
    				}
    			} 
    			//将b数组拷贝回去
    			fun(); 
    		} 
    		else {
    			for(int i = 1; i <= n/2; i++){
    				b[i*2-1] = a[i];//a前一半
    				b[i*2] = a[i+n/2];//a后一半 
    			}
    			fun(); 
    		}
    	} 
    	for(int i = 1; i <= n; i++){
    		cout << a[i];
    		if(i != n) cout << " ";//去除行末空格 
    	}
    	return 0;
    }
    
    
    • 1

    Information

    ID
    901
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    10
    Tags
    # Submissions
    1
    Accepted
    1
    Uploaded By