Cerinta completa

You are a waiter at a party. There is a pile of numbered plates. Create an empty array. At each iteration, , remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible by the prime number. If it is, stack it in pile . Otherwise, stack it in stack . Store the values in from top to bottom in . In the next iteration, do the same with the values in stack . Once the required number of iterations is complete, store the remaining values in in , again from top to bottom. Return the array.

Example


An abbreviated list of primes is . Stack the plates in reverse order.


Begin iterations. On the first iteration, check if items are divisible by .

Move elements to .

On the second iteration, test if elements are divisible by .

Move elmements to .

And on the third iteration, test if elements are divisible by .

Move elmements to .

All iterations are complete, so move the remaining elements in , from top to bottom, to .

. Return this list.

Function Description

Complete the waiter function in the editor below.

waiter has the following parameters:

  • int number[n]: the numbers on the plates
  • int q: the number of iterations

Returns

  • int[n]: the numbers on the plates after processing

Input Format

The first line contains two space separated integers, and .
The next line contains space separated integers representing the initial pile of plates, i.e., .

Constraints



Sample Input 0

5 1
3 4 7 6 5

Sample Output 0

4
6
3
7
5

Explanation 0

Initially:

= [3, 4, 7, 6, 5]<-TOP

After 1 iteration (divide by 2, the 1st prime number):

= [5, 7, 3]<-TOP

= [6, 4]<-TOP

Move elements to .

All iterations are complete, so move elements to .

.

Sample Input 1

5 2
3 3 4 4 9

Sample Output 1

4
4
9
3
3

Explanation 1

Initially:

= [3, 3, 4, 4, 9]<-TOP

After iteration (divide by 2):

= [9, 3, 3]<-TOP

= [4, 4]<-TOP

Move to .

After iteration (divide by 3):

= []<- TOP

= [3, 3, 9]<-TOP

Move elements to .

There are no values remaining in .


Limbajul de programare folosit: cpp14

Cod:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1300;
const int M = 110000;
int ans[M], stk[M], tmpstk[M],prim[N];
int atop, top, ttop, n,q, num;

void init(){
    num = 0;
    for(int i = 2; i < M; ++ i){
        bool isfind = false;
        for(int j = 2; j <= sqrt(i); ++ j){
            if(i%j==0){
                isfind = true;
                break;
            }
        }
        if(!isfind){
            prim[num ++] = i;
        }
        if(num >= 1200)
            return ;
    }
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    scanf("%d%d",&n,&q);
    init();
    atop = top = 0;
    for(int i = 0; i < n; ++ i){
        scanf("%d",&stk[top ++]);
    }
    for(int i = 0; i < q; ++ i){
        ttop = 0;
        while(top){
            int v = stk[top - 1];
            -- top;
            if(v%prim[i] == 0){
                ans[atop ++] = v;
            }else{
                tmpstk[ttop ++] = v;
            }
        }
        while(atop){
            printf("%d\n",ans[atop - 1]);
            -- atop;
        }
        for(int j = 0; j < ttop; ++ j){
            stk[j] = tmpstk[j];
        }
        top = ttop;
        if(!top)
            break;
    }
    while(top){
        printf("%d\n",stk[top - 1]);
        -- top;
    }
    return 0;
}

Scor obtinut: 1.0

Submission ID: 464653278

Link challenge: https://www.hackerrank.com/challenges/waiter/problem

Waiter