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. BZOJ1036[ZJOI2008]树的统计——树链剖分+线段树

    题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v ...

  2. Luogu4195 【模板】exBSGS(exBSGS)

    如果a和p互质,用扩欧求逆元就可以直接套用普通BSGS.考虑怎么将其化至这种情况. 注意到当x>=logp时gcd(ax,p)是一个定值,因为这样的话每个存在于a中的质因子,其在ax中的出现次数 ...

  3. MVC DropDownList

    最近发现一个 MVC中绑定前台DropDownList , 并且设置默认选中项的简单方法. 直接上代码 方案一 Action:  ViewData["goodsTypeList"] ...

  4. BZOJ 3745: [Coci2015]Norma(分治)

    题意 给定一个正整数序列 \(a_1, a_2, \cdots, a_n\) ,求 \[ \sum_{i=1}^{n} \sum_{j=i}^{n} (j - i + 1) \min(a_i,a_{i ...

  5. Oracle drop table 和 truncate table对grant授权的影响

    [oracle@crl ~]$ rlwrap sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Production on Tue May 16 14: ...

  6. loj #116. 有源汇有上下界最大流

    题目链接 有源汇有上下界最大流,->上下界网络流 注意细节,重置cur和dis数组时,有n+2个点 #include<cstdio> #include<algorithm> ...

  7. 使用electron为贪吃蛇游戏创建全局快捷键

    上图就是我们的简体版贪吃蛇游戏,我们可以看到使用键盘上面的上下左右可以对贪吃蛇进行控制. The picture above is our simplified version of Snake Ea ...

  8. theano使用

    一  theano内置数据类型 只有thenao.shared()类型才有get_value()成员函数(返回numpy.ndarray)? 1. 惯常处理 x = T.matrix('x') # t ...

  9. Harbor镜像迁移

    目录 背景说明 方案实现 背景说明 在早期生产环境尝试使用docker的时候,虽然使用了harbor作为镜像仓库,但是并没有做好相关存储规划,所有的镜像都直接存储到了harbor本地.随着业务发展,本 ...

  10. 基于URL的HAProxy负载均衡设置

    例子包括ACL的url_beg. url_beg提交URL中使用的字符串相匹配. 使用URL /blog(cnblog/api)所有请求重定向到WEB服务器的6200端口.所有其他请求将重定向到服务器 ...