geeksforgeeks-Array-Rotate and delete
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的更多相关文章
- [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 ...
- Leetcode-189 Rotate Array
#189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 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 ...
- [Swift]LeetCode189. 旋转数组 | Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- Rotate Array 旋转数组 JS 版本解法
Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...
- LeetCode 189:旋转数组 Rotate Array
公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
随机推荐
- Minimum Cost POJ - 2516(模板题。。没啥好说的。。)
题意: 从发货地到商家 送货 求送货花费的最小费用... 有m个发货地,,,n个商家,,每个商家所需要的物品和物品的个数都不一样,,,每个发货地有的物品和物品的个数也不一样,,, 从不同的发货地到不同 ...
- 部署kubernetes1.8.3高可用集群
Kubernetes作为容器应用的管理平台,通过对pod的运行状态进行监控,并且根据主机或容器失效的状态将新的pod调度到其他node上,实现了应用层的高可用. 针对kubernetes集群,高可用性 ...
- 【codevs4919】线段树练习4
题目大意:维护一个长度为 N 的序列,支持两种操作:区间加,区间查询有多少数是 7 的倍数. 题解:在每个线段树中维护一个权值数组 [0,6],由于个数显然支持区间可加性,因此可用线段树来维护. 代码 ...
- 【POJ3061】Subsequence
题目大意:给定一个有 N 个正整数的序列,求出此序列满足和大于等于 S 的长度最短连续子序列. #include <cstdio> #include <algorithm> u ...
- MSSQL 转 ACCESS 在表格结构上应注意的
今天在把一个MSSQL数据库转为ACCESS发现了一些问题: 在MSSQL表格中的一个(标识)递增字段转到ACCESS后,变成了 “数字”类型,而不是“自动编号”. 而当在Access中,一个字段类型 ...
- idea svn performing vcs refresh 很长时间
go to settings - version control - background set changelists to cache initially to minimal value (1 ...
- python写GUI
图形用户界面 本文利用wxpython wx包中的方法都是以大写的字幕开头 import wx def load(event): file = open(filename.GetValue()) co ...
- 因缺失log4j.properties 配置文件导致flume无法正常启动。
因缺失log4j.properties 配置文件导致flume无法正常启动 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.报错:log4j:WARN No appenders ...
- Python介绍以及安装
Python介绍以及安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 借用我的导师的一句话:当你看到这篇文章的时候,那么恭喜你,你已经是踏入了开发的大门!欢迎加入:高级运维工程师 ...
- PLSQL Developer 连接Linux 下Oracle的安装与配置
一.下载 下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 这是Ora ...