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: ...
随机推荐
- Educational Codeforces Round 58 Div. 2 自闭记
明明多个几秒就能场上AK了.自闭. A:签到. #include<iostream> #include<cstdio> #include<cmath> #inclu ...
- BZOJ1304 CQOI2009叶子的染色(树形dp)
令f[i]表示i子树内最少染色次数,加上012状态分别表示该子树内叶节点已均被满足.存在黑色叶节点未被满足.存在白色叶节点未被满足,考虑i节点涂色情况即可转移.事实上贪心也可以. #include&l ...
- POJ1611-The Suspects-并查集
记录元素个数的并查集. 利用sz数组保存并查集的大小.每次union时,把小的集合并到大的中去,并更新sz数组. #include <cstdio> #include <algori ...
- Deep Learning(深度学习)学习笔记整理系列 一
声明: 1)该Deep Learning的学习系列是整理自网上很大牛和机器学习专家所无私奉献的资料的.具体引用的资料请看参考文献.具体的版本声明也参考原文献. 2)本文仅供学术交流,非商用.所以每一部 ...
- bzoj1007/luogu3194 水平可见直线 (单调栈)
先按斜率从小到大排序,然后如果排在后面的点B和前面的点A的交点是P,那B会把A在P的右半段覆盖掉,A会把B在P的左半段覆盖掉. 然后如果我们现在又进来了一条线,它跟上一条的交点还在上一条和上上条的左边 ...
- JAVA:当数据库重启后连接池没有自动识别的解决办法
今天发现服务器上的一个服务程序出现问题,软件抛出:Connection reset by peer: socket write error 无法正常提供服务,找了一下原因,原来是因为数据库服务器重启, ...
- 前端基础之html(一)
https://www.cnblogs.com/haiyan123/p/7516060.html 一.初始html 1.web服务本质 import socket sock=socket.socket ...
- 浏览器中输入URL发生了什么
浏览器中输入URL会发生什么呢?这是我们经常会问到的一个问题. 我们知道的都是会发送http请求,服务端会处理请求给我们响应的结果,浏览器会渲染html 页面 但其实会遗漏掉一些比较重要的东西.下面的 ...
- ReactNative快速入门
首先放图 这就是我通过简单的搭建环境写出的helloworld和使用的button组件. 那么搭建环境如何搭建呢? 使用的软件有:Node 最新版,Python2.7,Android环境要有配置And ...
- MATLAB:图像裁切(imcrop函数)
对图像进行裁切可用imcrop函数,实现过程如下: close all; %关闭当前所有图形窗口,清空工作空间变量,清除工作空间所有变量 clear all; clc; [A,map]=imread( ...