博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
geeksforgeeks-Array-Rotate and delete
阅读量:6799 次
发布时间:2019-06-26

本文共 2819 字,大约阅读时间需要 9 分钟。

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 
using namespace std; int main() { //code // define the number of test cases int T; cin>>T; for(int t=0; t
>N; int a[N]; int i = 0; for(i=0;i
>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

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

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

转载于:https://www.cnblogs.com/alanma/p/7360215.html

你可能感兴趣的文章
Oracle 字符串处理函数
查看>>
中国银行涉嫌洗黑钱却另有隐情?
查看>>
排序问题分析
查看>>
【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(9) 角色管理,分配权限...
查看>>
《程序是怎样跑起来的》读书笔记——第八章 从源文件到可执行文件
查看>>
【一句日历】2019年5月
查看>>
服务器端产生大量的close_time
查看>>
自定义从Azure下载回来的远程桌面连接(.rdp)文件,使其提供更多丰富功能
查看>>
c语言高级语言控制成分while,这衣服收费的形式特征有
查看>>
android bitmap 描边,Android 绘图之Canvas相关API使用
查看>>
计算机科学导论计算实例,经典计算计算模型计算机科学导论.ppt
查看>>
如何确定一个网站是用Wordpress开发的
查看>>
爬虫采集-基于webkit核心的客户端Ghost.py [爬虫实例]
查看>>
VDI序曲一 服务器虚拟化
查看>>
先考学历还是先提升能力?
查看>>
四、物理优化(7)查看索引使用情况
查看>>
[原创]如何从数据库层面检测两表内容的一致性
查看>>
学霸装学渣
查看>>
Microsoft Dynamics CRM 2015 完全安装好以后 完全备份
查看>>
git 在windows下的应用(一) - 本地仓库代码管理
查看>>