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. 看完让你彻底理解 WebSocket 原理,附完整的实战代码(包含前端和后端)

    1.前言 最近有同学问我有没有做过在线咨询功能.同时,公司也刚好让我接手一个 IM 项目.所以今天抽时间记录一下最近学习的内容.本文主要剖析了 WebSocket 的原理,以及附上一个完整的聊天室实战 ...

  2. MongoDB安装的坑

    目前最新版本的MongoDB3.6在windows下安装会出现很难解决的问题,所以就换了3.4版本,由于我之前3.6的安装不成功,所以卸载了好几次,其中目录改变了一次,就导致了这次大坑,用了我三四个小 ...

  3. go vendor目录

    参考 https://blog.csdn.net/u010649766/article/details/80327035 那么查找依赖包路径的解决方案如下: 当前包下的vendor目录. 向上级目录查 ...

  4. 洛谷 P4375 [USACO18OPEN]Out of Sorts G(树状数组求冒泡排序循环次数加强版)

    传送门:Problem 4375 参考资料: [1]:https://www.cnblogs.com/Miracevin/p/9662350.html [2]:https://blog.csdn.ne ...

  5. ELK技术实战-安装Elk 5.x平台

    ELK技术实战–了解Elk各组件   转载  http://www.ywnds.com/?p=9776 ELK技术实战-部署Elk 2.x平台 ELK Stack是软件集合Elasticsearch. ...

  6. ElasticSearch搜索介绍四

    ElasticSearch搜索 最基础的搜索: curl -XGET http://localhost:9200/_search 返回的结果为: { "took": 2, &quo ...

  7. 关于连接linux被拒

    一.scp被拒 在服务器A命令行使用scp往服务器B传输,如: scp -i id_rsa_125 file1 user01@IP:/home #ssh_exchange_identification ...

  8. 设计模式---单一职责模式之桥模式(Bridge)

    一:概念 Bridge模式又叫做桥接模式,其实基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任他的主要特点是吧抽象与行为实现分离开来,从而可以保持各部分的独立性以及一对 ...

  9. 震惊!最全PyCharm教程

    PyCharm PyCharm是一个用于计算机编程的集成开发环境(IDE),主要用于Python语言开发,由捷克公司JetBrains开发,提供代码分析.图形化调试器,集成测试器.集成版本控制系统(V ...

  10. 绕过/*,web.xml直接访问jsp【转】

    web.xml中如果配置了/* 全匹配,那么不能用servet去响应页面返回了,因为全都被会/*拦截. <servlet> <servlet-name>validateAuth ...