geeksforgeeks-Array-Rotation and deletion

As usual Babul is again back with his problem and now with numbers. He thought of an array of numbers in which he does two types of operation that is rotation and deletion. His process of doing these 2 operations are that he first rotates the array in a clockwise direction then delete the last element. In short he rotates the array nth times and then deletes the nth last element. If the nth last element does not exists then he deletes the first element present in the array. So your task is to find out which is the last element that he deletes from the array so that the array becomes empty after removing it.
For example
A = {1,2,3,4,5,6}.
He rotates the array clockwise i.e. after rotation the array A = {6,1,2,3,4,5} and delete the last element that is {5} so A = {6,1,2,3,4}. Again he rotates the array for the second time and deletes the second last element that is {2} so A = {4,6,1,3}, doing these steps when he reaches 4th time, 4th last element does not exists so he deletes 1st element ie {1} so A={3,6}. So continuing this procedure the last element in A is {3}, so o/p will be 3.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines. The first line of each test case contains an integer N. Then in the next line are N space separated values of the array A.

Output:
For each test case in a new line print the required result.

Constraints:
1<=T<=200
1<=N<=100
1<=A[i]<=10^7

Example:
Input
2
4
1 2 3 4
6
1 2 3 4 5 6
Output:
2
3

C++(gcc5.4)代码:

    #include <iostream>
    using namespace std;
    int main() {
        //code
        // define the number of test cases
        int T;
        cin>>T;
        
        for(int t=0; t<T; t++)
        {
            //get the two line input
            int N;
            cin>>N;
            int a[N];
            int i = 0;  
            for(i=0;i<N;i++)
                cin>>a[i];
    
            //Rotate and delete
            int index_delete = 1;
            int array_length = N;
            int tmp;
            while(array_length>1)  
            {
                //Rotate
                tmp = a[array_length - 1];
                for(int j=array_length-1; j>0; j--)
                {
                    a[j] = a[j-1];
                }
                a[0] = tmp;
                
                //delete
                for(int k=array_length<index_delete?0:array_length-index_delete; k<array_length-1; k++)
                {
                    a[k]=a[k+1];
                }    
                
                index_delete += 1;
                array_length -= 1; 
            }
            cout<<a[0]<<endl;
        }
        return 0;
    }

注:更加严谨的将一行数字(允许负数)存入数组的代码如下,但是在测试时包含这部分代码的程序会提示超出时间限制!

#include<iostream>  
using namespace std;  
int main()  
{  
    int a[50];  
    int i = 0;  
    char c;  
    while((c=getchar())!='\n')  
    {  
        if(c!=' ')//把这句判断条件改动  
        {  
            ungetc(c,stdin);  
            cin>>a[i++];  
        }  
    }  
    for(int j=0;j<i;j++)  
    {  
        cout<<"a["<<j<<"]:"<<a[j]<<endl;  
    }  
}

or

#include<iostream>  
using namespace std;  

int main()  
{  
    int a[20];  
    int i = 0;  
    char c;  
    cin>>a[i++];  
    while((c=getchar())!='\n')  
    {  
        cin>>a[i++];  
    }  
    for(int j=0;j<i;j++)  
    {  
        cout<<"a["<<j<<"]:"<<a[j]<<endl;  
    }  
}  

python代码

#code
def rotate(arr):
    n = len(arr)
    tmp = arr[-1]
    arr[1:n+1] = arr[0:-1]
    arr[0] = tmp;
    return -1
 
# Input number of test cases
t = int(input())

# One by one run for all input test cases
for i in range(0,t):
 
    # Input the size of the array
    n = int(input())

    # Input the array
    arr = [int(i) for i in input().split()]
 
    # Rotate and delete
    index_delete = 1;
    array_length = n;

    while array_length>1: 
        # Rotate
        rotate(arr)
        
        # delete
        if array_length<index_delete:
            delete_index = 0
        else:
            delete_index = array_length-index_delete
        
        arr.pop(delete_index)   
        
        index_delete += 1;
        array_length -= 1; 
    
    print(arr[0])
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,789评论 0 23
  • □南海飘萍树摄 更喜岷山千里雪 摄于10月下旬黄龙至成都飞机上
    _s_阅读 543评论 0 0
  • 1 TED演讲和其他一些普通的演讲有所不同,它更有宣传性鼓动性和说服性,演讲的主题一般都是关于对大脑深层认知和意识...
    鲁莽书生阅读 256评论 0 0
  • 我的女儿可能有个假妈,而且她们的假妈得了个真毛病。 因为跟别的妈妈相比较假妈的自控力实在算不上好,这是她最大的毛病...
    冰山二三事阅读 231评论 0 0