fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define SIZE 101
  5.  
  6. typedef struct
  7. {
  8. int data[SIZE];
  9. int front, rear;
  10. } Queue;
  11.  
  12. void init(Queue *q)
  13. {
  14. q->front = q->rear = 0;
  15. }
  16.  
  17. int is_full(Queue *q)
  18. {
  19. return ((q->rear + 1) % SIZE == q->front);
  20. }
  21.  
  22. int is_empty(Queue *q)
  23. {
  24. return (q->front == q->rear);
  25. }
  26.  
  27. void push(Queue *q, int n)
  28. {
  29.  
  30. if (is_full(q))
  31. return;
  32. q->rear = (q->rear + 1) % SIZE;
  33. q->data[q->rear] = n;
  34. }
  35.  
  36. int pop(Queue *q)
  37. {
  38. if (is_empty(q))
  39. return -1;
  40. q->front = (q->front + 1) % SIZE;
  41. return q->data[q->front];
  42. }
  43.  
  44. int main(void)
  45. {
  46.  
  47. int t, m, n, i, num, j, check;
  48. scanf("%d", &t);
  49.  
  50. for (i = 0; i < t; i++)
  51. {
  52. check = 0;
  53. Queue q;
  54. init(&q);
  55.  
  56. scanf("%d %d", &n, &m);
  57. for (j = 0; j < n; j++)
  58. {
  59. scanf("%d", &num);
  60. push(&q, num);
  61. }
  62. if (n == 1)
  63. {
  64. printf("%d\n", 1);
  65. check = 1;
  66. }
  67. else if (n != 1)
  68. {
  69. for (j = 0; j < m - 1; j++)
  70. {
  71. int tmp = pop(&q);
  72. push(&q, tmp);
  73. }
  74. }
  75. if (check == 0)
  76. printf("%d\n", pop(&q));
  77. }
  78.  
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0.01s 5432KB
stdin
1
6 0
1 1 9 1 1 1
stdout
1