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;  
    }  
---
### python代码

geeksforgeeks-Array-Rotation and deletion的更多相关文章

  1. Data Structure Array: Program for array rotation

    http://www.geeksforgeeks.org/array-rotation/ O(n), O(1) #include <iostream> #include <vecto ...

  2. geeksforgeeks-Array-Rotate and delete

    As usual Babul is again back with his problem and now with numbers. He thought of an array of number ...

  3. Must practice programming questions in all languages

    To master any programming languages, you need to definitely solve/practice the below-listed problems ...

  4. geeksforgeeks@ Largest Number formed from an Array

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=380 Largest Number formed from an Array G ...

  5. geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=493 Sorting Elements of an Array by Frequ ...

  6. [geeksforgeeks] Count the number of occurrences in a sorted array

    Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...

  7. Suffix array

    A suffix array is a sorted array of all suffixes of a given string. The definition is similar to Suf ...

  8. 算法最坏,平均和最佳情况(Worst, Average and Best Cases)-------geeksforgeeks 翻译

    最坏,平均和最佳运行时间(Worst, Average and Best Cases) 在上一篇文章中,我们讨论到了渐进分析可以解决分析算法的问题,那么在这一篇中,我们用线性搜索来举例说明一下如何用渐 ...

  9. 42.旋转数组的最小元素[Get min value of rotated array]

    [题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5 ...

随机推荐

  1. ACdream1187-Rational Number Tree-模拟/找规律

    找到规律模拟就可以了,用DFS模拟很简洁,用循环模拟比较直观(大概吧) 注意输入输出用%llu,1ULL<<64=0!被这几个小问题卡了好久 #include <cstdio> ...

  2. HDU4403-模拟、数学

    一道很难的奥数题,给出一个数字串,插入加号和等号使之成立.求成立的算式数. 我的做法是,先分成两段,中间插入等号 ,再分别求出左右两边可能的值和个数,然后对比,把值相等的情况乘起来,加到最终结果上. ...

  3. MT【231】棋子方法数

    设有5枚无区别的棋子放在如图$5*5$的棋盘的小方格中,放棋子的规则是每行每列放且仅放一个棋子,同时,不允许放在黑方格内,则共有______ 方法. 答案:5的错排数44.第一行的数不放第二列(相当于 ...

  4. HTML 百度地图API调用示例源码

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user

    当我在subsite里点击"Add a document",报这个错,后来一看event log: 在AAM里加上一条: 问题搞定:

  6. 【转】用宏定义代替printf函数

    问题提出 有时候我们想用宏定义来决定是编译debug版本的代码还是release的代码,dubug版本的代码会通过printf打印调试信息,release版本的代码则不会.我们总不能对每一条print ...

  7. 简易版AC自动机

    为什么说是简易版? 因为复杂度大概是\(O(M*\overline N)\),而似乎还有另一种大概是\(O(M+\sum N)\)的. 不过据说比赛不会卡前一种做法,因为模式串一般不会很长. 那么步入 ...

  8. python 线程间事件通知

    这是线程间最简单的通信机制:一个线程发送事件,其他线程等待事件事件机制使用一个内部的标志,使用set方法进行使能为True,使用clear清除为falsewait方法将会阻塞当前线程知道标记为True ...

  9. java用poi读取Excel表格中的数据

    Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版.Apache POI 代 ...

  10. HTML:<input type="text">文本框不可编辑的方式

    1.<input type="text" name="name" value="姓名" disabled /> 该方式显示的文本 ...