geeksforgeeks-Array-Rotation and deletion

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;
    }

注:更加嚴謹?shù)膶⒁恍袛?shù)字(允許負數(shù))存入數(shù)組的代碼如下苍碟,但是在測試時包含這部分代碼的程序會提示超出時間限制逆皮!

#include<iostream>  
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;  
    }  
}

or

#include<iostream>  
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代碼

#code
def rotate(arr):
    n = len(arr)
    tmp = arr[-1]
    arr[1:n+1] = arr[0:-1]
    arr[0] = tmp;
    return -1
 
# Input number of test cases
t = int(input())

# One by one run for all input test cases
for i in range(0,t):
 
    # Input the size of the array
    n = int(input())

    # Input the array
    arr = [int(i) for i in input().split()]
 
    # Rotate and delete
    index_delete = 1;
    array_length = n;

    while array_length>1: 
        # Rotate
        rotate(arr)
        
        # delete
        if array_length<index_delete:
            delete_index = 0
        else:
            delete_index = array_length-index_delete
        
        arr.pop(delete_index)   
        
        index_delete += 1;
        array_length -= 1; 
    
    print(arr[0])
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末亩进,一起剝皮案震驚了整個濱河市橄杨,隨后出現(xiàn)的幾起案子驾孔,更是在濱河造成了極大的恐慌肪跋,老刑警劉巖无午,帶你破解...
    沈念sama閱讀 223,002評論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瘪松,死亡現(xiàn)場離奇詭異,居然都是意外死亡悠瞬,警方通過查閱死者的電腦和手機们豌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評論 3 400
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浅妆,“玉大人望迎,你說我怎么就攤上這事×柰猓” “怎么了辩尊?”我有些...
    開封第一講書人閱讀 169,787評論 0 365
  • 文/不壞的土叔 我叫張陵,是天一觀的道長趴乡。 經(jīng)常有香客問我对省,道長,這世上最難降的妖魔是什么晾捏? 我笑而不...
    開封第一講書人閱讀 60,237評論 1 300
  • 正文 為了忘掉前任蒿涎,我火速辦了婚禮,結(jié)果婚禮上惦辛,老公的妹妹穿的比我還像新娘劳秋。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 69,237評論 6 398
  • 文/花漫 我一把揭開白布玻淑。 她就那樣靜靜地躺著嗽冒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪补履。 梳的紋絲不亂的頭發(fā)上添坊,一...
    開封第一講書人閱讀 52,821評論 1 314
  • 那天,我揣著相機與錄音箫锤,去河邊找鬼贬蛙。 笑死,一個胖子當著我的面吹牛谚攒,可吹牛的內(nèi)容都是我干的阳准。 我是一名探鬼主播,決...
    沈念sama閱讀 41,236評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼馏臭,長吁一口氣:“原來是場噩夢啊……” “哼野蝇!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起括儒,我...
    開封第一講書人閱讀 40,196評論 0 277
  • 序言:老撾萬榮一對情侶失蹤绕沈,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后塑崖,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體七冲,經(jīng)...
    沈念sama閱讀 46,716評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,794評論 3 343
  • 正文 我和宋清朗相戀三年规婆,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蝉稳。...
    茶點故事閱讀 40,928評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡抒蚜,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出耘戚,到底是詐尸還是另有隱情嗡髓,我是刑警寧澤,帶...
    沈念sama閱讀 36,583評論 5 351
  • 正文 年R本政府宣布收津,位于F島的核電站饿这,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏撞秋。R本人自食惡果不足惜长捧,卻給世界環(huán)境...
    茶點故事閱讀 42,264評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望吻贿。 院中可真熱鬧串结,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至把敞,卻和暖如春弥奸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背奋早。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評論 1 274
  • 我被黑心中介騙來泰國打工其爵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人伸蚯。 一個月前我還...
    沈念sama閱讀 49,378評論 3 379
  • 正文 我出身青樓摩渺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親剂邮。 傳聞我的和親對象是個殘疾皇子摇幻,可洞房花燭夜當晚...
    茶點故事閱讀 45,937評論 2 361

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,595評論 0 23
  • □南海飄萍樹攝 更喜岷山千里雪 攝于10月下旬黃龍至成都飛機上
    _s_閱讀 524評論 0 0
  • 1 TED演講和其他一些普通的演講有所不同,它更有宣傳性鼓動性和說服性挥萌,演講的主題一般都是關(guān)于對大腦深層認知和意識...
    魯莽書生閱讀 244評論 0 0
  • 我的女兒可能有個假媽绰姻,而且她們的假媽得了個真毛病。 因為跟別的媽媽相比較假媽的自控力實在算不上好引瀑,這是她最大的毛病...
    冰山二三事閱讀 225評論 0 0