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

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;

}

include

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代码

geeksforgeeks-Array-Rotate and delete的更多相关文章

  1. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  2. Leetcode-189 Rotate Array

    #189.    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...

  3. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  4. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  5. [Swift]LeetCode189. 旋转数组 | Rotate Array

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  6. Rotate Array 旋转数组 JS 版本解法

    Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...

  7. LeetCode 189:旋转数组 Rotate Array

    公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...

  8. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  9. LeetCode_189. Rotate Array

    189. Rotate Array Easy Given an array, rotate the array to the right by k steps, where k is non-nega ...

  10. Python3解leetcode Rotate Array

    问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...

随机推荐

  1. LOJ #6435. 「PKUSC2018」星际穿越(倍增)

    题面 LOJ#6435. 「PKUSC2018」星际穿越 题解 参考了 这位大佬的博客 这道题好恶心啊qwq~~ 首先一定要认真阅读题目 !! 注意 \(l_i<r_i<x_i\) 这个条 ...

  2. 自学Linux Shell6.1-环境变量概念

    点击返回 自学Linux命令行与Shell脚本之路 6.1-环境变量概念 环境变量 在Linux中,很多程序和脚本都通过环境变量来获取系统信息.存储临时数据和配置信息: bash shell使用环境变 ...

  3. NOI2018退役记

    NOI2018退役记 终于我也退役了-- Day0 高中毕业前最后一次坐飞机了--在机场干什么呢?当然是打元气打元气打元气.下飞机干什么呢?当然是打元气打元气打元气. 有接机服务,大巴上有个导游,又向 ...

  4. [luogu4265][USACO18FEB]Snow Boots silver

    题目大意 求出最少需要丢去多少双靴子才能到达终点. 解法 解法一: 看到数据的范围,非常清楚\(O(n^3)\)能过掉所有的数据,那么我们就果断暴力. 解法二: 比较容易会想到用DP做,我一开始定义\ ...

  5. eclipse启动速度优化

    1. 在eclipse.ini文件中添加如下参数(红色部分) -startup plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.ja ...

  6. A1010. Radix

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  7. HTML中使用<input>添加的按钮打开一个链接

    在HTML中,<form>表单的<input type="button">可以添加一个按钮.如果想让该按钮实现<a> 的超链接功能,需要如下实现 ...

  8. Quadratic.java

    /****************************************************************************** * Compilation: javac ...

  9. 【CSS】定义元素的对齐方式

    1.文本内容居中对齐:text-align.扩展用法:父元素嵌套子元素时,且子元素的宽度小于父元素宽度,使用text-align:center,可以实现子元素的居中对齐. <!DOCTYPE h ...

  10. 记录一次iptables端口转发的配置

    需求是公网访问2.2.2.22的80端口,直接转发到内网的192.100.100.178的80端口上. 代理服务器的,两个不同的网卡 eth0 2.2.2.22 eth1 192.100.100.10 ...